Valhalla Legends Archive

Programming => Web Development => Topic started by: j0k3r on February 11, 2004, 09:06 PM

Title: [PHP] + MySQL getting multiple rows
Post by: j0k3r on February 11, 2004, 09:06 PM
I've written my page so it can get the first row from MySQL, now how would I go about getting the second row?
Title: Re:[PHP] + MySQL getting multiple rows
Post by: Ersan on February 11, 2004, 09:22 PM
global $myrow;
@mysql_query(" SELECT * from {tablename} ");
 $myrow = mysql_fetch_array($result);
 if($myrow != NULL) {
    extract($myrow);
echo(" $data1, $data2, $data3 ");
}
Title: Re:[PHP] + MySQL getting multiple rows
Post by: j0k3r on February 12, 2004, 08:51 AM
That didn't help much, maybe posting some of my code would've been good...

$selectdate = mysql_query("SELECT `date` FROM `table1`");
$selecttitle = mysql_query("SELECT `title` FROM `table1`");
$selectname = mysql_query("SELECT `name` FROM `table1`");
$selectmessage = mysql_query("SELECT `message` FROM `table1`");

$date = mysql_fetch_array($selectdate, MYSQL_ASSOC);
$title = mysql_fetch_array($selecttitle, MYSQL_ASSOC);
$name = mysql_fetch_array($selectname, MYSQL_ASSOC);
$message = mysql_fetch_array($selectmessage, MYSQL_ASSOC);


echo "[".$date['date']."] \"".$title['title']."\" ".$name['name']."<BR><BR>";
echo $message['message'];

It's really sloppy right now, I'll touch it up when I get home, but that just gets the first row, and right now there are 2, and eventually it will keep growing.
Title: Re:[PHP] + MySQL getting multiple rows
Post by: Banana fanna fo fanna on February 12, 2004, 04:05 PM
Oh my god, that's disgusting.


$result = mysql_query("SELECT * FROM `table1`");

while ($row = mysql_fetch_array($result)) {
echo "[".$row['date']."] \"".$row['title']."\" ".$row['name']."<br /><br />";
echo $row['message'];
}
Title: Re:[PHP] + MySQL getting multiple rows
Post by: j0k3r on February 12, 2004, 05:05 PM
Quote from: St0rm.iD on February 12, 2004, 04:05 PM
Oh my god, that's disgusting.
Lmao, first time I've heard someone call code disgusting. I threw it together in 5 minutes before going to bed, although I doubt I'd have been able to get it shrunk down that much, thanks++.
Title: Re:[PHP] + MySQL getting multiple rows
Post by: Banana fanna fo fanna on February 13, 2004, 06:45 PM
sure anytime
Title: Re:[PHP] + MySQL getting multiple rows
Post by: j0k3r on February 13, 2004, 07:50 PM
Quote from: St0rm.iD on February 12, 2004, 04:05 PM

while ($row = mysql_fetch_array($result)) {

While we're on it, explain how that line works please?
Title: Re:[PHP] + MySQL getting multiple rows
Post by: Banana fanna fo fanna on February 14, 2004, 09:14 AM
mysql_fetch_array returns FALSE when there are no more rows. The while loop checks $row as its loop condition. "Not null" corresponds to TRUE. When it becomes FALSE, the loop ends.
Title: Re:[PHP] + MySQL getting multiple rows
Post by: j0k3r on February 14, 2004, 12:28 PM
That wasn't what I meant  :P

What values are stored in $result and how does mysql_fetch_array() work to get all the rows?

[edit]Happy 1000 to me  ;D[/edit]
Title: Re:[PHP] + MySQL getting multiple rows
Post by: Banana fanna fo fanna on February 14, 2004, 03:38 PM
$result is just a pointer. It just references the results of a query, its nothing special.
Title: Re:[PHP] + MySQL getting multiple rows
Post by: CrAzY on February 16, 2004, 07:38 AM
Quote from: j0k3r on February 13, 2004, 07:50 PM
Quote from: St0rm.iD on February 12, 2004, 04:05 PM

while ($row = mysql_fetch_array($result)) {

While we're on it, explain how that line works please?

He's Right...
Title: Re:[PHP] + MySQL getting multiple rows
Post by: j0k3r on February 16, 2004, 05:48 PM
Quote from: CrAzY on February 16, 2004, 07:38 AM
Quote from: j0k3r on February 13, 2004, 07:50 PM
Quote from: St0rm.iD on February 12, 2004, 04:05 PM

while ($row = mysql_fetch_array($result)) {

While we're on it, explain how that line works please?

He's Right...
I never said he wasn't, I just didn't understand the line and wanted him to clarify so I don't just use someone else's code and be some leaching 'tard.