0

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!

3
Contributors
12
Replies
13
Views
7 Years
Discussion Span
Last Post by levsha
Featured Replies
  • 1

    [QUOTE]@cwarn23, I think the OP was saying, that it shows only 1 record instead of 2 ! [/QUOTE] Maybe my explanation was too short. Sometimes people will do the following: [CODE=php]<?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']; … Read More

0

Replace lines 11 to 13 with the following

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

Replace lines 11 to 13 with the following

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

Nope, it didn't work.

0

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']; 
}
0

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']; 
}
0

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.

0

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.

0

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?

0

@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.

1

@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.

0

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. :)

0

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. :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.