Viewed   89 times

So as MYSQL is deprecated and eveyone keeps telling me to update, I thought it was about time I did.

But as I'm not used to mysqli_*, it seems alien to me. And it's not a simple edit when I have a whole site coded in Mysql.

So I'm wondering: How would I transform the below code into Mysqli? Just to give me and anyone else a good starting point when dealing with querying the database.

$sql_follows="SELECT * FROM friends WHERE user1_id=".$_SESSION['id']." AND status=2 OR user2_id=".$_SESSION['id']." AND status=2";
$query_follows=mysql_query($sql_follows) or die("Error finding friendships");
if($query_follows>0){
}

EDIT: On reading up and editing my whole site the above code converted to MYSQLI_ would go something like this..

 $Your_SQL_query_variable= mysqli_query($connectionvariable,"SELECT * FROM friends WHERE user1_id=".$_SESSION['id']." AND status=2 OR user2_id=".$_SESSION['id']." AND status=2")) {
        printf("Error: %sn", $mysqli->error);
    }

 Answers

1

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

Monday, October 31, 2022
3

14 [Line 14] Please check your code for parse errors, we failed to parse " ". Conversion will be incomplete!".

This error is caused by the space before the ( in your mysql_connect() call. Replacing it with $conn=mysql_connect("$localhost", "$dbusername", "$dbpass"); removes this warning output by MySQLConverterTool.

The remaining two errors are things that you should deal with by actually looking, yourself, at the difference between mysql_connect() and mysqli_connect(). mysql_connect()’s first argument, $server, can be formatted like hostname:port whereas with mysqli_connect() you would pass only hostname to its first argument and pass port as an optional fifth parameter. Also, mysqli would have you specify the database in the mysqli_connect() call instead of having a separate function analogous to mysql_select_db().

I suggest that, if you need, you use the converter tool to convert all of your sourcecode from mysql to mysqli except for these lines with the warnings in them. Only you know what format "$localhost" comes in: if it might contain port information, you must separate the port information out. You should probably set the database to use in mysqli_connect() instead of using the converter’s automatic USE $db shim. This is exactly what the converter is trying to tell you :-).

Just to note, I would not say:

My connect file (I've taken out what I think is unnecessary code such as error handling for simplicity) creates errors that I'm not sure how to fix.

The above suggests that the PHP code generated by the converter is, itself, throwing PHP warnings and errors at runtime (not that the converter is complaining about your original code or informing you that you need to actually do some manual conversion as I discussed above). That is why we were looking for errors like the once-missing semicolon which you corrected.

Friday, August 26, 2022
 
aportr
 
4
<?php
$sql = new mysqli('127.0.0.1','root','Qwert12345','plot_io_db');
//echo $sql->query('Select * From players');
?>

It will work. Just remove port from localhost (127.0.0.1)

Tuesday, August 30, 2022
 
1

UPDATE queries do not return result objects, it returns TRUE (assuming success). You're then trying to call TRUE->fetch_array(), which obviously won't work.

Now, for doing what you actually want to do, try this:

$row = $mysqli->query("SELECT.....")->fetch_array();
Friday, September 9, 2022
 
5

mysqli_connect("","username" ,"password","databasename");//Server name cannot be NULL

use loaclhost for server name(In Loacl)

<?php
    $con = mysqli_connect("localhost","username" ,"password","databasename");

    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
?>

Or can use MySQLi Procedural

<?php
    $servername = "localhost";
    $username = "username";
    $password = "password";

    // Create connection
    $con = mysqli_connect($servername, $username, $password);

    // Check connection
    if (!$con) {
        die("Connection failed: " . mysqli_connect_error());
    }
    echo "Connected successfully";
?>

EDIT 01

$servername = "localhost";
$username = "root";
$password = "";
Monday, October 10, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :