Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Databases _ How do I correctly connect and query MySQL using MySQLi ?

Posted by: Jasonc310771 Jan 31 2013, 08:18 AM

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.

Posted by: Brian Chandler Feb 1 2013, 08:40 AM

IIUC, this is just a different, object-oriented interface to MySQL from PHP. If you have a working page using the older function interface, I think it is supremely pointless converting it for no good reason. If you know nothing about OO programming, it's an even worse idea.

You have:

QUOTE
mysqli("$dbhost", "$dbuser", "$dbpass", "$dbname");


Why do you write "$dbuser"? Do you understand what the quotes mean? If $text is a string, then "$text" is exactly the same string; if $number is a number, such as 4, then "$number" is the string "4", which is not a number.

You need to read the bit of the manual about "strings" and other types in PHP; then do you see what you need to do? (Sorry, I'm not going to just give you the answer.)


Posted by: Brian Chandler Feb 3 2013, 12:33 AM

Beg your pardon! The php website says that the php-mysql interface is deprecated and "will be removed in future".

I wouldn't take this very seriously. There must be thousands and thousands of websites using massive amounts of code written with this interface, some of them convoluted. If the php people removed the interface (which would be an act of vandalism, basically) someone will either fork PHP, or write a library to emulate the calls by converting them to the mysqli interface. (This would be very easy, at least for all the simple cases.)


Posted by: Christian J Feb 3 2013, 08:42 AM

Meanwhile there's a drop-in replacement for MySQL being developed: http://en.wikipedia.org/wiki/Mariadb

Don't know if you can combine that with MYSQLi.

Posted by: digitalpurpose Apr 2 2013, 10:10 AM

CODE

<?php

function your_db_connect() {
  $db_user= 'your_db_user';
  $db_pass= 'your_db_pass';
  $db_name= 'your_db_name';
  
  $link = mysqli_connect('localhost', $db_user, $db_pass, $db_name);

  return $link;
}

# This is pretty much a waste, since the actual mysqli_query function is the same thing...
# but, if you add more functionality to it, this is the starting point.

function your_db_query($link, $query) {
  $result = mysqli_query($link, $query);
  
  return $result;
}

# Same as above, pretty redundant, but if you want to create your own proprietary db functions,
# you need to remember to close the connection as well.

function your_db_close($link) {
  mysqli_close($link);
}

?>

Posted by: jimlongo Oct 12 2013, 03:33 PM

MariaDB can be used with MYSQLi. The php interface is exactly the same. PhpMyAdmin works with it. It's a "drop-in" replacement so once you install you'd never know the difference.

Php will remove MYSQL extensions in the near future, I think you can bank on that. The solution would be to stay on a server with an older version of Apache/PHP.

Posted by: Brian Chandler Oct 13 2013, 11:46 AM

QUOTE(jimlongo @ Oct 13 2013, 05:33 AM) *

MariaDB can be used with MYSQLi. The php interface is exactly the same. PhpMyAdmin works with it. It's a "drop-in" replacement so once you install you'd never know the difference.

Php will remove MYSQL extensions in the near future, I think you can bank on that. The solution would be to stay on a server with an older version of Apache/PHP.


Why do you think that? I guess it might break something over 20% of all working websites, for no purpose. And if they did, as I said someone will simply add it back in.

Posted by: jimlongo Oct 13 2013, 11:57 AM

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.


Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)