0

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

Edited by RonKevinT.Manuela

3
Contributors
5
Replies
47
Views
2 Years
Discussion Span
Last Post by gabrielcastillo
0

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.

0

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.

0

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);

?>
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.