Jump to content

[SOLVED] mysql_result NOT a VALID RESOURCE


dsaba

Recommended Posts

I get these 2 errors

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

Warning: mysql_result(): supplied argument is not a valid MySQL result resource

 

I know for a fact that my only query in there where I first define $rec_entries_q IS A VALID RESOURCE AND RETURNS SOMETHING

 

so its in the synatx of mysql_result that I messed up somwhere

 

 

<?php

$rec_entries_q = mysql_query("SELECT * FROM mh_info WHERE entry_status='Active' ORDER BY datetime_modified DESC LIMIT 0, 5") or die(mysql_error());
$max = mysql_num_rows($rec_entries_q);


for ($i=1; $i<=$max; $i++)                                 {
//query for specific row
$rownum = $i - 1;
$rec_entries_q = mysql_result($rec_entries_q, $rownum);
$rec_entries_qrow = mysql_fetch_array($rec_entries_q);


//something wrong with those statements ^^^^^^^^ ??


?>

Link to comment
https://forums.phpfreaks.com/topic/48235-solved-mysql_result-not-a-valid-resource/
Share on other sites

hi,

what is the meaning of this:

 

$rec_entries_q = mysql_result($rec_entries_q, $rownum);

 

You are asigning a value to $rec_entries_q from the cell and it's no longer a resource result. Try giving another name to this variable.

 

that shouldn't matter because I can do this:

$string = 'hello';

$string = str_replace('hello', 'hello world', $string);

echo $string; //it echoes hello world

I only get this error with this new code:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

 

<?php
$rec_entries_q = mysql_query("SELECT * FROM mh_info WHERE entry_status='Active' ORDER BY datetime_modified DESC LIMIT 0, 5") or die(mysql_error());
$max = mysql_num_rows($rec_entries_q);



for ($i=1; $i<=$max; $i++) 								{
//query for specific row
$rownum = $i - 1;
$rec_hello = mysql_result($rec_entries_q, $rownum) or die(mysql_error());
$rec_entries_qrow = mysql_fetch_array($rec_hello)or die(mysql_error());
?>

i know what i'm doing wrong

in php manual:

mysql_query returns a result resource

 

mysql_result returns a string

 

mysql_fetch_array will only take a result resource to make an array out of it

 

the problem is I want to make an associative array (by the field names) out of the result I get from mysql_result (which is a string)

 

How do I do that?

mysql_fetch_array has to receive a result set from query for first paramter, not a string or something else. in your case your result set is

$rec_entries_q, so your call should be:

 

mysql_fetch_array($rec_entries_q)

 

and as you gave an example you shouldn't change $rec_entries_q.

 

just to mention: it is not a good practice to type a query in that way :

 

select * from ....

 

it's better to name the columns you need.

 

if you want to get all the info from your query why don't you try doing this:

 

while ($row = mysql_fetch_array($rec_entries_q)) {
   echo $row[0];
   echo $row[1];
   echo $row[2];
}

 

in that way you don't need to know the number of returned rows.

 

-thanks for the reply BUT

My problem was never about needing to know the number of rows, it was about putting the result source of a particular row into a associative array based on field names

 

I need to use the data from the rows in an associative array, i can't echo out the row as it is, with all the field names, i only use some

 

so this is what i'm doing now I'm so close, just need to fix this syntax:

<?php
for ($i=1; $i<=5; $i++) 								{
//query for specific row
$rownum = $i - 1;
$rec_entries_q = mysql_query("SELECT * FROM mh_info WHERE entry_status='Active' ORDER BY datetime_modified DESC LIMIT $rownum, $rownum") or die(mysql_error());
$rec_entries_qrow = mysql_fetch_array($rec_entries_q)or die(mysql_error());
?>

 

so my LIMIT numbers are variables

the problem is this: LIMIT $rownum, $rownum

is not the correct syntax, it displays nothing

 

this works: LIMIT 5, 5

 

this doesn't: LIMIT '$rownum', '$rownum'

and neither does this: LIMIT $rownum, $rownum

 

so how do I use the variables value with the LIMIT syntax?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.