• Welcome to Valhalla Legends Archive.
 

[PHP] + MySQL getting multiple rows

Started by j0k3r, February 11, 2004, 09:06 PM

Previous topic - Next topic

j0k3r

I've written my page so it can get the first row from MySQL, now how would I go about getting the second row?
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

Ersan

#1
global $myrow;
@mysql_query(" SELECT * from {tablename} ");
 $myrow = mysql_fetch_array($result);
 if($myrow != NULL) {
    extract($myrow);
echo(" $data1, $data2, $data3 ");
}

j0k3r

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.
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

Banana fanna fo fanna

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

j0k3r

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++.
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo


j0k3r

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?
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

Banana fanna fo fanna

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.

j0k3r

#8
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]
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

Banana fanna fo fanna

$result is just a pointer. It just references the results of a query, its nothing special.

CrAzY

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

j0k3r

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.
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo