3

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

Hopefully a quick one for one of you out there.

I'm getting two errors that are related to :

Warning: mysql_query() expects parameter 1 to be string, array given

My code is as follows...

 <?php
#
#  FUTURE EVENTS
#

# PAGE SELECTOR FUNCTION
function pageselector ($pagenum, $pages, $arg, $perpage) {
  #page we're loading
  if (!$pagenum) $pagenum=1;
  # PRINT PAGE SELECTOR
  $pages=ceil($pages/$perpage);
  #last = prev page unless we're already at page 1
  if ($pagenum==1) {$last=1;}     else {$last=$pagenum-1;}
  #next = next page unless we have no more pages to display
  if ($pagenum==$pages) $next=$pagenum;     else $next=$pagenum+1;
  #create page numbers
  $pagenumbers="";
  for ($i=1; $i<=$pages; $i++) {
      if ($i==$pagenum)
          $pagenumbers.=$i.' ';
      else
      $pagenumbers.="<a href=\"?pagenum=$i$arg\">$i</a> "; 
        }
  # create page num in case of blank page
  if (!$pagenumbers) {
    $pagenumbers="<a href=\"?pagenum=1$arg\">1</a> "; 
      }
print <<< pageSelector
<div class="pageselector">
Page: 
<a href="?pagenum=1$arg">&laquo;</a> <a href="?pagenum=$last$arg">&lsaquo;</a> 
$pagenumbers
<a href="?pagenum=$next$arg">&rsaquo;</a> <a href="?pagenum=$pages$arg">&raquo;</a>
</div><br />
pageSelector;
  return $pagenum;
  }


$perpage=3;
$now=time()-(1 * 24 * 60 * 60);
$sql="SELECT count(*) AS total FROM event_tbl WHERE dates>'$now'";
$sql=mysql_query($sql);

$pagenum=pageselector ($pagenum, $sql['total'], '', $perpage);

$eventlist=array();
$sql="SELECT * FROM event_tbl WHERE dates>'$now' ORDER BY dates ASC";
$sql=mysql_query($sql);
$sql=mysql_fetch_array($sql);
while ($sqlevent=mysql_query($sql)){
  $eventlist[]=$sqlevent['id'];
    }

for ($i=0; $i<$perpage; $i++) {
  $tempid=($perpage*($pagenum-1))+$i;
  $sql="SELECT * FROM event_tbl WHERE id='$eventlist[$tempid]'";
  $sql=mysql_query($sql);
  $smallevent=mysql_query($sql);
    if ($smallevent['dates']==0)
      break;
  print '<div>';
    $smallevent['description']=strip_tags($smallevent['description'], '<img>');
  $shortdescription=substr($smallevent['description'], 0, 350);
  print '<span style="font-size: 14px;"><b><a href="./event.php?event='.$smallevent['id'].'">'.$smallevent['name'].'</a></b> - <b>Date:</b> '.date('j/n/Y', $smallevent['dates']).' - <b>Price:</b> ';
    $places=$smallevent['placesavailable']-$smallevent['placesbooked'];
    if ($places>0)
    print '&pound;'.$smallevent['price'].'<br />';
  else 
    print '<span class="soldout">SOLD OUT</span><br />';
    print '</span><br />';
    print $shortdescription.'...';
    print '<br /><br />';
    print '<a href="./tellfriend.php?event='.$smallevent['id'].'"><img src="./images/link-tellfriend.png" alt="" /></a> &nbsp;';
    print '<a href="http://maps.google.com/maps?daddr='.$smallevent['postcode'].'" target="_blank"><img src="./images/link-directions.png" alt="" /></a> &nbsp;';
    print '<a href="./event.php?event='.$smallevent['id'].'"><img src="./images/link-info.png" alt="" /></a> &nbsp;';
    if ($smallevent['bookless']==0 & $places>0)
      print '<a href="./bookevent.php?event='.$smallevent['id'].'"><img src="./images/link-book.png" alt="" /></a> ';
    print '</div><br /><br />';
    }
?>

The two error lines are :

while ($sqlevent=mysql_query($sql)){  
$eventlist[]=$sqlevent['id'];   }

$smallevent=mysql_query($sql);

I've got a DB Connect file (Although not sure whether its triggering or not??)

Any help appreciated.

Cheers

Community
  • 1
  • 1
StuBlackett
  • 3,789
  • 15
  • 68
  • 113

3 Answers3

6
$eventlist=array();
$sql="SELECT * FROM event_tbl WHERE dates>'$now' ORDER BY dates ASC";
$sql=mysql_query($sql);
$sql=mysql_fetch_array($sql);
while ($sqlevent=mysql_query($sql)){
  $eventlist[]=$sqlevent['id'];
}

The error is pretty clear. For some reason (I don't know what), you call mysql_query again with an array.

Perhaps you meant:

$eventlist=array();
$sql = "SELECT * FROM event_tbl WHERE dates > '$now' ORDER BY dates ASC";
$res = mysql_query($sql);
while ($sqlevent = mysql_fetch_array($res)) {
  $eventlist[] = $sqlevent['id'];
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2

You have to use different names for your variables. Just before the faulty while :

$sql=mysql_query($sql);
$sql=mysql_fetch_array($sql);

The second line assigns the result of mysql_fetch_array to the variable named $sql which is then no longer a string. You can use $sql_result for example.

However, reading your code, I think there's other mistakes. For example, I think you want to do something like this :

$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){
  $eventlist[] = $row['id'];
}

mysql_query is for getting the results from the query and then you can read the results with mysql_fetch_array. You can't use the result of mysql_query directly. There's plenty of good examples in the linked documentation, you should read them.

The second problem is exactly of the same kind.

krtek
  • 26,334
  • 5
  • 56
  • 84
  • He probably doesn't want that semicolon in there. And usually one uses the name `$result` for the resultset i.e. what you get back from `mysql_query`, then something like `$row` for each row in that resultset. – Lightness Races in Orbit Apr 05 '11 at 15:29
  • @Tomalak yeah sure, my copy-paste was a little too quick, I corrected the mistakes ! Thanks :) – krtek Apr 05 '11 at 15:32
0
$sql=mysql_fetch_array($sql);  
while ($sqlevent=mysql_query($sql)){
$eventlist[]=$sqlevent['id'];
}

$sql is an array as mysql_fetch_array returns an array !!! try using other variable !


$eventlist=array();
$sql="SELECT * FROM event_tbl WHERE dates>'$now' ORDER BY dates ASC";
$result=mysql_query($sql,$connection);
$sql=mysql_fetch_array($result);

....

Sourav
  • 17,065
  • 35
  • 101
  • 159