Jump to content


MySQL Query


6 replies to this topic

#1 °•Guru•°

    Young Padawan

  • Members
  • Pip
  • 73 posts
  • Gender:Male
  • Location:Australia, Sydney

Posted 22 May 2007 - 11:06 PM

Hey Everyone it's been a while since my last post and i have a blog now yay :D...

anyways back to the point:

SELECT *
FROM mytable
ORDER BY RAND() LIMIT 0,20

thats my Query and pretty much it gets 20 words out of a list of 150 and displays them randomly...but what i'm having trouble is how to seperate these words as 1 per recordset if that makes sense so i can display all 20 in a table 1 word in each cell...and i hoping do this some how through mysql or do i need to do this through php...i am using the recordset thingy through dreamwever...NOT HAND CODING IT...(can't be bothered lol)


Cheers in advance

PS ATM i have one word repeating 20 times which is a different word each time the page refreshes

and find attached below a screenshot of what i want/ am getting ATM just not with the different words and thats the problem is only displaying the annoying 1 word!! :D anyways here is the screenshot

Attached Files



#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 23 May 2007 - 02:01 PM

Your problem is you're using dreamweaver and not hand coding it :)

Watch, it takes less time to hand code the following than it does to even open dreamweaver and find the file you want to edit. Plus, it will probably work better then what ever dreamweaver spits out:
$sql = mysql_query("SELECT * FROM mytable ORDER BY RAND() LIMIT 20");
echo '<table>
	'<tr>';
for($x=0; $r = mysql_fetch_assoc($sql); $x++){
	if($x == 4){
		echo '	</tr><tr>';
		$x=0;
	}

	echo '		<td>'.$r['word'].'</td>'."\n";
}
echo '	</tr>
</table>';

Edited by rc69, 23 May 2007 - 02:03 PM.


#3 °•Guru•°

    Young Padawan

  • Members
  • Pip
  • 73 posts
  • Gender:Male
  • Location:Australia, Sydney

Posted 23 May 2007 - 06:12 PM

hmmm i keep on getting a parse error when i drop that in? how should i place it on my php page?

the error happens at echo '<table> and down??

Thanks for the help so far :)

PS here is my code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<?php require_once('Connections/myconnection.php'); ?>
<?php
$sql = mysql_query("SELECT * FROM mytable ORDER BY RAND() LIMIT 20");
echo '<table>
	'<tr>';
for($x=0; $r = mysql_fetch_assoc($sql); $x++){
	if($x == 4){
		echo '	</tr><tr>';
		$x=0;
	}

	echo '		<td>'.$r['word'].'</td>'."\n";
}
echo '	</tr>
</table>';
?>
<body>

</body>
</html>

Is this correct as the last end closing php tag is still red not bold? and anything below it just is red aswell which i know is wrong? Cheers

Edited by °•Guru•°, 23 May 2007 - 06:23 PM.


#4 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 23 May 2007 - 07:38 PM

*Gasp*
Rc made an error in his code!

$sql = mysql_query("SELECT * FROM mytable ORDER BY RAND() LIMIT 20");
echo '<table>';
	'<tr>';
for($x=0; $r = mysql_fetch_assoc($sql); $x++){
	if($x == 4){
		echo '	</tr><tr>';
		$x=0;
	}

	echo '		<td>'.$r['word'].'</td>'."\n";
}
echo '	</tr>
</table>';

You should try to actually solve the problem yourself since its just a simple parse error. You should have been able to spot it immediately that rc missed an end quote and semicolon.

#5 °•Guru•°

    Young Padawan

  • Members
  • Pip
  • 73 posts
  • Gender:Male
  • Location:Australia, Sydney

Posted 23 May 2007 - 07:45 PM

ya i picked up on it and fixed it...was busy so couldn't post back straight away...Thanks for the help everyone :)

#6 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 23 May 2007 - 10:10 PM

View PostDemonslay, on May 23 2007, 06:38 PM, said:

*Gasp*
Rc made an error in his code!

$sql = mysql_query("SELECT * FROM mytable ORDER BY RAND() LIMIT 20");
echo '<table>';
	'<tr>';
for($x=0; $r = mysql_fetch_assoc($sql); $x++){
	if($x == 4){
		echo '	</tr><tr>';
		$x=0;
	}

	echo '		<td>'.$r['word'].'</td>'."\n";
}
echo '	</tr>
</table>';

You should try to actually solve the problem yourself since its just a simple parse error. You should have been able to spot it immediately that rc missed an end quote and semicolon.
Ya, i forgot my usual disclaimer at the end of my post (the i make parse errors frequently one).

Unfortunately, it wasn't that i forgot an end quote, but added too many quotes.
echo '<table>
	<tr>';
Although Guru solved this on his own, demonslay, you made a mistake also:
echo '<table>';
	'<tr>';
How is the <tr> supposed to be displayed? :)

#7 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 24 May 2007 - 05:59 PM

Rofl. I make a mistake while correcting your mistake, oh the irony!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users