Jump to content

PHP For loop with differents IDs


Legendary_003

Recommended Posts

Hello people, everything all right ??

I am with the following doubt, hope you can help me !!

In my "mysql" database I have a list with ID = 1, 3 and 4 and I would like to list them in
"html<select><option></option></select>" !!

I can´t list them like:

 

$sql_list = "SELECT * FROM dados_socios WHERE usuario = '$usuario' ORDER BY ID";
$result_list = $conn->query($sql_list);
	echo "<select>";
	for($x = 0; $x <= $result_list->num_rows; $x++) {
	echo "<option>ID: $x</option>";
	}
	echo "</select>";

Because if I list this way would show:
ID: 1
ID: 2
ID: 3

And I would like the original:
ID: 1
ID: 3
ID: 4

How Can I do that ??
Waiting answer and thanks for the attention !!
One hug

Link to comment
Share on other sites

Don't use "select *". Specify the columns you need.

Then use a foreach loop, not a for loop.

Don't embed variables in the sql string, use prepared queries with placeholders, then execute with the variables passed as parameters.

$sql_list = "SELECT ID FROM dados_socios WHERE usuario = ? ORDER BY ID";
$result_list = $conn->prepare($sql_list);
$result_list->execute([$usario]);

echo "<select>";
  foreach {$result_list as $row)
    echo "<option>ID: {$row['ID']}</option>";
  }
 echo "</select>";

 

  • Like 1
Link to comment
Share on other sites

@Barand completely agree with your response. But, unless I am missing something, I think there is a step missing in your example code. Shouldn't there be a step to fetch the results? I think $result_list would be a PDO object and not an array of the results. And there is a missing opening curly brace for the foreach() statement - although I consider that a minor typo that I would expect the OP to identify/fix.

$sql_list = "SELECT ID FROM dados_socios WHERE usuario = ? ORDER BY ID";
$result_query = $conn->prepare($sql_list);
$result_query->execute([$usario]); 
$result_ary = $result_query->fetchAll();

echo "<select>";
  foreach {$result_ary as $row)
  {
    echo "<option>ID: {$row['ID']}</option>";
  }
echo "</select>";

 

Link to comment
Share on other sites

@Psycho the PDO result/statement object is traversable whether you use query() or execute().

The mysqli result object is traversable (when the result of a query) but the statement object as a result of execute() is not (as well as having a different set of methods to process).

This adds to my theory that mysqli result class and statement class were developed by two teams who never spoke to one another.

EG

$res = $pdo->query("SELECT name FROM reindeer");
foreach ($res as $r) {
    echo $r['name'] . "<br>";
}

results...

Comet
Cupid
Dasher
Vixen
Prancer
Dancer
Donner
Blitzen

 

  • Like 1
  • Haha 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.