Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,343 members, 7,808,209 topics. Date: Thursday, 25 April 2024 at 08:45 AM

Please How Can I Convert This Mysql Code To Mysqli - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Please How Can I Convert This Mysql Code To Mysqli (1310 Views)

Download E-commerce System Using Php/mysqli With Free Source Code / Get Responsive E-learning System Using Php/mysqli With Free Source Code / Someone Should Help Me With This Mysql And Vue Problem (2) (3) (4)

(1) (Reply) (Go Down)

Please How Can I Convert This Mysql Code To Mysqli by Kingvick: 8:07am On Nov 14, 2018
I need help in converting this codes

if(!get_magic_quotes_gpc())
{
$_GET = array_map('mysql_real_escape_string', $_GET);
$_POST = array_map('mysql_real_escape_string',
$_POST);
$_COOKIE = array_map('mysql_real_escape_string',
$_COOKIE);
}
else
{ $_GET = array_map('stripslashes', $_GET);
$_POST = array_map('stripslashes', $_POST);
$_COOKIE = array_map('stripslashes', $_COOKIE);
$_GET = array_map('mysql_real_escape_string', $_GET);
$_POST = array_map('mysql_real_escape_string',
$_POST);
$_COOKIE = array_map('mysql_real_escape_string',
$_COOKIE);
}

tried this but its not working



if(!get_magic_quotes_gpc())
{
$_GET = array_map(mysqli_real_escape_string($db, $_GET));
$_POST = array_map(mysqli_real_escape_string($db, $_POST));
$_COOKIE = array_map(mysqli_real_escape_string($db, $_COOKIE));
}
else
{
$_GET = array_map('stripslashes', $_GET);
$_POST = array_map('stripslashes', $_POST);
$_COOKIE = array_map('stripslashes', $_COOKIE);
$_GET = array_map(mysqli_real_escape_string($db, $_GET));
$_POST = array_map(mysqli_real_escape_string($db, $_POST));
$_COOKIE = array_map(mysqli_real_escape_string($db, $_COOKIE));
}
Re: Please How Can I Convert This Mysql Code To Mysqli by MrWondah(m): 10:18am On Nov 14, 2018
Kingvick:
if(!get_magic_quotes_gpc())
{
$_GET = array_map(mysqli_real_escape_string($db, $_GET));
$_POST = array_map(mysqli_real_escape_string($db, $_POST));
$_COOKIE = array_map(mysqli_real_escape_string($db, $_COOKIE));
}
else
{
$_GET = array_map('stripslashes', $_GET);
$_POST = array_map('stripslashes', $_POST);
$_COOKIE = array_map('stripslashes', $_COOKIE);
$_GET = array_map(mysqli_real_escape_string($db, $_GET));
$_POST = array_map(mysqli_real_escape_string($db, $_POST));
$_COOKIE = array_map(mysqli_real_escape_string($db, $_COOKIE));
}
Can you give more details?
Re: Please How Can I Convert This Mysql Code To Mysqli by Kingvick: 1:19pm On Nov 14, 2018
MrWondah:
Can you give more details?
Pls check the topic again I modified it
Re: Please How Can I Convert This Mysql Code To Mysqli by MrWondah(m): 1:51pm On Nov 14, 2018
Kingvick:
I need help in converting this codes



tried this but its not working

Your code looks good. But it might be a problem with your DB connection. Did you use mysqli or mysql in your db connection? if there is discrepancy your code wont work. So it would be better if you share the error code you got and the db connection code
Re: Please How Can I Convert This Mysql Code To Mysqli by Kingvick: 2:10pm On Nov 14, 2018
MrWondah:

Your code looks good. But it might be a problem with your DB connection. Did you use mysqli or mysql in your db connection? if there is discrepancy your code wont work. So it would be better if you share the error code you got and the db connection code
i made use of mysqli here is the complete convertion i tried

<?php

$dbname = ""; // Your Sql Database Name

$dbuser = ""; // Your sql database username

$dbhost = ""; // its always localhost, except if your host states otherwise

$dbpass = ""; // Your sql database password


