How to get the array objects in the selector?

Hello! This question: deduce data from DB in an array, then I want some data from the array to display the selector form.
Code like this:
<?php $wwwlist = mysqli_query ($connection, "SELECT * FROM $dbtable WHERE userid = '$user' ");
?>
 <form method="post">
<span>Website</span>
<select>
 <?php while($data = mysqli_fetch_array($wwwlist)) { 
 echo "<option-->. $data ['name']";
}
?>
but this code does not work - the selector is empty, no item. How to properly display the objects in this array in the selector?</select></form>
July 2nd 19 at 17:00
1 answer
July 2nd 19 at 17:02
Solution
echo "<option>{$data['name']}</option>";
well, or
echo '<option>'.$data['name'].'</option>';

UPD: although this error is not an empty selector would as to any garbage in the selector... probably from the base of an empty result comes...

what do you have in the variable $dbtable and $user ?
in short, make
echo "SELECT * FROM $dbtable WHERE userid = '$user' ";

before query to DB, and see what's in there
Now the code is like this -
$user = 'u1';
$dbtable = 'vhosts_kp';
$list = mysqli_query ($connection, "SELECT name FROM $dbtable WHERE userid = '$user' ");
$listarr = mysqli_fetch_array ($list);
<select>
 <?php foreach ( $listarr as $key =--> $value ) { 
 if ($key == 'name') {
 echo "<option>$value</option>";
}
}
?>
 </select>

But it outputs 1 site twice, and the array itself is also strange
print_r ($listarr);
outputs this: Array ( [0] => tun.ru [name] => tun.ru ) - Glennie_Beie commented on July 2nd 19 at 17:05
Here is the result of executing the same query in phpmyadmin:
95ef82e58c984b79b5a219b16730ba93.jpg - Glennie_Beie commented on July 2nd 19 at 17:08
at first it was almost right, but right now quite a mess :)
every mysqli_fetch_array gives one row in the form array ('name' => 'contents', [0] => 'same content'), same twice - so by name and by ordinal could be a value of a field to find.
accordingly, one fetch - one selector, and display it $data['name']. - kimberly43 commented on July 2nd 19 at 17:11
... or $listarr['name'], how do you rename the new version - kimberly43 commented on July 2nd 19 at 17:14
<?php while($data = mysqli_fetch_array($listarr)) { 
 echo "<option-->{$data['name']}";
}
 ?>

not working - Glennie_Beie commented on July 2nd 19 at 17:17
the phrase "Not working" too many values :)
That outputs something?

Ctrl-U (or whatever it is in your browser page source to), copy all the contents of the select with all options, pastim here. - kimberly43 commented on July 2nd 19 at 17:20
<b>My sites</b><span>Website</span><select>
 </select><table border="1"><thead><tr><th>Website</th><th>admin Email</th><th>Index files</th><th>Aliases</th><th>Encoding domain</th></tr></thead><tbody><tr><td>admin</td><td><textarea name="data[3][email]">dd@dd.ru</textarea></td><td> <textarea name="data[3][indexfiles]">index.php index.html</textarea></td><td> <textarea name="data[3][alias]">ServerAlias www</textarea></td><td> <textarea name="data[3][charset]">UTF-8</textarea></td></tr><tr><td>ferrum.ru</td><td><textarea name="data[4][email]">zirc@a.ru</textarea></td><td> <textarea name="data[4][indexfiles]">index.php index.html</textarea></td><td> <textarea name="data[4][alias]">ServerAlias argentum.ru</textarea></td><td> <textarea name="data[4][charset]">UTF-8</textarea></td></tr><tr><td>arg.ru</td><td><textarea name="data[5][email]">admin@rovel.ru</textarea></td><td> <textarea name="data[5][indexfiles]">index.php index.html</textarea></td><td> <textarea name="data[5][alias]">ServerAlias zinc.ru</textarea></td><td> <textarea name="data[5][charset]">UTF-8</textarea></td></tr><tr><td>arg.ru</td><td><textarea name="data[6][email]">admin@rovel.ru</textarea></td><td> <textarea name="data[6][indexfiles]">index.php index.html</textarea></td><td> <textarea name="data[6][alias]">ServerAlias zinc.ru</textarea></td><td> <textarea name="data[6][charset]">UTF-8</textarea></td></tr><tr><td>gogol.ru</td><td><textarea name="data[9][email]">email@mail.ru</textarea></td><td> <textarea name="data[9][indexfiles]">index.php index.html</textarea></td><td> <textarea name="data[9][alias]">ServerAlias google.ru</textarea></td><td> <textarea name="data[9][charset]">UTF-8</textarea></td></tr>

 <form method="post"></form>





