954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

mysql ORDER By RAND(), LIMIT 1

Hi, I have a bit of a strange problem where I am unable to "echo" results from a database -

I will show you the code then explaine what im trying to do

<?php
include_once "connect_to_mysql.php";
 
$sql = mysql_query("SELECT * FROM table ORDER BY RAND(), LIMIT 1");

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

	$add = $row["add"];
	$link = $row["link"];
	$title = $row["title"];
	
	echo "
		<b>$add</b>
		<a href='$add'$link</a>						
		";
	
	}	
	
?>


db has the following fields id, add, link, title

The script to connect to the database works fine, as if i change the host, it gives me an error.

But im not getting any images from the database

What im trying to do is, randomly select any field and link the add field using the link field, so if a user clicks the add shown, it will take them to the website address in the link field.

Im also not sure how to add the title, so when a user hovers over the add shown, it will show a title tag ???

Can someone provide any helpful guidance to help me learn more coding skills.

Thanks to those who reply,
Lloyd

LloydFarrell
Junior Poster
101 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

Hm, what kind of query is that?

mysql_query("SELECT * FROM table ORDER BY RAND(), LIMIT 1");


I guess...

<?php
include_once "connect_to_mysql.php";
 
$sql = mysql_query("SELECT * FROM table");
 
while($row = mysql_fetch_assoc($sql))
{ 
 
	$add = $row['add'];
	$link = $row['link'];
	$title = $row['title'];
 
	echo '
		<b>'.$add.'</b>
		<a href="'.$add.$link.'" title="'.$title.'"></a>';
 
}	
 
?>
amd_k8
Light Poster
49 posts since Jan 2010
Reputation Points: 12
Solved Threads: 11
 

hi thanks for your post reply,

The query searches the db randomly selects one add and displays it

What you provided gave an error !!

I tried
$title which worked, but im not getting the add to show as its in a java script format as shown below
*****************************************

*********************************************************
im trying to rotate my adds from tradedoubler but im not sure if its possible to display java script correctly from a mysql db ??

Thanks for your help

lloyd

LloydFarrell
Junior Poster
101 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

Read the FAQ, you have a syntax error in your query.

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

you had syntax error on your query
SELECT * FROM table ORDER BY RAND(), LIMIT 1
erase the comma next to RAND(), it should look something like thisSELECT * FROM table ORDER BY RAND() LIMIT 1

vaultdweller123
Posting Pro
554 posts since Sep 2009
Reputation Points: 42
Solved Threads: 75
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: