...

View Full Version : getting all of an array from mysql



Xiong Chiamiov
03-17-2007, 05:22 PM
I've had no formal training in PHP/MySQL, just what I pick up from my books. So, as many others have done, I am creating a simple front page where it shows news with a date greater than or equal to today's. The problem is, I always lose one news item (somehow due to my arrays), but I don't know enough about the language to understand why.
So, I've got my connection stuff, then this:


$sql = "SELECT content FROM `news` WHERE `date` >= '2007-01-28' ORDER BY date";
$rs = mysql_query($sql, $conn);
$foo = mysql_fetch_array($rs);
if($foo) {
$row = $foo;
}

Note that I still have a permanent date, because I've been working on it over a while and I don't want to have to change the dates in my db to be in the future.
Then, down later, I've got:


while ( $row = mysql_fetch_array($rs) )
{
echo ($row["content"] . "<br>");
}

As I said, I always lose the 1st result, but am not sure quite why. Thanks for the help.

Fumigator
03-18-2007, 12:46 AM
Your first result you fetched on the third line of the first block of code you posted, here:



$foo = mysql_fetch_array($rs); //fetches first row in the query result


Then you don't do anything with that row before you begin your loop, and the first thing you do is fetch another row:



while ( $row = mysql_fetch_array($rs) ) //fetches the next row in the query result
{
echo ($row["content"] . "<br>");
}


Plus, you're checking the data you assigned to the fetched row, when you should be checking the mysql_query() result:


$rs = mysql_query($sql, $conn);
// $foo = mysql_fetch_array($rs); //just don't do this here
//if($foo) {
if (!rs) { //check the result of mysql_query()
die("SQL Error! ".mysql_error());
}

Xiong Chiamiov
03-18-2007, 01:06 AM
Thank you very much. That was what I was looking for.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum