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
 
Reply to this topicStart new topic
Replies
jimlongo
post Oct 13 2013, 11:57 AM
Post #2


This is My Life
*******

Group: Members
Posts: 1,128
Joined: 24-August 06
From: t-dot
Member No.: 16



Websites that require MYSQL extensions will simply need to find servers that offer those deprecated versions of Apache/PHP.

I guess someone might add it back in, I'm not sure of the who, how and why of that.

But why when all that's required is to do nothing. Just stay where you are. For many people it will be a matter of pressuring their providers to keep those old versions of php running. I guess that becomes the inflection point, if providers decide that to offer old versions of php is not worth it for them then their customers who require those services have to make a decision. Change or move.

Just like Frontpage, it still exists. Eventually most people move on. Major providers like cPanel eventually remove it. Those customers requiring it have to look elsewhere.

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: 27th April 2024 - 08:58 PM