Current location: Hot Scripts Forums » Programming Languages » PHP » random function sql statement


random function sql statement

Reply
  #1 (permalink)  
Old 12-16-11, 12:27 PM
cyberfolli cyberfolli is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 93
Thanks: 8
Thanked 0 Times in 0 Posts
random function sql statement

I am trying to figure out how to modify this section of code to make the featured listings random. I tried a couple of variations but I keep getting errors

Code:
// Add ORDER BY and LIMIT functions into the SQL
  $sql2 = $sql;

  // Make all featured listings come first
  $sql2.= ' ORDER BY ';

  if (!isset($search['seed']))
   $search['seed'] = 0;

  if (isset($search['order_by']) && $search['order_by'] != 'ANY' &&
     ($search['order_by'] == 'price' ||
     $search['order_by'] == 'date_added' || $search['order_by'] == 'bedrooms' ||
     $search['order_by'] == 'bathrooms')) 
   $sql2.= ' ' . $search['order_by'] . ' ';
  else
   $sql2.= ' featured, RAND(' . $search['seed'] . ') ';

  if (isset($search['order_by']) && $search['order_by'] != 'ANY' 
  && isset($search['order_by_type']) && $search['order_by_type'] != 'ANY' &&
  ($search['order_by_type'] == 'DESC' || $search['order_by_type'] == 'ASC' )) 
   $sql2.= $search['order_by_type'];

  $sql2.=  ' LIMIT ' . $results_page . ', ' . $conf['search_results'];
Reply With Quote
  #2 (permalink)  
Old 12-18-11, 09:22 AM
alxkls alxkls is offline
Newbie Coder
 
Join Date: Nov 2011
Posts: 98
Thanks: 0
Thanked 9 Times in 9 Posts
what errors? sql or php errors?
post the errors
echo the complete sql query and see if something is wrong with it
Reply With Quote
  #3 (permalink)  
Old 12-18-11, 09:29 AM
cyberfolli cyberfolli is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 93
Thanks: 8
Thanked 0 Times in 0 Posts
There are no errors .. i am asking how to change the sql statement to display random information
Reply With Quote
  #4 (permalink)  
Old 12-18-11, 04:56 PM
alxkls alxkls is offline
Newbie Coder
 
Join Date: Nov 2011
Posts: 98
Thanks: 0
Thanked 9 Times in 9 Posts
in other words a list of random results? thats plainly sql:

Code:
select * from my_table order by rand( ) limit 10
Reply With Quote
  #5 (permalink)  
Old 12-18-11, 05:02 PM
cyberfolli cyberfolli is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 93
Thanks: 8
Thanked 0 Times in 0 Posts
if I change this

Code:
 $sql2.= ' ORDER BY ';
to this
Code:
 $sql2.= ' ORDER BY RAND( ) LIMIT 10 ';
I get this error



Critical Error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'featured, RAND(0) LIMIT 0, 12' at line 1
Reply With Quote
  #6 (permalink)  
Old 12-19-11, 05:14 AM
alxkls alxkls is offline
Newbie Coder
 
Join Date: Nov 2011
Posts: 98
Thanks: 0
Thanked 9 Times in 9 Posts
post the complete query

edit:
NOT
Code:
RAND(0)
//nor
RAND( )
//SIMPLY
RAND()
also when you put limit-just limit 12, not limit 0, 12
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to make my affiliate links open in a new window browser??? bigendz13 PHP 3 10-26-10 08:35 AM
ASP upload prob minority ASP 1 06-27-05 08:35 AM
Function that has Sql statement gop373 ASP 3 02-28-05 09:56 AM
PHP Error Fairnie PHP 8 06-26-04 07:15 AM
change my field in this example sal21 ASP 3 07-14-03 02:49 AM


All times are GMT -5. The time now is 08:58 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.