nofel
November 14, 2011, 8:33am
1
hi all,
i m working my way on a cms and i am getting a error
here is my query
<?php
$position_set = mysql_query("SELECT * FROM subjects");
if(!$position_set){
die ("Cannot process because" . mysql_error());
}else{
echo $position_set;
}
$subject_row = mysql_num_rows($subject_set);
for ($count = 1;$count<= $subject_row;$count++){
"<option value=\\"{$count}\\">{$count}</option>";
}
?>
when i try to echo it out, it gives me Resource id # 14. why does it give this?
Immerse
November 14, 2011, 8:42am
2
$position_set is a mysql query result, it’s what is called a resource in PHP lingo.
What you need to do is to fetch the values from the query result;
$position_set_query = mysql_query("SELECT * FROM subjects");
$position_set = mysql_fetch_assoc($position_set_query);
Now $position_set will contain an array with the values from the first recordset row your query returned.
See your question in post #7 in this thread: http://www.sitepoint.com/forums/showthread.php?797303-fetch-array-error
It’s the same question. And it has been answered.
nofel
November 14, 2011, 9:26am
4
is my loop right? coz its crashing the browser again n again?
Is the result set called $position_set or $subject_set?
Are you sure? Look at the code you posted. How is the result set called in the first line?
nofel
November 14, 2011, 9:39am
8
here is the code, yes it was different above
<?php
$position_set = mysql_query("SELECT * FROM subjects");
$subject_set= mysql_fetch_assoc($position_set);
for($count = 1; $count<=$subject_set++; $count++){
echo "<option value=\\"\\">{$count}</option>";
}
}
?>
Just take a good look at that code. Explain to yourself what each single line is doing.
I’m sure you’ll see the error.
nofel
November 14, 2011, 11:23am
10
ok, well i made it.but i am left with infinite loop where as the variable that is being passed into for loop has only 3 entries. i checked the mysql_query and its working fine (meaning printing the right data) but why is my loop going infinite?
<?php
$position_set = mysql_query("SELECT * FROM subjects");
$subject_row = mysql_fetch_assoc($position_set);
//print_r($subject_row);exit;
{
for($count = 1; $count<=$subject_row; $count++){
echo "<option value=\\"\\">{$count}</option>";
}
}
?>
SpikeZ
November 14, 2011, 11:26am
11
does mysql_fetch_assoc return an integer that can be compared too?
If it doesnt then it will have nothing to compare $count to so it will never stop…
nofel
November 14, 2011, 11:30am
12
I guess no. so i used mysql_num_rows. and it solved the problem!
thanks