0

EDIT: SOLVED. Thanks to the guys in the comments who recommended turning PHP error messages on, showed me exactly where things were going wrong. For those that are curious, it was as simple as this: two function calls had "mysql_" instead of "mysqli_". Also thanks to the user that pointed out that prepared SQL queries must use ->execute() instead of mysqli_query().

I'm working on a community bulletin board. I've gotten it to a point where the page for posting a listing works and submits it to the database, but when I try to search for the listings I'm having some trouble with the SQL query. Here's the code that I have:

<?php
if(isset($_GET['submit'])){
    $location = $_GET['location'];
    echo "Looking for listings in: " . $location;
    echo "<br />";
    require_once('mysqli_connect.php');

    $query = "
SELECT email_address
     , phone_number
     , date
     , listing_content 
  FROM listings 
 WHERE location='?'
 ";
    echo $query;
    $stmt = mysqli_prepare($dbc, $query);
    mysqli_stmt_bind_param($stmt, "s", $location);

    $results = mysqli_query($stmt);

    $numListings = mysql_num_rows($results);
    echo $numListings . " listing(s) found";
    echo "<hr>";
    if ($numListings == 0){
      echo "Feel free to press the button above to post the first listing in " . 
$location;
    } else {
      while($row = mysql_fetch_array($results)){
        echo "Location: " . $location;
        echo "<br />";
        echo "Date: " . $row[date];
        echo "<br />";
        echo "Email address: " . $row[email_address];
        echo "<br />";
        if($row[phone_number] != NULL){
          echo "Phone number: " . $row[phone_number];
          echo "<br />";
        }
        echo $row[listing_content];
        echo "<br />";
        echo "<hr>";
      }
    }
    mysqli_stmt_close($stmt);
    mysqli_close($dbc);
}
?>

And this is the output I'm getting, where everything below the button "Make new post" is generated by the PHP:

Screenshot of browser window

It looks like it's crashing, at some point after the line echo $query; because it prints that out but it doesn't print some of the other statements after that. Either it's crashing or the SQL query is somehow messing up without crashing it or throwing an error.

This is what the database looks like:

Screenshot of management panel

psychgaz
  • 1
  • 1
  • 1
    http://php.net/manual/en/mysqli.prepare.php You have to `$stmt->execute()` a prepared query `mysqli_query($stmt);` wont work. – RiggsFolly Feb 24 '18 at 22:41
  • 2
    Also, turn on PHP errors while developing, https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Geoffrey Feb 24 '18 at 22:42
  • 2
    Add `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` to the top of your script. This will force any `mysqli_` errors to generate an Exception that you can see on the browser and other errors will also be visible on your browser. – RiggsFolly Feb 24 '18 at 22:43
  • '?' is a string, the contents of which is a question mark – Strawberry Feb 25 '18 at 00:06
  • Thanks for the advice on displaying errors. Once again, I'm very new to PHP and I'm basically teaching it to myself by doing this project. I've done so and will play around with it and update my post if I have more info, keep an eye out, please. Thanks again. – psychgaz Feb 25 '18 at 01:20
  • Everything is in working order. Thanks guys! Check out the edit on the original post if you were curious as to what the issue was :) – psychgaz Feb 25 '18 at 01:53

0 Answers0