Jump to content

Recommended Posts

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. 

Link to comment
https://forums.phpfreaks.com/topic/297228-undefined-offset-0/
Share on other sites

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?  

Link to comment
https://forums.phpfreaks.com/topic/297228-undefined-offset-0/#findComment-1515901
Share on other sites

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.

  • Like 1
Link to comment
https://forums.phpfreaks.com/topic/297228-undefined-offset-0/#findComment-1515922
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.