Im trying to convert mysql query to mysqli but it doesnt work...why?
From:

mysql_query("UPDATE views SET views=views+1 WHERE viewID='1'");

To:

  $q1=$mysqli->query("UPDATE views SET views=views+1 WHERE viewID='1'");

Errors:

Notice: Undefined variable: mysqli in C:\wamp\www\SICS\home.php on line 5
Fatal error: Call to a member function query() on a non-object in C:\wamp\www\SICS\home.php on line 5

Recommended Answers

All 5 Replies

What does your connection string look like?\

Should be something like this: $mysqli = new mysqli("host", "user", "pass", "db");

then you can use the $mysqli object.

I got a similar problem. My connecting string is exactly like above. However, the website is a litle complex and a lot of things are done through functions in include pages.

The connection string resides in a page called dbconnect where the login credentials ar called from another include page which works just fine.

But now for example I have another include page where I query the database and with the old mysql query it worked just fine like:`

function getSetting($property)
   {
       global $dbpraefix;
       $res = mysql_query("SELECT value FROM ".$dbpraefix."settings
                           WHERE property = '".$property."'");
       $row = mysql_fetch_row($res);
       return $row[0];
   }`

If I convert this to mysqli like:

function getSetting($property){
    global $dbpraefix;
    $res = $mysqli->query("SELECT value FROM ".$dbpraefix."settings
                           WHERE property = '".$property."'");
    $row = $res-num_rows;
    return $row[0];
}

I am getting the following error codes:

` Notice: Undefined variable: mysqli in C:\wamp\www\
Fatal error: Call to a member function query() on a non-object in C:\wamp\www

If anybody could help me out and point me to what I've missed would be nice.
Thanks a lot.

Can you show your dbconnect code?

No problem. Since I just started converting from mysql to mysqli, there are still the old commented mysql statements in.

<?php

include("dbsettings.php");

$db_connect = new mysqli($dbhost, $dbuser, $dbpass, $db);

//$db_connect = mysql_connect($dbhost, $dbuser, $dbpass);

if(mysqli_connect_errno()){
    exit("Connection failed: ".mysqli_connect_errno());
}

//mysql_select_db($db, $db_connect);

?>

Try passing your db_connect object to your functions.

    function getSetting($connection, $property){
        global $dbpraefix;
        $res = $mysqli->query($connection, "SELECT value FROM ".$dbpraefix."settings
        WHERE property = '".$property."'");
        $row = $res-num_rows;
        return $row[0];
    }

or setting your db_connect to a global

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.