PHP警告:mysql_query()期望参数1为字符串

时间:2011-04-05 15:20:51

标签: php mysql

  

可能重复:
  mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

希望你们中的一个能快速完成。

我收到两个与以下相关的错误:

  

警告:mysql_query()期望   参数1为字符串,给定数组

我的代码如下......

 <?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 />';
    }
?>

两条错误行是:

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

$smallevent=mysql_query($sql);

我有一个DB Connect文件(虽然不确定它是否触发?)

任何帮助表示感谢。

干杯

3 个答案:

答案 0 :(得分: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'];
}

错误很清楚。出于某种原因(我不知道是什么),您再次使用数组调用mysql_query

也许你的意思是:

$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'];
}

答案 1 :(得分:2)

您必须为变量使用不同的名称。就在错误的while

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

第二行将mysql_fetch_array的结果分配给名为$sql的变量,该变量不再是字符串。例如,您可以使用$ sql_result

然而,阅读你的代码,我认为还有其他错误。例如,我想你想做这样的事情:

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

mysql_query用于从查询中获取结果,然后您可以使用mysql_fetch_array阅读结果。您无法直接使用mysql_query的结果。链接文档中有很多很好的例子,你应该阅读它们。

第二个问题恰恰相同。

答案 2 :(得分:0)

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

$ sql是一个数组,因为mysql_fetch_array返回一个数组! 尝试使用其他变量!


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

...