I want to create a select list according to the mySQL database, but I get empty results when using mysqli_fetch_array. The options number is correct, but all empty.
Here's the code:
$rows = mysqli_num_rows($result);
$self = $_SERVER['PHP_SELF'];
<div id='editUser'>
<span class='ACtitle'>Edit Users</span>
<div id='addField'>
<form action="<?php $self ?>" method="post">
<select name="target" size="1">
<option value='none'>Select user you wish to edit.</option>
<?php
for($ii = 0; $ii <= $rows; $ii++)
{
$sqlSta3 = "select name from 'users'";
$result2 = mysqli_query($database, $sqlSta3);
$nameArray = mysqli_fetch_array($result2);
print_r($nameArray);
$tableName = $nameArray['name'];
print "<option value='$tableName'> $tableName </option>";
}
?>
</select>
<input type="text" name="updateName"
</form>
</div>
</div>
You need to replace the mysql_fetch_array
with mysql_fetch_assoc
to use your method. mysql_fetch_array
returns array like this [0] => "data",[1] => "data"
mysql_fetch_assoc
returns array with the name of the cells in your table ['username'] => 'data',['fname'] => "data";
Here is the fixed code:
$rows = mysqli_num_rows($result);
$self = $_SERVER['PHP_SELF'];
<div id='editUser'>
<span class='ACtitle'>Edit Users</span>
<div id='addField'>
<form action="<?php $self ?>" method="post">
<select name="target" size="1">
<option value='none'>Select user you wish to edit.</option>
<?php
for($ii = 0; $ii <= $rows; $ii++)
{
$sqlSta3 = "SELECT name From users ";
$result2 = mysqli_query($database, $sqlSta3);
$nameArray = mysqli_fetch_assoc($result2);
print_r($nameArray);
$tableName = $nameArray['name'];
print "<option value='$tableName'> $tableName </option>";
}
?>
</select>
<input type="text" name="updateName"
</form>
</div>
</div>