Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Drop-down list using mysql when query not found

Codes here !

Moderators: macek, egami, gesf

Drop-down list using mysql when query not found

Postby azirionphp » Sun Oct 13, 2013 7:13 pm

I built a simple input drop-down list, using <select> which populates from a mysql database.

It works fine, but if the result from the query is not found then the drop-down list just shrinks and doesn't say anything.

I want it to say something like: "Name not found". I've searched everywhere but I can't seem to find the way.

This is my code:
Code: Select all
<?php
   if ( $myquery = $mysqli->prepare("SELECT name, idname FROM db WHERE
name LIKE '%".$name."%'") ) {
      $myquery->execute();
      $myquery->store_result();
      $myquery->bind_result( $nompac, $idpac ) ;
}

<form name="form1" method="post" action="example.php">
   <table >
      <tr>
        <td>Name: </td>
        <td>
           <select name="chosen_name">
           <?php
              while ( $myquery->fetch() ) {
                  echo "<strong><option value=".$idpac.">".$nompac."</option></strong>";       
              }
           ?>
           </select>
        </td>
        <td><input type="submit" name="Submit" value="Go" class="button"/></td>
     </tr>
  </table>
</form>


So I added:
Code: Select all
if ( empty( $myquery ) ) {
    echo "<strong><option value=''>Name not found</option></strong>";
} else {
    while ( $myquery->fetch() ) {
        echo "<strong><option value='".$idpac."'>".$nompac."</option></strong>";       
    }
}


But it just ignores the code and does nothing.

Next I added var_dump($myquery); before the if( empty( $myquery ) ) and nothing happened, so I placed it next to the mysqli prepared query and the output was:
object(mysqli_stmt)#2 (9) { ["affected_rows"]=> int(0) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(0) ["field_count"]=> int(2) ["errno"]=> int(0) ["error"]=> string(0) "" ["sqlstate"]=> string(5) "00000" ["id"]=> int(2) }


Any ideas what am I doing wrong?
azirionphp
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sun Oct 13, 2013 7:00 pm

Re: Drop-down list using mysql when query not found

Postby azirionphp » Thu Oct 17, 2013 7:43 am

Never mind guys....

already have the answer from another forum, this one took soooo long :P
azirionphp
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sun Oct 13, 2013 7:00 pm

Re: Drop-down list using mysql when query not found

Postby johnj » Thu Oct 17, 2013 10:11 pm

you have to do appropriate conditional branching if a record is not found.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.