Jump to content

Mysqli from mysql


FUNKAM35

Recommended Posts

$total_results = mysqli_result(mysqli_query("SELECT COUNT(*) as Num  FROM property  WHERE  $where "),0) ;
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num  FROM property  WHERE  $where "),0) ;

Hi, Please can anyone help

 

I have changed MySQL_

 

to mysqli_

 

but I am getting fatal error on above line, please can anyone help

Thanks

Link to comment
https://forums.phpfreaks.com/topic/297568-mysqli-from-mysql/
Share on other sites

Hi there,

 

mysql_query is deprecated and it will be removed on future PHP versions. Use mysqli_query instead.

 

mysqli_query takes two parameters - the database connection and the actual query, therefore you have to include the database connection file in your code.

 

For example :

$dbCon = mysqli_connect("localhost", "root", "password", "database_name") or die(mysqli_error());

As you can see, I am storing the database connection in a variable.

 

To implement this into mysqli_query we now need to store the actual query in a variable (you could also write the full query as the second parameter but it will make your code less readable)

$sql = "SELECT COUNT(*) as Num  FROM property  WHERE  $where "; 

Now to implement this with mysqli:

$query = mysqli_query($dbCon, $sql); 

For mysql_result you can use the below function which is basically a mysqli_result:

function mysqli_result($res,$row=0,$col=0) { 
    $numrows = mysqli_num_rows($res); 
    if ($numrows && $row <= ($numrows-1) && $row >=0) {
        mysqli_data_seek($res,$row);
        $resrow = (is_numeric($col)) ? mysqli_fetch_row($res) : mysqli_fetch_assoc($res);
        if (isset($resrow[$col])) {
            return $resrow[$col];
        }
    }
    return false;
}

After all this, your line should look like:

$total_results = mysqli_result($query, 0);

Hope this helps in a way or another.

Link to comment
https://forums.phpfreaks.com/topic/297568-mysqli-from-mysql/#findComment-1517860
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.