The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> 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
Brian Chandler
post Feb 1 2013, 08:40 AM
Post #2


Jocular coder
********

Group: Members
Posts: 2,298
Joined: 31-August 06
Member No.: 43



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

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Feb 3 2013, 12:33 AM
Post #3


Jocular coder
********

Group: Members
Posts: 2,298
Joined: 31-August 06
Member No.: 43



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

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Feb 3 2013, 08:42 AM
Post #4


.
********

Group: WDG Moderators
Posts: 8,008
Joined: 10-August 06
Member No.: 7



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.
User is online!PM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
digitalpurpose
post Apr 2 2013, 10:10 AM
Post #5


Novice
**

Group: Members
Posts: 27
Joined: 7-March 11
Member No.: 14,063



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

?>


This post has been edited by digitalpurpose: Apr 2 2013, 10:11 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
jimlongo
post Oct 12 2013, 03:33 PM
Post #6


This is My Life
*******

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



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.

This post has been edited by jimlongo: Oct 12 2013, 03:35 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Oct 13 2013, 11:46 AM
Post #7


Jocular coder
********

Group: Members
Posts: 2,298
Joined: 31-August 06
Member No.: 43



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
jimlongo
post Oct 13 2013, 11:57 AM
Post #8


This is My Life
*******

Group: Members
Posts: 1,125
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

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: 19th January 2019 - 03:16 PM