dsaba Posted April 23, 2007 Share Posted April 23, 2007 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 More sharing options...
bache Posted April 23, 2007 Share Posted April 23, 2007 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. Link to comment https://forums.phpfreaks.com/topic/48235-solved-mysql_result-not-a-valid-resource/#findComment-235792 Share on other sites More sharing options...
dsaba Posted April 23, 2007 Author Share Posted April 23, 2007 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 Link to comment https://forums.phpfreaks.com/topic/48235-solved-mysql_result-not-a-valid-resource/#findComment-235795 Share on other sites More sharing options...
dsaba Posted April 23, 2007 Author Share Posted April 23, 2007 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()); ?> Link to comment https://forums.phpfreaks.com/topic/48235-solved-mysql_result-not-a-valid-resource/#findComment-235800 Share on other sites More sharing options...
fert Posted April 23, 2007 Share Posted April 23, 2007 try changing $rec_hello = mysql_result($rec_entries_q, $rownum) or die(mysql_error()); $rec_entries_qrow = mysql_fetch_array($rec_hello)or die(mysql_error()); to $rec_entries_qrow=mysql_result($rec_entries_q, $rownum) or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/48235-solved-mysql_result-not-a-valid-resource/#findComment-235801 Share on other sites More sharing options...
dsaba Posted April 23, 2007 Author Share Posted April 23, 2007 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? Link to comment https://forums.phpfreaks.com/topic/48235-solved-mysql_result-not-a-valid-resource/#findComment-235812 Share on other sites More sharing options...
dsaba Posted April 23, 2007 Author Share Posted April 23, 2007 yes I can query for a particular row SELECT * FROM `table` LIMIT 5,5 there now this fixes my problem Link to comment https://forums.phpfreaks.com/topic/48235-solved-mysql_result-not-a-valid-resource/#findComment-235814 Share on other sites More sharing options...
bache Posted April 23, 2007 Share Posted April 23, 2007 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. Link to comment https://forums.phpfreaks.com/topic/48235-solved-mysql_result-not-a-valid-resource/#findComment-235818 Share on other sites More sharing options...
dsaba Posted April 23, 2007 Author Share Posted April 23, 2007 -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? Link to comment https://forums.phpfreaks.com/topic/48235-solved-mysql_result-not-a-valid-resource/#findComment-235822 Share on other sites More sharing options...
dsaba Posted April 23, 2007 Author Share Posted April 23, 2007 solved thanks for all the help everyone! Link to comment https://forums.phpfreaks.com/topic/48235-solved-mysql_result-not-a-valid-resource/#findComment-235834 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.