I want to put a sql query result into an array.
I tried the code below but it shows the 1st record for $count times.
Obviously it’s something wrong at the “$dept[$i]= $row[‘name’];”.
But i have no idea how to fix it.
Somebody help please?
$sql="SELECT name FROM system_dept ORDER BY id";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
$count=mysql_num_rows($result);
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
else
{
$dept = array();
$i=0;
for($i=0;$i<$count;$i++)
{
$dept[$i]= $row['name'];
echo $dept[$i];
}
}
Ok, i tried to use mysqli but it doesnt work.
the web server shows that:
MySQL client version: 4.1.22
PHP extension: mysql
Can mysqli works in mysql php extension?
you are only fetching one row to fetch more rows you’ll need to call fetch result again
so just add this to your code and things should be fine:
I would recommend using a while loop instead like the following:
If at all possible look into mysqli and PDO, as they are both more efficient