Legendary_003 Posted September 11 Share Posted September 11 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 Quote Link to comment https://forums.phpfreaks.com/topic/323952-php-for-loop-with-differents-ids/ Share on other sites More sharing options...
Barand Posted September 11 Share Posted September 11 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>"; 1 Quote Link to comment https://forums.phpfreaks.com/topic/323952-php-for-loop-with-differents-ids/#findComment-1635027 Share on other sites More sharing options...
Psycho Posted September 11 Share Posted September 11 @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>"; Quote Link to comment https://forums.phpfreaks.com/topic/323952-php-for-loop-with-differents-ids/#findComment-1635051 Share on other sites More sharing options...
Barand Posted September 11 Share Posted September 11 @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 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/323952-php-for-loop-with-differents-ids/#findComment-1635055 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.