icue Posted February 14, 2014 Share Posted February 14, 2014 Hi, could someone please help me update this function. I have search and found many solutions but the problem is I am a novice at PHP and do not understand how to implement them. function plogger_init_collections($arr) { $sql = "SELECT COUNT(DISTINCT `parent_collection`) AS `num_items` FROM `".PLOGGER_TABLE_PREFIX."pictures`"; $result = run_query($sql); $num_items = mysql_result($result, 0, 'num_items'); $GLOBALS['total_pictures'] = $num_items; There several functions in this Plogger gallery application that uses mysql_result but I think if someone could show me a working example I could workout how to update the others. I hope I have included sufficient information and thanks in advance for any help. Quote Link to comment https://forums.phpfreaks.com/topic/286201-help-updating-mysql_result-from-mysql-to-mysqli/ Share on other sites More sharing options...
Ch0cu3r Posted February 14, 2014 Share Posted February 14, 2014 (edited) What is run_query() has that been updated to use mysqli_query()? $result should now be the query result? If that is case then use $row = $mysqli->fetch_row(); $num_items = $row[0]; Or alternately $row = $mysqli->fetch_assoc(); $num_items = $row['num_items']; Edited February 14, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/286201-help-updating-mysql_result-from-mysql-to-mysqli/#findComment-1468903 Share on other sites More sharing options...
icue Posted February 14, 2014 Author Share Posted February 14, 2014 Hi, thank you for responding. Because I was not sure how to do the update to mysqli I found an online app MySQLConverterTool which did most of the update. However, it was not able to do the mysql_result(). I have looked at the original file and I don't think run_query() has been updated. I have searched and read a lot but still do not understand how to do this. From what I have read there is an object way and a procedural way. I am not sure but I don't think app is written the object way so is it possible to mix them? Sorry for being so dumb but I am not sure how to insert what you have written into the function I posted. Quote Link to comment https://forums.phpfreaks.com/topic/286201-help-updating-mysql_result-from-mysql-to-mysqli/#findComment-1468973 Share on other sites More sharing options...
Jacques1 Posted February 14, 2014 Share Posted February 14, 2014 Hi, there is no direct equivalent to mysql_result(). You have to fetch the whole row and then extract the first value as shown by Ch0cu3r. A shorter solution would be list($num_items) = $items_query->fetch_row(); I strongly recommend that you actually learn how to use MySQLi and make use of its new features. Don't just run some tool to translate the old code. The new extension is very different in many aspects, and it's important to understand them. Quote Link to comment https://forums.phpfreaks.com/topic/286201-help-updating-mysql_result-from-mysql-to-mysqli/#findComment-1468976 Share on other sites More sharing options...
icue Posted February 15, 2014 Author Share Posted February 15, 2014 Hi, Sorry but still don't get it. I have been trying to teach myself but there is beyond my knowledge. The gallery app is quite a big app written by someone else and is no longer being updated by the developer. Based on the suggestions I replaced the following lines Line 2103 $num_items = mysql_result($result, 0, 'num_items'); with list($num_items) = $items_query->fetch_row(); Line 2650 $num_comments = mysql_result($comment_result, 0, 'num_comments'); with list($num_comments) = $items_query->fetch_row(); but I get the following errors: Notice: Undefined variable: items_query in /htdocs/mysite/portfolio/plog-includes/plog-functions.php on line 2103Fatal error: Call to a member function fetch_row() on a non-object in/htdocs/mysite/portfolio/plog-includes/plog-functions.php on line 2103 I also get the same errors for line 2650. I also have one that is slightly different line 2931 return mysql_result($numresult, 0, 'num_pictures'); Would it be possible to show me how to insert list($num_items) = $items_query->fetch_row(); into the function Quote Link to comment https://forums.phpfreaks.com/topic/286201-help-updating-mysql_result-from-mysql-to-mysqli/#findComment-1468987 Share on other sites More sharing options...
mac_gyver Posted February 15, 2014 Share Posted February 15, 2014 i think for your goal of converting a multi-thousand line code file, writing a function that emulates what mysql_result() does but for the mysqli extension would result in the least amount of pain. include the following function definition into any main file that needs it - function mysqli_result($result , $offset , $field = 0){ $result->data_seek($offset); $row = $result->fetch_array(); return $row[$field]; } usage in the code in the first post in this thread (just change mysql_result(....) to mysqli_result(....)) - $num_items = mysqli_result($result, 0, 'num_items'); Quote Link to comment https://forums.phpfreaks.com/topic/286201-help-updating-mysql_result-from-mysql-to-mysqli/#findComment-1468991 Share on other sites More sharing options...
ginerjm Posted February 15, 2014 Share Posted February 15, 2014 (edited) I'm going to step in here to give you a couple of quick hints in case Jacques1 is offline. In the following lines: $num_items = mysql_result($result, 0, 'num_items'); and $num_comments = mysql_result($comment_result, 0, 'num_comments'); The variable named as the first argument of the MySQL_result call is the variable that was assigned the results of a query. You need to review the code preceding the error lines to check what variable name received the results for that specific query Look for a line such as : $result_var_name = mysqli_query(......); and be sure to match that variable name with the one in the fetch statement to correct those error messages, as below: list($num_comments) = $result_var_name->fetch_row(); Edited February 15, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/286201-help-updating-mysql_result-from-mysql-to-mysqli/#findComment-1468994 Share on other sites More sharing options...
icue Posted February 15, 2014 Author Share Posted February 15, 2014 Thank you all for your help. I will try mac_gyver's suggestion first as this app have many long php files. I will have to attempt it tomorrow as it's the early hours of the morning here. Hope I can get it working but if I can I hope I will be able to get some more help. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/286201-help-updating-mysql_result-from-mysql-to-mysqli/#findComment-1468998 Share on other sites More sharing options...
icue Posted February 17, 2014 Author Share Posted February 17, 2014 Ok, I think I am making some progress. As suggested I put the function supplied by Mac_gyver at the top of the "plog-function.php" file. However when I ran it I get the following error: Fatal error: Call to a member function data_seek() on a non-object in /htdocs/mysite/portfolio/plog-includes/plog-functions.php on line 4 From this function function mysqli_result($result , $offset , $field = 0){ $result->data_seek($offset); $row = $result->fetch_array(); return $row[$field]; } This is the function I am calling mysqli_result() from: function plogger_picture_comment_count() { $row = $GLOBALS['current_picture']; $comment_query = "SELECT COUNT(`id`) AS `num_comments` FROM `".PLOGGER_TABLE_PREFIX."comments` WHERE approved = 1 AND `parent_id`='".$row['id']."'"; $comment_result = run_query($comment_query); $num_comments = mysqli_result($row, 0, 'num_comments'); return $num_comments; } Is this error caused because the example supplied is written the object way and not the procedural way. I did some more searching and found this example function: function soda_mysqli_result($result , $offset , $field = 0){ if (mysqli_num_rows($result) && $offset <= (mysqli_num_rows($result)-1) && $offset >= 0){ mysqli_data_seek($result,$offset); $resrow = mysqli_fetch_row($result); if (isset($resrow[$field])){ return $resrow[$field]; } } return false; } This gave the following error: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, array given in /htdocs/mysite/portfolio/plog-includes/plog-functions.php on line 4 Some bits that were not working before almost work but it is not finding the images in the database. Sorry but I don't know how to correct these errors. Any further assistance would be much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/286201-help-updating-mysql_result-from-mysql-to-mysqli/#findComment-1469309 Share on other sites More sharing options...
icue Posted February 17, 2014 Author Share Posted February 17, 2014 Sorry I just noticed after posting that I had changed the function name to soda_mysqli_result so as not to get confused. Although it does not show in my post I have also changed it in the function example supplied. Quote Link to comment https://forums.phpfreaks.com/topic/286201-help-updating-mysql_result-from-mysql-to-mysqli/#findComment-1469314 Share on other sites More sharing options...
mac_gyver Posted February 17, 2014 Share Posted February 17, 2014 the two errors mean the same thing. you are not passing a mysqli result object to the function. in your first usage of the function i gave, you are not passing the correct variable name as the first parameter to the function. you are passing $row, which is likely a row fetched from some other query and put into $GLOBALS['current_picture']. this is your statement running the query in that specific code - $comment_result = run_query($comment_query); you would call either function using - $comment_result as the first parameter because that is the variable holding the result object (we assume) that the run_query() function returns. Quote Link to comment https://forums.phpfreaks.com/topic/286201-help-updating-mysql_result-from-mysql-to-mysqli/#findComment-1469318 Share on other sites More sharing options...
icue Posted February 18, 2014 Author Share Posted February 18, 2014 Hi, Thank you for responding. I changed it back to the function you gave me and passed $comment_result as the variable and it worked. I have one other error but I will have a go at solving it myself before asking for more help. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/286201-help-updating-mysql_result-from-mysql-to-mysqli/#findComment-1469327 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.