The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

> How do I correctly connect and query MySQL using MySQLi ?, not sure how to convert from MySQL to MySQLi
Jasonc310771
post Jan 31 2013, 08:18 AM
Post #1





Group: Members
Posts: 1
Joined: 31-January 13
Member No.: 18,568



I have started to convert my site which uses MySQL so it will use MySQLi but having a few problems..

I have the connection file...

CODE

/*    DATABASE ROUTINES    */
function dbConnect() {
  $dbhost = 'localhost';
  $dbuser = '****_****';    $dbpass = '****';    $dbname = '****_****';
        // mysqli - start
        $mysqli = new mysqli("$dbhost", "$dbuser", "$dbpass", "$dbname");
        // mysqli - end
}
/*    END OF DATABASE ROUTINES        */


and the query function that I use...

CODE

//Function to query the database.
  function db_query($query) {
    //$result = mysql_query($query) or db_error($query, mysql_errno(), mysql_error());
    $result = $mysqli->query("$query");
    return $result;
  }


But get the following error...

Fatal error: Call to a member function query() on a non-object ..... on line 1216

which is this line...
CODE

$result = $mysqli->query("$query");


What is likely to be wrong with my code or method to access the data ?

The query itself has not changed.

I have just checked and I have not got a function of my own called 'query' either, so it is not that.



EDIT: I have just added extra code taken from php.net to show if the connection wored or not, no error only the host info shown. So I take it that the connection works. It has to be something with the actual db_query function. But looking at other pages I googled, they all show the same sort of query method, I even tried with and without the quotes around the string $result = $mysqli->query("$query"); and just $result = $mysqli->query("$query");

Just to see what would happen I tried to get my db_query to run the same query instead of what was in the $query string, and I get the same errors.
CODE

//Function to query the database.
  function db_query($query) {
    //$result = mysql_query($query) or db_error($query, mysql_errno(), mysql_error());
    $result = $mysqli->query("SELECT * FROM `cats` WHERE 1");
    return $result;
  }



I am really at a loss as to why this won't work. Never used mysqli before.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Posts in this topic


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 26th April 2024 - 12:19 PM