rvdveen27 Posted July 8, 2015 Share Posted July 8, 2015 Hello all On a page where I have: <?php echo $rows[0]['date']; ?> This code suddenly stopped working. It worked perfectly fine before, what I did was empty the table in the database and create a new entry (since the other entry was for testing purposes) and since I did that I now get errors on the page that say: Notice: Undefined offset: 0 in /home/emerald/domains/emeraldimmersiontrucking.com/public_html/convoy.php on line 97 Now it's true that I don't full understand the meaning of "[0]" in between $rows and ['date']. I tried changing it to [1] but that also didn't help. Here is my current code: $query = " SELECT u.username ,cv.date as 'date' ,cv.comment as 'comment' ,cv.id as 'convoyid' FROM convoy cv, convoysignup cvsu INNER JOIN users u ON u.id = cvsu.username "; try { $stmt = $db->prepare($query); $result = $stmt->execute(); } catch(PDOException $ex) { die("Failed to run query: " . $ex->getMessage()); } $rows = $stmt->fetchAll(); $count = $stmt->rowcount(); ?> <br> <br> <div class="container" style="width:550px;"> <center> <h2>The next convoy will be at:</h2><br> <div class="panel panel-default"> <div class="panel-heading"> <font size="5"><?php echo $rows[0]['date']; ?> GMT +1</font><br> </div> <div class="panel-body"> <font size="3"><?php echo $rows[0]['comment']; ?></font><br> <br> Please read the <a href="inforules.php">Information & Rules</a> page for the convoy rules before departure.<br> </div> </div> </center> It would be highly appreciated if someone could explain me what the [0] actually does and/or what I'm doing wrong in here. Many thanks in advance. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted July 8, 2015 Share Posted July 8, 2015 0 should not be the problem unless you got no results from your query. Test the query result for true and try echoing out the row count value to see what you have. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted July 8, 2015 Share Posted July 8, 2015 As ginerjm implied, $stmt->fetchAll() returns an array containing the result set...if there are any results. Otherwise, it returns false. More information can be found here: http://php.net/manual/en/pdostatement.fetchall.php Quote Link to comment Share on other sites More sharing options...
rvdveen27 Posted July 8, 2015 Author Share Posted July 8, 2015 0 should not be the problem unless you got no results from your query. Test the query result for true and try echoing out the row count value to see what you have. Thank you. This helped me find out the reason behind the error. I cleaned out the convoysignup table so it has no entries. The query checks for entries but there are none. It checks for the signups regarding that specific convoy for later down the page, where the signups are printed in a table Code: <h1>Sign ups</h1><br> <div class="table-responsive"> <table class="table table-striped"> <thead> <tr> <th width="8%">#</th> <th>Name</th> </tr> </thead> <tbody> <?php $rank = 0; foreach($rows as $row): ++$rank; ?> <tr> <td><?php echo $rank; ?></td> <td><?php echo $row['username']; ?></td> </tr> <?php endforeach; ?> </tbody> </table> </div> Should I separate that query and split it into two queries so it will look for the sign ups at a later point or is there still a way to do this within one query? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted July 8, 2015 Share Posted July 8, 2015 Use an outer join in your query. Look it up. PS - this is a good example of what happens when a programmer makes assumptions and doesn't explicitly check the results of things like query executions, file opens, etc. Always, always, always check things. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.