Updating from MySQL to MySQLi

Updating from MYSQL to MYSQLI

You can download a converter tool from here:

https://github.com/philip/MySQLConverterTool

The code it generates is pretty gross, mainly because of the way it implements the default database link argument with a $GLOBAL variable. (This also makes it easy to recognize when someone is using code that's gone through the converter.)

There's also a MySQL Shim Library located here:

https://github.com/dshafik/php7-mysql-shim

How to change mysql to mysqli?

The first thing to do would probably be to replace every mysql_* function call with its equivalent mysqli_*, at least if you are willing to use the procedural API -- which would be the easier way, considering you already have some code based on the MySQL API, which is a procedural one.

To help with that, the MySQLi Extension Function Summary is definitely something that will prove helpful.

For instance:

  • mysql_connect will be replaced by mysqli_connect
  • mysql_error will be replaced by mysqli_error and/or mysqli_connect_error, depending on the context
  • mysql_query will be replaced by mysqli_query
  • and so on

Note: For some functions, you may need to check the parameters carefully: Maybe there are some differences here and there -- but not that many, I'd say: both mysql and mysqli are based on the same library (libmysql ; at least for PHP <= 5.2)

For instance:

  • with mysql, you have to use the mysql_select_db once connected, to indicate on which database you want to do your queries
  • mysqli, on the other side, allows you to specify that database name as the fourth parameter to mysqli_connect.
  • Still, there is also a mysqli_select_db function that you can use, if you prefer.
Once you are done with that, try to execute the new version of your script... And check if everything works ; if not... Time for bug hunting ;-)

Updating from MySQL to MySQLi issues with pulling data from database

You cannot simply replace mysql_* with mysqli_*. They have different syntaxes.
You should for example fix how you execute the query. Mysqli expects two parameters: the connection and then the query. You pass just the query:

<?php
$connect = mysqli_connect('localhost', 'root', 'Password');
$select_db = mysqli_select_db('stats');
$id = mysqli_real_escape_string($_GET['UUID']);
//Remove LIMIT 1 to show/do this to all results.
$query = 'SELECT * FROM `playerslog` WHERE `UUID` = '.$id.' LIMIT 1';
$result = mysqli_query($connect, $query);
$row = mysqli_fetch_array($result);
// Echo page content

?>

from mysql to mysqli updating code

mysqli_query() returns :

For successful SELECT, SHOW, DESCRIBE, or EXPLAIN queries it will return a mysqli_result object.

For other successful queries it will return TRUE. FALSE on failure

As evident from your error, you are getting False as $result which you are then passing in mysqli_num_rows(), which expects as mysqli_result.

You need to modify your $sql as following as there may be ambiguous column error, if same column names exists in both tables products and products_description.

$sql = "SELECT p.products_id, pd.products_name, p.products_quantity, p.products_weight, p.products_price 
FROM products p
INNER JOIN products_description pd on p.products_id = pd.products_id";

Upgrading to MySQLi - As easy as swapping out mysql for mysqli?

Check out this SO question which provides answers to at least most of your functions.

For example:

  • mysql_connect will be replaced by mysqli_connect
  • mysql_error will be replaced by mysqli_error and/or mysqli_connect_error, depending on the context
  • mysql_query will be replaced by mysqli_query

updating the MySQL database with time by using mysqli_multi_query (php)

You need quotes around the time.

$time = date("h:i");
$query = "UPDATE a_2020 SET done = 'yes' WHERE id = '2' ;";
$query .= "UPDATE a_2020 SET nowTime = '$time' WHERE id = '1' ";
$result = mysqli_multi_query($con,$query);
echo mysqli_error($con);

Note that in my experience, there's rarely a good reason to use mysqli_multi_query(). It provides little benefit and just makes things more complicated. Just call mysqli_query() twice.

And it's generally better to use prepared statements, which aren't available with mysqli_multi_query(). This avoids quoting problems and also protects against SQL injection.



Related Topics



Leave a reply



Submit