Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi

I am trying to run my search form and getting this error on result page

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /Applications/XAMPP/xamppfiles/htdocs/m/results.php on line 84
No search results found...please search again

My code for result page :

PHP
<?php
// Check that the form has been submitted
if ($_POST) {

    // Store the form data in variables

    $form_series_name = $_POST['series_name'];
    $form_card_player = $_POST['card_player'];
    $form_card_condition = $_POST['card_condition'];
    //$form_card_price = $_POST['card_price'];


    // Check if the radio button data exists
     if (array_key_exists('card_condition', $_POST))
     {
     $form_card_condition = $_POST['card_condition'];
      }
      else
      {
        $form_card_condition = "";
            }

    // Trim any trailing and leading spaces from the form data
    $form_series_name = trim($form_series_name);
    $form_card_player = trim($form_card_player);
    $form_card_condition = trim($form_card_condition);



    // Open a connection to the database
    $link = mysqli_connect(----------my values-------);

    // Define a query that retrieves all fields names and id
    $query = "SELECT cards.card_id, series.series_name, cards.card_player,cards.card_condition
    FROM cards, series
    WHERE cards.cards_id = series.series_id";


    // Restrict the SQL query with an AND clause if a
    // series id has been supplied
    if ($form_series_id != 0)
     { $query .= "AND series.series_id = $form_series_id "; }

    // if player name  has been supplied
    if ($form_card_player != "")
    {
        $query .= " AND cards.card_player= '$form_card_player'";
    }

    // Restrict the SQL query with an AND clause if a contract  status has been supplied
    if ($form_card_condition != "")
    {
         $query .= "AND cards.card_condition = $form_card_condition ";
     }



    //this will check the value put by the user in the lowest price  box by the user
    //if ($form_lowest_price != "")
    //{
        //$query .= " AND card_price >= $form_lowest_price";
    //}

    //this will check the value put by the user in the highest price  box by the user
    //if ($form_highest_price != "")
    //{
        //$query .= " AND card_price <= $form_highest_price"; // this will show the query
    //}

    // Run the query and store the result
    $result = mysqli_query($link, $query);

    $number = mysqli_num_rows($result);

    //echo $query;

    // this will show the whole tablle record from database if the user not select and type anything on query page
    if ($number == 0)
    {
        echo "No search results found...please search again";
    }
    else
    {

    // this contain the header information in the cell of the table
        echo <<<END
        <Table  width='60%' border="1">
         <TR>
            <TH>Card Id</TH>
            <TH>Series</TH>
            <TH>Player </TH>
            <TH>Condition</TH>
            //<TH>Price</TH>

         </TR>
END;
    // Assign each record in the result to an array
    while ($row = mysqli_fetch_array($result))
    {
        // Assign each array element to a variable
        $card_id = $row['card_id'];
        $series_name = $row['series_name'];
        $card_player = $row['card_player'];
        $card_condition = $row['card_condition'];


       if ($card_condition == 1)
        {
            Echo "<TD>Mint</TD>";
        }
        elseif ($card_condition == 2)
        {
            Echo "<TD>Good</TD>";
        }
        elseif ($card_condition == 3)
        {
            Echo "<TD>Very Good</TD>";
        }
        //$card_price = $row['card_price'];


    // this will show  data from database in the table format what we require in assignment
        Echo "<TR >";

        Echo "<TD>$card_id_id</TD>";
        Echo "<TD>$series_name</TD>";
        Echo "<TD>$card_player</TD>";
        Echo "<TD>$card_condition</TD>";

         "</TR>";

    }
    Echo "</TABLE>";
}

// Close the connection to the database
mysqli_close($link);

}

?>


[edit]add proper tag-johny[/edit]
Posted
Updated 15-May-22 8:46am
v4

You should check the result (that is $result) of mysqli_query before passing it to mysqli_num_rows, see the documentation [^].
 
Share this answer
 
I was using wrong SQL query.

SQL
// Define a query that retrieves all fields names and id
    $query = "SELECT cards.card_id, series.series_name, cards.card_player,cards.card_condition
    FROM cards, series
    WHERE cards.cards_id = series.cards_id ";



Thanks for your help guys.
 
Share this answer
 
you didnt select database. besides check res to ensure its working properly, if not then use mysql_error function to get the error message
 
Share this answer
 
How to fix Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in D:\wamp64\www\liveSearch\livesearch.php on line 12


hello I have the same issue with my code and here is my code please help !
PHP
<?php

include("config.php");
if(isset($_POST['input'])){

    $input = $_POST['input']; 

    $query = "SELECT * FROM wheat_genes WHERE Gene_Name LIKE '{$input}%' OR Gene_id LIKE '{$input}%' OR Gene_Annotation '{$input}%' OR Gene_Regulation LIKE '{$input}%'"; 

    $result = mysqli_query($con, $query); 
    
    if(mysqli_num_rows($result)>0){?>
        <table class="table table-bordered table-striped mt-4">
            <thead>
                 <tr>
                    <th>ID</th>
                    <th>Gene_id</th>
                    <th>Gene_Name</th>
                    <th>Gene_Annotation</th>
                    <th>Gene_Regulation</th>
                    <th>CDS</th>
                    <th>Primer_R</th>
                    <th>Primer_F</th>
                    <th>CG%</th>
                    <th>Compelet_seqence</th>
                 </tr>
            </thead>
            <tbody>
                <?php
                while($row=mysqli_fetch_assoc($result)){
                    $id = $row["ID"];
                    $Gene_id= $row["Gene_id"];
                    $Gene_Name = $row["Gene_Name"];
                    $Gene_Annotation = $row["Gene_Annotation"];
                    $Gene_Regulation = $row["Gene_Regulation"];
                    $CDS = $row["CDS"];
                    $Primer_R = $row["Primer_R"];
                    $Primer_F = $row["Primer_F"];
                    $CG = $row["CG%"];
                    $Compelet_seqence = $row["Compelet_seqence"];

                }
                ?>
                <tr>
                    <td><?php echo $ID ;?></td>
                    <td><?php echo $Gene_id ;?></td>
                    <td><?php echo $Gene_Name ;?></td>
                    <td><?php echo $Gene_Annotation ;?></td>
                    <td><?php echo $Gene_Regulation ;?></td>
                    <td><?php echo $CDS ;?></td>
                    <td><?php echo $Primer_R ;?></td>
                    <td><?php echo $Primer_F ;?></td>
                    <td><?php echo $CG ;?></td>
                    <td><?php echo $Compelet_seqence ;?></td>

                
                </tr>

            </tbody>
        </table>
        <?php
    

    }else{
        echo "<h4 class='text-danger text-center mt-3'>No data Found</h4>";
    }
    
};
?>    
 
Share this answer
 
Comments
Richard Deeming 16-May-22 4:47am    
Your question is not a solution to someone else's question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900