Mysql select single query in PHP

$sql = "SELECT DISTINCT Branch FROM student_main"; $result = mysql_query($sql); $row_num = mysql_num_rows($result); $rows = mysql_fetch_array($result); echo "<select name='Branch'>"; for($i=0;$i<=$row_num-1;$i++){ echo "<option value='".$rows[$i]."'>".$rows[$i]."</option>"; } echo "</select>"; echo "<input type='submit' Value='submit' />"; echo "</form>"; 

I am trying to create a dropdown using the code above for my form. But that does not work. There are 3 different values ​​in the Branch column, but the drop-down list shows only one value (the first) and the next two as empty values.

However, when in echo $ row_num it hits 3.
This means that it extracts three lines, but then why it is not shown in the drop-down list.

If I run the same request in phpmyadmin, it shows the correct answer. ir returns three different Branch values.

+4
source share
7 answers

You should do something like this:

 $sql = "SELECT DISTINCT Branch FROM student_main"; $result = mysql_query($sql); echo "<select name='Branch'>"; while ($row = mysql_fetch_array($result)) { echo "<option value='".$row[0]."'>".$row[0]."</option>"; } echo "</select>"; echo "<input type='submit' Value='submit' />"; echo "</form>"; 
+5
source

You need to complete the request loop using the following:

  $sql = "SELECT DISTINCT Branch FROM student_main"; $result = mysql_query($sql); echo "<select name='Branch'>"; while($rows = mysql_fetch_array($result)){ // should probably use mysql_fetch_assoc() echo "<option value='".$rows['Branch']."'>".$rows['Branch']."</option>"; } echo "</select>"; echo "<input type='submit' Value='submit' />"; echo "</form>"; 
+7
source

you need mysql_fetch_array() for each row. This function returns an associative array for row one only . just include it inside the for loop right above your echo expression.

edit: mysql_fetch_array() actually returns an array (by default) that has indexes with associative indexes and with numbers. You can continue to use it in the same way.

+6
source

mysql_fetch_array returns only the current data set as an array and moves the internal pointer forward. You need to call mysql_fetch_array again to get all the results.

 while ($row = mysql_fetch_array($result)) { echo "<option value='".$row['Branch']."'>".$row['Branch']."</option>"; } 
+3
source

There is a problem in the loop using the while loop:

 while($rows=mysql_fetch_array($result)){ echo "<option value='".$rows[$i]."'>".$rows[$i]."</option>"; } 

try it

+2
source

You really need to learn how to use patterns.
But it seems Stackoverflow is definitely not the place to learn professional ways to develop websites.

get your details first

 $select = $array(); $sql = "SELECT DISTINCT Branch FROM student_main"; $res = mysql_query($sql) or trigger_error(mysql_error().$sql); while($row = mysql_fetch_array($res)) $select = $row[]; 

And then use it in the template

 <form> <select name='Branch'> <? foreach($select as $row): ?> <option value="<?=htmlspecialchars($row['Branch'])?>"> <?=htmlspecialchars($row['Branch'])?> </option> <? endforeach ?> </select> <input type='submit' Value='submit' /> </form> 
+2
source

mysql_fetch_array will only return the first row ...

see here for full details :)

+1
source

Source: https://habr.com/ru/post/1339228/


All Articles