php - Dropdown list is empty

100

My dropdown list shows empty. I am not sure if my query returns any records. How can I find out if my query returned any result? Here is my code. Please let me know. I am stuck with this issue.

 <?php $link = mysql_connect("localhost", "root", "root"); 
            if (!$link) {     die('Could not connect: ' . mysql_error()); } 
            echo 'Connected successfully'; 

                mysql_select_db("mydb");  
                $sql = "SELECT state_id, state_code FROM states"; 
                $result = mysql_query($sql) or die(mysql_error());
                ?>

 <p>  
<select name="vers">  
 <?  

 while($row = mysql_fetch_array($result)){ 
   echo "<option value=\"".$row['state_id']."\">".$row["state_code"]."</option>";   
 }  
 ?>  
 </select>

<?
            mysql_close($link); 
            ?> 
427

Answer

Solution:

Test the success or failure of your query withif (!$result) and usemysql_num_rows() to verify the number of rows returned. Rather thandie(), you can display a better error to your user and possibly render different HTML, rather than just exit the script abruptly.

  mysql_select_db("mydb");  
  $sql = "SELECT state_id, state_code FROM states"; 
  $result = mysql_query($sql);

  // Instead of die(), use a construct like this for better error handling on your page
  if (!$result) {
    // query error
  }     

  if (mysql_num_rows($result) === 0) {
    // No rows returned
  }
  else {
    // Success! Populate your <select> with <option>s
    while($row = mysql_fetch_array($result)){ 
     echo "<option value=\"".$row['state_id']."\">".$row["state_code"]."</option>";   
    }
  }

UPDATE

Add an empty default option. Make sure itsvalue attribute is not something you would retrieve from your database.

<select name="vers"> 
  <option value='-1'>Choose an option...</option>
696

Answer

Solution:

Try running the query directly against the database using a tool such as MySql Workbench

SELECT state_id, state_code FROM states

I suspect there are no results.

UPDATE: Given that 50 results are expected... can you try this version with minor updates and report back on any warnings or errors you can see.

<?php
use error_reporting(E_ALL | E_STRICT); // Development use only!

$link = mysql_connect("localhost:3306", "root", "root") or die ('Error connecting to mysql' . mysql_error());

mysql_select_db("mydb");  

$sql = "SELECT state_id, state_code FROM states;"; 
$result = mysql_query($sql) or die(mysql_error());
?>
<p>  
    <select name="vers">  
    <?php
    while($row = mysql_fetch_array($result)) { 
        echo '<option value="' . $row['state_id'] . '">' . $row['state_code']  . '</option>';
    }  
    ?>  
    </select>
</p>
<?php
mysql_close($link); 
?> 
68

Answer

Solution:

How can I find out if my query returned any result?

Run your query on your Database Server instead of through PHP and see if it returns any results. If you use shared hosting with MySQL, many hosts provide a phpmyadmin interface that lets you run SQL commands.

132

Answer

Solution:

you can use mysql_num_rows to find out how many elements you have in your$result

993

Answer

Solution:

At first, you should check your table "states". If it is empty, your code is correct. For example, you can use PhpMyAdmin - http://www.phpmyadmin.net. Execute your query with phpMyAdmin

People are also looking for solutions to the problem: php - Drupal changing links

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.