Jump to content

[SOLVED] need help on random select


pixeltrace

Recommended Posts

hi,

 

i need help, i need to display images limit to 1 only at random select.

this is the code that i used

<?

function show_latest_banner() {
global $database, $mosConfig_live_site;

$query = "SELECT * FROM #__events WHERE state =1 ORDER BY publish_up RAND() LIMIT 1";
$database->setQuery( $query );
$rows = $database->loadObjectList();

?>
<img src="<?= $mosConfig_live_site .'/images/events/'.$rows->images ?>" class="thumbimages">
					<br><br>


<?php
}

show_latest_banner();

?>

 

i am getting an error and i dont know exactly how to fix it.
this is the error i am getting
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /var/www/html/spa/modules/mod_events_banner.php on line 120

 

hope you could help me fix this.

 

thanks!

Link to comment
Share on other sites

The # is the mysql comment character - anything following is taken to be a comment. You should avoid giving your databases,tables or fields names with special characters or reserved words.

 

You can get around the problem by placing the table name in backticks(`), but it would be better to change the table name.

 

$query = "SELECT * FROM `#__events` WHERE state =1 ORDER BY publish_up RAND() LIMIT 1";

Link to comment
Share on other sites

i solve the problem already.

 

thanks!

 

this is the code that i used

 

<? function show_latest_banner() {
global $database, $mosConfig_live_site;

$query = "SELECT id FROM #__events WHERE state =1 ORDER BY RAND() LIMIT 1";
	 	$database->setQuery($query);
	 	$id = $database->loadResult();

$query = "SELECT images FROM #__events WHERE id =$id";
	 	$database->setQuery($query);
	 	$image = $database->loadResult();
?>
<a href="index.php?option=com_events&task=event_details&id=<? echo $id ?>&Itemid=78"><img src="<?= $mosConfig_live_site .'/images/events/'.$image ?>" class="thumbimages"></a>
<?php
}

show_latest_banner();

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.