php - show 2 random rows instead of one -
my sql query:
$q = mysql_query("select * `ads` keywords '%$key%' order rand()");
results: keyword123
this query searches , results in 1 random row want show 2 random rows. how that?
any solution?
how??
im grabbing using this
$row = mysql_fetch_array($q); if ($row <= 0){ echo 'not found'; }else{ echo $row['tab']; }
that query (as-is) return more 1 row (assuming more 1 row %$key%). if you're seeing 1 record, it's possible you're not cycling through result set, rather pulling top response off stack in php code.
to limit response 2 records, append limit 2
onto end of query. otherwise, you'll every row matches like
operator.
//build our query $sql = sprintf("select tab ads keyword '%s' order rand() limit 2", ('%'.$key.'%')); // load results of query variable $results = mysql_query($sql); // cycle through each returned record while ( $row = mysql_fetch_array($result) ) { // $row echo $row['tab']; }
the while-loop
run once per returned row. each time runs, $row
array inside represent current record being accessed. above example echo values stored in tab
field within db-table.
Comments
Post a Comment