Here is my code:

<?php
$connection = mysql_connect("myserver.com","myuser","mypassword") or die ("Couldn't connect to server."); 
$db = mysql_select_db("mydb", $connection) or die ("Couldn't select database."); 

$search=$_POST['search'];

$data = "SELECT firstname, lastname, task FROM inventors WHERE taskdate - curdate() = 1";
  $query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());
  $data2 = mysql_fetch_array($query);

echo "$data2[firstname] "; 
echo "$data2[lastname] ";
echo "$data2[task]"; 
  
?>

In my database, I have two records that meet the criteria.
When I run the select in MySQLAdmin, it brings me two records.
The PHP, though, echoes only one.
What's wrong with the code?

Thank you!

Replace lines 11 to 13 with the following

echo $data2['firstname']; 
echo $data2['lastname'];
echo $data2['task'];

Replace lines 11 to 13 with the following

echo $data2['firstname']; 
echo $data2['lastname'];
echo $data2['task'];

Nope, it didn't work.

You are supposed to put mysql_fetch_array in a while loop.

while( $data2 = mysql_fetch_array($query)) {
echo $data2['firstname']; 
echo $data2['lastname'];
echo $data2['task']; 
}

No, doesn't work either...

You are supposed to put mysql_fetch_array in a while loop.

while( $data2 = mysql_fetch_array($query)) {
echo $data2['firstname']; 
echo $data2['lastname'];
echo $data2['task']; 
}

To test if no rows are found try the following:

while( $data2 = mysql_fetch_array($query)) {
echo $data2[0]; 
echo $data2[1];
echo $data2[2]; 
}

The nothing is displayed then that means there are no rows due to the WHERE clause.

Still returns one row instead of two... :S

To test if no rows are found try the following:

while( $data2 = mysql_fetch_array($query)) {
echo $data2[0]; 
echo $data2[1];
echo $data2[2]; 
}

The nothing is displayed then that means there are no rows due to the WHERE clause.

Still returns one row instead of two... :S

So it displayed something... So we know that you were using the right names but the normal cause of this is an extra line of code. Try the following script.

<?php
$connection = mysql_connect("myserver.com","myuser","mypassword") or die ("Couldn't connect to server."); 
$db = mysql_select_db("mydb", $connection) or die ("Couldn't select database."); 

$search=$_POST['search'];

$data = "SELECT firstname, lastname, task FROM inventors WHERE taskdate - curdate() = 1";
  $query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());
while($data2 = mysql_fetch_assoc($query)) {
echo $data2['firstname']; 
echo $data2['lastname'];
echo $data2['task']; 
echo '<hr>';
}
?>

Now what happens?

@cwarn23, I think the OP was saying, that it shows only 1 record instead of 2 !
@levsha,
Are you sure you have 2 records for that query ? If you have 2 records, it *should* display them. There is nothing wrong with the query or the while loop.

@cwarn23, I think the OP was saying, that it shows only 1 record instead of 2 !

Maybe my explanation was too short. Sometimes people will do the following:

<?php
$connection = mysql_connect("myserver.com","myuser","mypassword") or die ("Couldn't connect to server."); 
$db = mysql_select_db("mydb", $connection) or die ("Couldn't select database."); 

$search=$_POST['search'];

$data = "SELECT firstname, lastname, task FROM inventors WHERE taskdate - curdate() = 1";
  $query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());
$data2 = mysql_fetch_assoc($query)
while($data2 = mysql_fetch_assoc($query)) {
echo $data2['firstname']; 
echo $data2['lastname'];
echo $data2['task']; 
echo '<hr>';
}
?>

However the above is incorrect and therefore should be replaced with the below.

<?php
$connection = mysql_connect("myserver.com","myuser","mypassword") or die ("Couldn't connect to server."); 
$db = mysql_select_db("mydb", $connection) or die ("Couldn't select database."); 

$search=$_POST['search'];

$data = "SELECT firstname, lastname, task FROM inventors WHERE taskdate - curdate() = 1";
  $query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());
while($data2 = mysql_fetch_assoc($query)) {
echo $data2['firstname']; 
echo $data2['lastname'];
echo $data2['task']; 
echo '<hr>';
}
?>

Basically that additional line can chew that first mysql result therefore only the second in this post should be used.

Hmm.. You are right! That makes sense..

So it displayed something... So we know that you were using the right names but the normal cause of this is an extra line of code. Try the following script.

<?php
$connection = mysql_connect("myserver.com","myuser","mypassword") or die ("Couldn't connect to server."); 
$db = mysql_select_db("mydb", $connection) or die ("Couldn't select database."); 

$search=$_POST['search'];

$data = "SELECT firstname, lastname, task FROM inventors WHERE taskdate - curdate() = 1";
  $query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());
while($data2 = mysql_fetch_assoc($query)) {
echo $data2['firstname']; 
echo $data2['lastname'];
echo $data2['task']; 
echo '<hr>';
}
?>

Now what happens?

It displays a horizontal rule under the first line of text. :)

Bingo! It worked.
Sorry for the previous comment - I missed the point - the extra line of code.

Thank you a million!

I hope one day I'll become as good at PHP as to be able to help others. :)

It displays a horizontal rule under the first line of text. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.