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
https://forums.phpfreaks.com/topic/104167-solved-need-help-on-random-select/
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";

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();

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.