Jump to content

Help updating mysql_result from mysql to mysqli


icue

Recommended Posts

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.
Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 2103
Fatal 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

Link to comment
Share on other sites

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');
Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.