Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-01-2013, 02:07 PM   PM User | #1
countrydj
Regular Coder

 
Join Date: Nov 2011
Location: Preston, UK
Posts: 130
Thanks: 36
Thanked 0 Times in 0 Posts
countrydj is an unknown quantity at this point
How do I change from mysql to mysqli ?

I have a script that is written in PHP and MySql. It works well.

However, I think it is now time to convert to MySqli.

I am using: php-5.3.3-14.el6_3.x86_64 and mysql-5.1.66-2.el6_3.x86_64 on CentOS release 6.3 (Final)

I have a global.php file which is called in my add.php page
My global.php contains:
PHP Code:
//connect to the database
if(!($link=mysql_pconnect($hostName$userName$password))){
    
displayErrMsg(sprintf("Error connecting to the host %s, by user %s"$hostName$userName));
    exit();
}

//select the database
if(!mysql_select_db($databaseName$link)){
    
displayErrMsg(sprintf("Error in selecting the %s database"$databaseName));
    
displayErrMsg(sprintf("error: %d %s"mysql_errno($link), mysql_error($link)));
    exit();

My add.php page contains:
PHP Code:
$qry "INSERT INTO " $vars["table directory"] . " "etc...

and

if(!(
$results mysql_query($qry$link))){
        
displayErrMsg(sprintf("Error in executing %s query"$qry));
        
displayErrMsg(sprintf("error:%d %s"mysql_errno($link), mysql_error($link)));
        exit();
    } 
Can anybody advice if it is a straight forward job to convert ???

I have a few scripts, that were written over the years, that could do with converting.

Thanks,
__________________
The MAN, The MYTH, The LEGEND:
John C
________________________________
Support your local Country Music Club
countrydj is offline   Reply With Quote
Old 02-01-2013, 07:39 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This is pretty good so far for easy conversions with procedural mysqli.
Connection is easier:
PHP Code:
$link mysqli_connect($hostName$userName$password$databaseName);
if (!
$link)
{
    
displayErrMsg(sprintf("Error connecting to the host %s, by user %s on database %s.  Cause: %s (%d)"$hostName$userName$databaseNamemysqli_connect_error(), mysqli_connect_errno())); 

There is no separate database selection mechanism anymore.

As for querying and errors, without going into prepared statements, you can almost use what you have. The mysql_query is replaced with mysqli_query, and the parameters are reversed. So the $link is ALWAYS required and is the first argument.
The error calls are the same, just again with mysqli_error/errno instead of the mysql_error/errno.
So your previous use of $link as a requirement, will actually help you with the conversion process.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
countrydj (02-03-2013)
Old 02-03-2013, 11:34 AM   PM User | #3
countrydj
Regular Coder

 
Join Date: Nov 2011
Location: Preston, UK
Posts: 130
Thanks: 36
Thanked 0 Times in 0 Posts
countrydj is an unknown quantity at this point
Hi Fou-Lu ...
Many thanks for your advise.

I didn't realise how easy it was.
I have tried in the past, but I think that I didn't put $link first, so it didn't work.
I made the same mistake this time, but went back to your post a read it more closely.

I have now changed my current project to mysqli.
I will now change all my other database projects.

Many thanks,
__________________
The MAN, The MYTH, The LEGEND:
John C
________________________________
Support your local Country Music Club
countrydj is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:49 AM.


Advertisement
Log in to turn off these ads.