PHP: 'While' loop skipping 'echo' when 'if' statement returns false

I’m working on my first project after going through the PHP course.

I am trying to get data output with a header date and below it events that occur on that date. I can only get one event output for each header date. I thought if the ‘if’ statement returned false it would go on to the ‘echo’ statement, but it instead loops again.

Here is the code (sorry it’s long, it includes everything):

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "venue";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = 'SELECT event.title, event.artists, event.opening_date, event.opening_time, place.site, place.addr, place.hours, place.web
FROM event
INNER JOIN place
ON event.place = place.site
ORDER BY opening_num ASC';


//Initially set date to 0 to get first opening_date to echo.
$testDate = " ";

$result = $conn->query($sql);

if ($result->num_rows > 0) {


    // data of each row
    while($row = $result->fetch_assoc()) {

      //output for each new date
      if ($row["opening_date"] != $testDate) {
        echo $row["opening_date"] . "<br>";

        $testDate = $row["opening_date"];
      }

        //Echo for each row, including "opening_date" == $testDate, hopefully.
        echo $row["site"] . "<br>" .
        $row["addr"] . "<br>" .
        $row["hours"] . "<br>" .

        $row["title"] . "<br>" .
        $row["artists"] . "<br>" .
        "Opening: " . $row["opening_date"] . ", " . $row["opening_time"] . "<br>" . "<br>";

    }
}
  else {
    echo "0 results";
}
$conn->close();
?>

Hope I’m asking the question properly and thanks for any help you can give!!
Aaron

Should this be,

while($row == $result->fetch_assoc()) {

?

That looks like it should work, but it gives me a “Notice: Undefined variable: row in /Applications/MAMP/htdocs/Sites/ … on line 30”.

Not able to test but it looks like $row is not defined. Can you write just this?

while ($result->fetch_assoc()) {

I’m only taking a stab at this since I have never worked with mySQL before. Sorry if I’m not of any help.

Hmmm… it does look like $row is undefined, but I think it gets passed to slq and defined as each row in the database. That’s how new I am, not only can’t I explain it well, I only partially understand it.

When I make your changes it goes through the loop, but says all the variables are undefined each time.

@ajaxpro18466,
First of all make sure you are getting a Result
http://stackoverflow.com/questions/19201929/php-echo-result-of-mysql-select-query

Then in your code

//Initially set date to 0 to get first opening_date to echo.

and you then use testDate= " "; which is a =string=


What is the result of an
echo $result
or
are you able to do a
var_dump($results);

##================================================
http://stackoverflow.com/questions/34228090/cannot-query-mysql-database-via-php
###===============================================

And you made sure that your SQL-query
is returning a result
if done =interactive= on your DataBase

SELECT event.title, event.artists, event.opening_date,event.opening_time, 
       place.site, place.addr, place.hours, place.web
FROM event
INNER JOIN place
ON event.place = place.site
ORDER BY opening_num ASC;
1 Like

I am getting results the way it is posted above. The only result I’m missing is when I have two events on the same date. I want:

May 1, 2016
event 1
event 2
June 1, 2016
event 3
July 1, 2016
event 4

Instead I get:

May 1, 2016
event 1
June 1, 2016
event 3
July 1, 2016
event 4

Note missing event 2 after “May 1, 2016”.
So when the if statement— $row["opening_date"] != $testDate —comes back as false, it not only ignores the first echo— echo $row["opening_date"] —that would echo the date, but also ignores the second echo— ( echo $row["site"] …) —that prints out the second event that occurs on that day. At least that’s the way it looks.

@ajaxpro18466,
Could you provide the RAW $row
with an

echo $row 

to be able to analyze the Data…??

This part of my code was working after all. There is just a bug in one table row.
Thanks everyone.

2 Likes