Reputation: 2166
i have tried $row[0] alone it works, but not $row[1]. It says like this "Notice: Undefined offset: 1 in C:\xampp\htdocs\t2.php on line 10"
<?php
$c=mysql_connect("localhost","root","");
//db connection
mysql_select_db("niro");
$re=mysql_query("select id from detail where grade >=80 ORDER BY RAND()");
//select who are all have above 80
while($x=mysql_fetch_array($re, MYSQL_BOTH))
{
printf ("%s %s",$x[0],$x[1]."\n");
// i have multiple fields but it says undefined offset : 1.
}
?>
Upvotes: 1
Views: 265
Reputation: 204
I guess the reason is you only have one field id in query result event if you may have mutlit field in your table. Because your sql is "select id from detail where grade >=80 ORDER BY RAND()".
You should use "select * from detail where grade >=80 ORDER BY RAND()" to get other fields in your table. And please make sure ORDER BY RAND() is what you want. It will select the data by rand order. If all you want is get all data which grade >= 80, no need to order it by random.
Update: The array index is the filed. If you only want to get and print the first 2 rows, you can use sql like: "select * from detail where grade >=80 limit 2"
Upvotes: 3
Reputation: 8361
You are getting undefined offset because you are selecting only one column in your select query try using select *
or you can mention more column names.
i.e
$re=mysql_query("select * from detail where grade >=80 ORDER BY RAND()");
Upvotes: 2