</tbody></table>
- Glennie_Beie commented on July 2nd 19 at 17:23
<?php
ini_set('error_reporting', E_ALL);
$user = 'u1';
$dbtable = 'vhosts_kp';
$connection = mysqli_connect('localhost', '***', '***, '***');
$wwwlist = mysqli_query ($connection, "SELECT * FROM $dbtable WHERE userid = '$user' ");
$list = mysqli_query ($connection, "SELECT name FROM $dbtable WHERE userid = '$user' ");
$data = mysqli_fetch_array($wwwlist);
$listarr = mysqli_fetch_array ($list);
$list = mysqli_query ($connection, "SELECT name FROM $dbtable WHERE userid = '$user' ") or die('Something wrong!');
 echo '<b-->My sites';
 // print the page header HTML table
 echo ";
 echo ";
 echo ";
 echo ";
echo ";
echo ";
echo ";
echo ";
 echo ";
 echo ";
 echo ";
 // print in an HTML table all data of clients from table MySQL 
 while($data = mysqli_fetch_array($wwwlist)){ 
 echo ";
 echo ";
 echo ";
 echo ";
 echo ";
 echo ";
 echo ";
}
?>
<span>Website</span><select>
 <?php while($data = mysqli_fetch_array($listarr)) { 
 echo "<option-->{$data['name']}";
}
?>
 </select><table border="1"><thead><tr><th>Website</th><th>admin Email</th><th>Index files</th><th>Aliases</th><th>Encoding domain</th></tr></thead><tbody><tr><td>' . $data['name'] . '</td><td><textarea name="data[' . $data ['unid'] . '][email]">' . $data ['email'] . '</textarea></td><td> <textarea name="data[' . $data ['unid'] . '][indexfiles]">' . $data ['indexfiles'] . '</textarea></td><td> <textarea name="data[' . $data ['unid'] . '][alias]">' . $data ['alias'] . '</textarea></td><td> <textarea name="data[' . $data ['unid'] . '][charset]">' . $data ['charset'] . '</textarea></td></tr>

 <form method="post"></form>





</tbody></table>
- Glennie_Beie commented on July 2nd 19 at 17:26
oymama... what a mess... :)))
not here to pick one and call it a mistake - that's impossible, sorry... then you just need to understand how it works.

the procedure is this:
- $list = mysqli_query(...) - here we get the result of the query (it is still unusable output)
- $one_record = mysqli_fetch_array($list) - so we get one (next) row from a result as an array; the second call - the second line, etc.;
- $one_record['field_name'] - so we get the field value from string, here it can be;

if you need the same result to scroll through twice - don't forget to "rewind" the pointer to the query result at the beginning (otherwise we'll hit the end, and every attempt to get something from the result would give "anything").
- mysqli_data_seek($list) - kimberly43 commented on July 2nd 19 at 17:29
FIX:
- mysqli_data_seek($list, 0) - kimberly43 commented on July 2nd 19 at 17:32
Here is the PHP code
<?php
$user = 'u1';
$dbtable = 'vhosts_kp';
$connection = mysqli_connect('localhost', 'xxx', 'xxx', 'xxx');
$list = mysqli_query ($connection, "SELECT name FROM $dbtable WHERE userid = '$user' ");
$listarr = mysqli_fetch_array ($list);
?>


 <form method="post">
<span>Website</span>

<select>
 <?php while($data = mysqli_fetch_array($listarr)) { 
 echo "<option-->{$data['name']}";
}
?>
</select>
</form>


Here is the HTML:


 <form method="post">
<span>Website</span>

<select>
</select>
</form>

- Glennie_Beie commented on July 2nd 19 at 17:35
but, better now -)) - kimberly43 commented on July 2nd 19 at 17:38
Ah, well so fucking fetchit need the sheet, not listarr - kimberly43 commented on July 2nd 19 at 17:41
Thank you. Earned
<?php
$user = 'u1';
$dbtable = 'vhosts_kp';
$connection = mysqli_connect('localhost', '***', '***', '***');
$list = mysqli_query ($connection, "SELECT name FROM $dbtable WHERE userid = '$user' ");
$listarr = mysqli_fetch_array ($list);
?>


 <form method="post">
<span>Website</span>

<select>
 <?php while($data = mysqli_fetch_array($list)) { 
 echo "<option value = '{$data['name']}'-->{$data['name']}";
}
?>
</select>
</form>

- Glennie_Beie commented on July 2nd 19 at 17:44

Find more questions by tags PHP