-3

I've tried some codes to get a dropdown select from my DB, but the output is just always blank and I don't know why

Or maybe an Autocomplete should be a better solution?

<select id="selectbox" name="selectbox"><br>
<?php <br>
$result = "SELECT * FROM colaboradores"; <br>
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)) {?> <br>
<option value="<?php echo $row['nome'];?>"></option><br>
<?php }?> <br>
</select>

UPDATE

$conexao=mysqli_connect('localhost','root','','bsh'); $result = mysqli_query("SELECT * FROM colaboradores", $conexao);

Nico Haase
  • 9,476
  • 35
  • 35
  • 57
  • 1
    this `` should be `` – Masivuye Cokile Dec 10 '18 at 11:40
  • 1
    you didn't put anything in between the `` - in a HTML select that's where the visual content goes. Maybe you meant to write something like `` - so that the ID is the value to be posted back, and the name is the friendly thing which the user sees. Secondly, unless you missed some code out, it appears you never actually executed your query? – ADyson Dec 10 '18 at 11:41
  • 3
    You select query is not correct, you need to use `mysqli_query()` function – Ayaz Ali Shah Dec 10 '18 at 11:42
  • It still appears all blank :/ – Carlos Santiago Dec 10 '18 at 11:50
  • 1
    Additionally to all other hints, you should check for errors. I don't think that `mysqli_fetch_array` won't complain about the wrong input type for the first parameter – Nico Haase Dec 10 '18 at 12:01
  • 1
    @CarlosSantiago You should [read the manual](http://php.net/manual/en/mysqli.query.php) to check the syntax for mysqli_query. In your case it should be `$result = mysqli_query($conexao, "SELECT * FROM colaboradores");`. The database link value comes first. This will be generating an error in your PHP - you should make sure you have error reporting switched on so you can see it. – ADyson Dec 10 '18 at 12:30
  • 1
    Also another side point: **Never** get your web app to login to the database as root. Root can do whatever it likes, so this just leaves your database an open book for hackers if they find any mistakes in your PHP. Instead create a separate user account specifically for this application which has only the permissions it actually _needs_ in order to work properly. Don't even use the root account as a shortcut during development or testing, because you need to test your account permissions as well - otherwise when you go live you might have unexpected errors relating to the user account setup – ADyson Dec 10 '18 at 12:31
  • Note: The object-oriented interface to `mysqli` is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface. Before you get too invested in the procedural style it’s worth switching over. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. – tadman Dec 10 '18 at 15:30

2 Answers2

4

Two things:

First, the query is not executed, you've only put it in a variable as string. Execute your query like this:

$result = mysqli_query($conn, "SELECT * FROM colaboradores");

Second, options should be filled with what you want to display, e.g.

<option value="<?= $row['nome']; ?>"><?= $row['nome']; ?></option>

Note that if value is not filled, it takes the value of the content, so writing <option><?= $row['nome']; ?></option> is the same.

Jules R
  • 563
  • 2
  • 18
  • Thanks for your awnser, but it stills blank :/ – Carlos Santiago Dec 10 '18 at 11:57
  • @CarlosSantiago can you edit your question with db content ? – Jules R Dec 10 '18 at 11:57
  • 2
    @CarlosSantiago then you need to check that a) your query actually returns any rows, and b) your field name is correct, and c) you don't have any errors. It should be a simple thing to debug. If you learn to do some basic debugging you can fix simple problems like this by yourself. It's almost certainly a simple thing, but because we don't have access to your code, your data, your error log, or indeed your mind, it's hard for us to give specific help. – ADyson Dec 10 '18 at 11:58
  • 1
    @CarlosSantiago `mysqli_query` syntax is `mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )` – Jules R Dec 10 '18 at 12:10
0

You write:

$result = mysqli_query("SELECT * FROM colaboradores", $conexao);

it should be:

$result = mysqli_query($conexao, "SELECT * FROM colaboradores");

then

<select id="selectbox" name="selectbox">
<?php
while($row = mysqli_fetch_assoc($result)) {
     echo "<option value='{$row['nome']}'>{$row['nome']}</option>";
}
?>
</select>