$db = mysqli_connect("$dbhost", "$dbuser", "$dbpass"wink or die("Could not connect."wink;
if(!$db)
die("no db"wink;
if(!mysqli_select_db($db,"$dbname"wink)
die("No database selected."wink;
if(!get_magic_quotes_gpc())
{
$_GET = array_map(mysqli_real_escape_string($db, $_GET));
$_POST = array_map(mysqli_real_escape_string($db, $_POST));
$_COOKIE = array_map(mysqli_real_escape_string($db, $_COOKIE));
}
else
{
$_GET = array_map('stripslashes', $_GET);
$_POST = array_map('stripslashes', $_POST);
$_COOKIE = array_map('stripslashes', $_COOKIE);
$_GET = array_map(mysqli_real_escape_string($db, $_GET));
$_POST = array_map(mysqli_real_escape_string($db, $_POST));
$_COOKIE = array_map(mysqli_real_escape_string($db, $_COOKIE));
}
?>

Am geting this errors

[14-Nov-2018 03:13:03 UTC] PHP Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in /home/yukhubco/public_html/incfiles/connect.php on line 19
[14-Nov-2018 03:13:03 UTC] PHP Warning: array_map() expects at least 2 parameters, 1 given in /home/yukhubco/public_html/incfiles/connect.php on line 19
[14-Nov-2018 03:13:03 UTC] PHP Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in /home/yukhubco/public_html/incfiles/connect.php on line 20
[14-Nov-2018 03:13:03 UTC] PHP Warning: array_map() expects at least 2 parameters, 1 given in /home/yukhubco/public_html/incfiles/connect.php on line 20
[14-Nov-2018 03:13:03 UTC] PHP Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in /home/yukhubco/public_html/incfiles/connect.php on line 21
[14-Nov-2018 03:13:03 UTC] PHP Warning: array_map() expects at least 2 parameters, 1 given in /home/yukhubco/public_html/incfiles/connect.php on line 21
Re: Please How Can I Convert This Mysql Code To Mysqli by MrWondah(m): 2:32pm On Nov 14, 2018
Kingvick:


Am geting this errors

[14-Nov-2018 03:13:03 UTC] PHP Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in /home/yukhubco/public_html/incfiles/connect.php on line 19
[14-Nov-2018 03:13:03 UTC] PHP Warning: array_map() expects at least 2 parameters, 1 given in /home/yukhubco/public_html/incfiles/connect.php on line 19
[14-Nov-2018 03:13:03 UTC] PHP Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in /home/yukhubco/public_html/incfiles/connect.php on line 20
[14-Nov-2018 03:13:03 UTC] PHP Warning: array_map() expects at least 2 parameters, 1 given in /home/yukhubco/public_html/incfiles/connect.php on line 20
[14-Nov-2018 03:13:03 UTC] PHP Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in /home/yukhubco/public_html/incfiles/connect.php on line 21
[14-Nov-2018 03:13:03 UTC] PHP Warning: array_map() expects at least 2 parameters, 1 given in /home/yukhubco/public_html/incfiles/connect.php on line 21


I'll take more look at your code, but from what I understand so far,

mysqli_real_escape_string() expects parameter 2 to be string, array given in /home/yukhubco/public_html/incfiles/connect.php on line 19

you can only pass a string and not an array in that function. Another option is for you to iterate through an array and apply it to the values (and keys since you're handling post and get).
Re: Please How Can I Convert This Mysql Code To Mysqli by Codedboy95(m): 4:03pm On Nov 14, 2018
Kingvick:
I need help in converting this codes

if(!get_magic_quotes_gpc())
{
$_GET = array_map('mysql_real_escape_string', $_GET);
$_POST = array_map('mysql_real_escape_string',
$_POST);
$_COOKIE = array_map('mysql_real_escape_string',
$_COOKIE);
}
else
{ $_GET = array_map('stripslashes', $_GET);
$_POST = array_map('stripslashes', $_POST);
$_COOKIE = array_map('stripslashes', $_COOKIE);
$_GET = array_map('mysql_real_escape_string', $_GET);
$_POST = array_map('mysql_real_escape_string',
$_POST);
$_COOKIE = array_map('mysql_real_escape_string',
$_COOKIE);
}


are you updating to php7.* ? if so get_magic_quotes_gpc() is depreciated as of php5.2 so it will throw an error on higher php versions

make use of this for your connection
<?php
$dbname = " "; // Your Sql Database Name
$dbuser = " "; // Your sql database username

$dbhost = " "; // its always localhost, except if your host states otherwise

$dbpass = " "; // Your sql database password


$db=($db = mysqli_connect("$dbhost", "$dbuser", "$dbpass", "$dbname", "3306"wink) or die ('Cannot connect to the database because: ' . ((is_object($db)) ? mysqli_error($db) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
?>

then the best way to sanitize user input is using prepared statement (PDO) instead of mysqli_real_escape. Read about prepared statement pdo
Re: Please How Can I Convert This Mysql Code To Mysqli by Kingvick: 6:08pm On Nov 14, 2018
Codedboy95:


are you updating to php7.* ? if so get_magic_quotes_gpc() is depreciated as of php5.2 so it will throw an error on higher php versions

make use of this for your connection
<?php
$dbname = " "; // Your Sql Database Name
$dbuser = " "; // Your sql database username

$dbhost = " "; // its always localhost, except if your host states otherwise

$dbpass = " "; // Your sql database password


$db=($db = mysqli_connect("$dbhost", "$dbuser", "$dbpass", "$dbname", "3306"wink) or die ('Cannot connect to the database because: ' . ((is_object($db)) ? mysqli_error($db) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
?>

then the best way to sanitize user input is using prepared statement (PDO) instead of mysqli_real_escape. Read about prepared statement pdo
Yes am updating to php7. Thanks for this information
Re: Please How Can I Convert This Mysql Code To Mysqli by Nobody: 8:23pm On Nov 14, 2018
Kingvick:
Yes am updating to php7. Thanks for this information

As codeBoy advice please use PDO. This will help you with prepared statements to avoid SQL injection.


In Php7+ magic_quotes_gpc() is depreciated.


Now to your question, the issue was with your datatype. Mysqli_realescape_strug accepts

a. A mysqli Link
b. A string variable data type to be escaped.

Now in your code you passed the GET and POST variable global. So if you really want to escape all the variables in that array. Kindly use a for loop statement..




$iLen = count ($_GET)
cleanedUpGet=array();

foreach( $_GET as $key => $value ) {

cleanedUpGet[$key] = mysqli_realescape_string($dbConnection, $value)

}


var_dump($_GET)



______________________________

If you want to do it the Object Oriented Way do.


mysqli::escapestring($stringVariable)




_________________________

However I will advice you use PDO.


Note: I don't test the above code but I believe it would work.



@commenters: On a side note I no longer work on Php7, I left php a long time to rustLang, I wanted asking if Php community has embraced the static type introduction ??
Re: Please How Can I Convert This Mysql Code To Mysqli by DHumbleNigga(m): 8:32pm On Nov 14, 2018
Kingvick:

i made use of mysqli here is the complete convertion i tried

<?php

$dbname = ""; // Your Sql Database Name

$dbuser = ""; // Your sql database username

$dbhost = ""; // its always localhost, except if your host states otherwise

$dbpass = ""; // Your sql database password


$db = mysqli_connect("$dbhost", "$dbuser", "$dbpass"wink or die("Could not connect."wink;
if(!$db)
die("no db"wink;
if(!mysqli_select_db($db,"$dbname"wink)
die("No database selected."wink;
if(!get_magic_quotes_gpc())
{
$_GET = array_map(mysqli_real_escape_string($db, $_GET));
$_POST = array_map(mysqli_real_escape_string($db, $_POST));
$_COOKIE = array_map(mysqli_real_escape_string($db, $_COOKIE));
}
else
{
$_GET = array_map('stripslashes', $_GET);
$_POST = array_map('stripslashes', $_POST);
$_COOKIE = array_map('stripslashes', $_COOKIE);
$_GET = array_map(mysqli_real_escape_string($db, $_GET));
$_POST = array_map(mysqli_real_escape_string($db, $_POST));
$_COOKIE = array_map(mysqli_real_escape_string($db, $_COOKIE));
}
?>

Am geting this errors

[14-Nov-2018 03:13:03 UTC] PHP Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in /home/yukhubco/public_html/incfiles/connect.php on line 19
[14-Nov-2018 03:13:03 UTC] PHP Warning: array_map() expects at least 2 parameters, 1 given in /home/yukhubco/public_html/incfiles/connect.php on line 19
[14-Nov-2018 03:13:03 UTC] PHP Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in /home/yukhubco/public_html/incfiles/connect.php on line 20
[14-Nov-2018 03:13:03 UTC] PHP Warning: array_map() expects at least 2 parameters, 1 given in /home/yukhubco/public_html/incfiles/connect.php on line 20
[14-Nov-2018 03:13:03 UTC] PHP Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in /home/yukhubco/public_html/incfiles/connect.php on line 21
[14-Nov-2018 03:13:03 UTC] PHP Warning: array_map() expects at least 2 parameters, 1 given in /home/yukhubco/public_html/incfiles/connect.php on line 21



I dont know how procedural works in php because i work with OOP.
But i think i can try.

$server ="localhost";
$user="root";
$pass=""; if you are using default configuration
$db="db_you_are_conmecting_to";

//Procedural connection to db
$conn =mysqli_connect($server, $user, $pass, $db);

If($conn==mysqli_connect_error){
die("connection failed connecting to Db"wink;
}

//Object Oriented
$conn = new mysqli($server, $user, $pass, $db);

If($conn->connect_error){
die("Error connecting to db"wink;
}



About the mysqli_real_escape_string ..Two parameter must be pass to it.
E.g

$name=$_GET['name'] = "Charles";

To now escape the string b4 saving to db

$name = mysqli_real_escape_string($conn, $name);

Bro always sanitize your input before using mysqli escape string function to store the data to DB.
Re: Please How Can I Convert This Mysql Code To Mysqli by Nobody: 8:41pm On Nov 14, 2018
try harder
Re: Please How Can I Convert This Mysql Code To Mysqli by youngds: 8:59pm On Nov 14, 2018
Kingvick:
I need help in converting this codes

if(!get_magic_quotes_gpc())
{
$_GET = array_map('mysql_real_escape_string', $_GET);
$_POST = array_map('mysql_real_escape_string',
$_POST);
$_COOKIE = array_map('mysql_real_escape_string',
$_COOKIE);
}
else
{ $_GET = array_map('stripslashes', $_GET);
$_POST = array_map('stripslashes', $_POST);
$_COOKIE = array_map('stripslashes', $_COOKIE);
$_GET = array_map('mysql_real_escape_string', $_GET);
$_POST = array_map('mysql_real_escape_string',
$_POST);
$_COOKIE = array_map('mysql_real_escape_string',
$_COOKIE);
}

tried this but its not working



if(!get_magic_quotes_gpc())
{
$_GET = array_map(mysqli_real_escape_string($db, $_GET));
$_POST = array_map(mysqli_real_escape_string($db, $_POST));
$_COOKIE = array_map(mysqli_real_escape_string($db, $_COOKIE));
}
else
{
$_GET = array_map('stripslashes', $_GET);
$_POST = array_map('stripslashes', $_POST);
$_COOKIE = array_map('stripslashes', $_COOKIE);
$_GET = array_map(mysqli_real_escape_string($db, $_GET));
$_POST = array_map(mysqli_real_escape_string($db, $_POST));
$_COOKIE = array_map(mysqli_real_escape_string($db, $_COOKIE));
}
Sometimes to change from mysqli to MySQL just require you removing the 'I'
Re: Please How Can I Convert This Mysql Code To Mysqli by Kingvick: 7:50am On Nov 15, 2018
Zsatan:


As codeBoy advice please use PDO. This will help you with prepared statements to avoid SQL injection.


In Php7+ magic_quotes_gpc() is depreciated.


Now to your question, the issue was with your datatype. Mysqli_realescape_strug accepts

a. A mysqli Link
b. A string variable data type to be escaped.

Now in your code you passed the GET and POST variable global. So if you really want to escape all the variables in that array. Kindly use a for loop statement..




$iLen = count ($_GET)
cleanedUpGet=array();

foreach( $_GET as $key => $value ) {

cleanedUpGet[$key] = mysqli_realescape_string($dbConnection, $value)

}


var_dump($_GET)



______________________________

If you want to do it the Object Oriented Way do.


mysqli::escapestring($stringVariable)




_________________________

However I will advice you use PDO.


Note: I don't test the above code but I believe it would work.



@commenters: On a side note I no longer work on Php7, I left php a long time to rustLang, I wanted asking if Php community has embraced the static type introduction ??

Thanks would test the codes

(1) (Reply)

If You Want To Hire C# ASP.NET Core Developers With The Following Skills / Other Things All Programmers Should Know / Recommend A Laptop For Me Bw 50-60k

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 46
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.