简体   繁体   中英

How to echo MySQL database has not returned anything?

My problem is that if a user types in a value that is not in my database the page stays blank. I wish to display a message saying their postcode is not valid. I have had a look through other posts and can't seem to get it to work with my code.

The user searches in a text box to find out the 'price' of delivery for their postcode ie they search m6 and it returns m6 - 0.50 in a table. If say they typed sk1, which is not part of the delivery area I want to display a message

What I have done is as follows:

index.php

<section id="post">
<p id="post"> postage calculator for manchester: </P>

    <?PHP
session_start();
     echo 'total cost £'. $_SESSION['postage'];
              ?>

    <form action="shop.php" method="post">
    <input class="text" type="text" name="postsub" min="0">
    <input class="button" type="submit" name="postsub1" value="postsub" >
    </form>
    </section>

process.php:

function button6() {

 $search_value = $_POST["postsub"];
 $codequery = "SELECT * FROM postage WHERE postcode LIKE '%$search_value%'";

 // connect to database
     $dbh= new PDO('mysql:host='.$hostname.';dbname='.$dbname,$username,$password);

  //generate SQL query
 $result=$dbh->prepare($codequery);

 //execute query
 $result->execute();

// display result
echo '<table id="tab">';
while($row=$result->fetch()){
echo '<tr>';
echo '<td>'.$row['postcode'].'</td>';
echo '<td>'.$row['price'].'</td>';
echo '</tr>';

 if (mysqli_num_rows($result)==0) { echo "no result"; }

  }
  echo '</table>';
 }

This is how it returns on a positive search and I just want the postode and price, and then when it is not any of those just display no result:

no resultno resultno resultno resultno resultno resultno result
m1  0.50
m2  0.50
m3  0.70
m4  0.70
m5  1
m6  1
m7  1.20

Your function has plenty of problems. A simple fix would be to remove the invalid mysqli_num_rows() and the if statement and parameterize the SQL. You should also connect once outside the function and then pass the connection into the function.

The best choice usually is to fetch all rows into an array and then have an if statement based on that data. If you have no values in $data then display some message.

function button6(PDO $dbh) {
    $search_value = $_POST["postsub"];
    $codequery = "SELECT * FROM postage WHERE postcode LIKE ?";
   
    //generate SQL query
    $result = $dbh->prepare($codequery);
   
    //execute query
    $result->execute(["%$search_value%"]);
    $data = $result->fetchAll(PDO::FETCH_ASSOC);
   
    if ($data) {
        // display result
        echo '<table id="tab">';
        foreach ($data as $row) {
            echo '<tr>';
            echo '<td>'.$row['postcode'].'</td>';
            echo '<td>'.$row['price'].'</td>';
            echo '</tr>';
        }
        echo '</table>';
    } else {
        echo 'No results!';
    }
}

To connect with PDO simply have this piece of code in a single place in your application.


$pdo = new \PDO("mysql:host=localhost;dbname=$dbname;charset=utf8mb4", $username,$password, [
    \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
    \PDO::ATTR_EMULATE_PREPARES => false
]);

Then when you call the function simply pass it as an argument.

button6($pdo);

Try this:

<?php
function button6() {

$search_value = $_POST["postsub"];
$codequery = "SELECT * FROM postage WHERE postcode LIKE '%$search_value%'";

// connect to database
    $dbh= new PDO('mysql:host='.$hostname.';dbname='.$dbname,$username,$password);

 //generate SQL query
$result=$dbh->prepare($codequery);

//execute query
$result->execute();

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo '<tr>';
        echo '<td>'.$row['postcode'].'</td>';
        echo '<td>'.$row['price'].'</td>';
        echo '</tr>';    
    }
} else {
    echo "No results";
}  

} 

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM