Jump to content

Need Basic Help Converting from mysql_ to mysqli_


Rogue3

Recommended Posts

I'm updating code to get rid of the PHP errors in the error log after moving to PHP 5.6. I'd rather correct the code than suppress the errors. I've tinkered with fixing this one but I keep crashing the code so I'm obviously leaving something out. It's a pretty short script, so hoping someone more versed in this language can show me how to make the mysqli_ corrections. Thanks in advance!

<?php 

$host = "localhost"; 
$user = "XYZ"; 
$pass = "XYZ"; 
$dbname = "XYZ"; 
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>"); 
mysql_select_db($dbname);

$prefix = "nuke";
$query = mysql_query("SELECT * FROM ".$prefix."_jreviews"); 
$num_rows = mysql_num_rows($query);
$rannum = rand(1,$num_rows); 

$sql = "SELECT id, title, imagethumb FROM ".$prefix."_jreviews WHERE id='$rannum'"; 
$result = mysql_query($sql);
list($id, $title, $imagethumb) = mysql_fetch_array($result); 

if (!empty($imagethumb)) { 
   echo "<a href=\"/content/modules.php?name=JReviews&rop=showcontent&id=$id\"><img src=\"$imagethumb\" align=\"center\" border=\"0\" width=\"100\" height=\"100\" alt=\"$title\" title=\"$title\"></a>"; 
 
} else { 
   echo "<a href=\"/content/modules.php?name=JReviews&rop=showcontent&id=$id\"><img src=\"/images/rdr-blank.jpg\" align=\"center\" border=\"0\" width=\"100\" height=\"100\" alt=\"$title\" title=\"$title\"></a>";

} 

mysql_close($connection);

?> 
Link to comment
Share on other sites

Why do I not see any mysqlI changes here?

 

Have you read the php manual to compare the old MySQL functions that you have used to the new/different mysqlI functions? There are pretty good examples of using mysqlI that should help you.

 

Of course - if you have PDO available to you THAT would be the way to go.

 

PS - do you have php error checking turned on and if so WHAT error messages do you have to show us?

Link to comment
Share on other sites

the way to convert code to use a new database extension, is to learn enough about the new extension to be able to make a connection, execute a query, and fetch data from a query. note: php automatically closes database connections when the script ends, so you don't need to and can remove, rather than convert, any code that's closing a connection.

 

next, forget about the mysqli extension. it is inconsistent and more complicated then needed. instead, learn and use the much simpler and more consistent PDO extension. the PDO extension has an added advantage in that once you learn the php statements to perform the database operations, you can use those same php statements with other database types, rather than to learn a new set of statements for each different database type. you sill have to make any sql changes for each different database type, but the php code remains the same.

 

also, your error handling should NOT unconditionally output the raw error information when on a live/public site. this gives hackers information they can use when they intentionally cause connection/query errors. you also don't have any error handling for the queries. if you use exceptions to handle database connection/query errors, and let php catch the exception, it will use its error_reporting, display_errors, and log_errors settings to control what happens with the actual error information. this will let display errors when developing and debugging code and log errors on a live/public site. this will also let you eliminate any existing error handling logic (you won't have to convert it) and it will give you error handling for the statements that doesn't have it now.

 

lastly, if any rows of data have been deleted from the table, your current method of getting a random row (which should use SELECT COUNT(*), rather than querying for all the rows), will exclude data with higher id's. you should instead query for the minimum and maximum id, get a random number between those two values, then because there can be holes in the id's, use WHERE id >= $rannum LIMIT 1 in the second query.

Link to comment
Share on other sites

The output error being received is:

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /path/path/path/Random_Review.php

 

I did try to convert it to mysqli but was not very successful. Unfortunately, I'm not very adept at writing PHP code, so I'm sure there's some basic notation that I'm missing. 

Link to comment
Share on other sites

That's where READING THE MANUAL will help you.

Wow, thanks for that. What exactly is the point of having a PHP Coding Help forum if all everyone is going to do is tell you to read the manual? I worked on this and researched this for hours before I came here and pretty much exhausted the level of my expertise. That's whay I came here, for help. But thanks for that advice. I never knew to check there. 

Link to comment
Share on other sites

I simply asked how to convert the code from mysql_ to mysqli_ 

 

It wasn't a long script so I didn't think I need to post what failed, as I had undone the changes as the script failed to run. I tried most of the obvious solutions to convert this but it still failed, so I'm sure there is some extra syntax that I'm forgetting with the new mysqli. 

Link to comment
Share on other sites

After some trial and error, I may have finally got all of the code fixed properly. I would love it if someone could just have a look and make sure everything looks OK. The script is running as expected, and I'm not getting any errors in my error log. 

<?php 
 
$host = "XYZ"; 
$user = "XYZ"; 
$pass = "XYZ"; 
$dbname = "XYZ"; 
$connection = mysqli_connect($host,$user,$pass) or die (mysqli_errno().": ".mysqli_error()."<BR>"); 
$database = mysqli_select_db($connection, $dbname);
 
$prefix = "nuke";
$query = mysqli_query($connection, "SELECT * FROM ".$prefix."_jreviews"); 
$num_rows = mysqli_num_rows($query);
$rannum = rand(1,$num_rows); 
 
$sql = "SELECT id, title, imagethumb FROM ".$prefix."_jreviews WHERE id='$rannum'"; 
$result = mysqli_query($connection, $sql);
list($id, $title, $imagethumb) = mysqli_fetch_array($result); 
 
if (!empty($imagethumb)) { 
   echo "<a href=\"/content/modules.php?name=JReviews&rop=showcontent&id=$id\"><img src=\"$imagethumb\" align=\"center\" border=\"0\" width=\"100\" height=\"100\" alt=\"$title\" title=\"$title\"></a>"; 
 
} else { 
   echo "<a href=\"/content/modules.php?name=JReviews&rop=showcontent&id=$id\"><img src=\"/images/rdr-blank.jpg\" align=\"center\" border=\"0\" width=\"100\" height=\"100\" alt=\"$title\" title=\"$title\"></a>";
 
} 
 
 
?>
I also removed the line closing the connection, as recommended.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.