dongzouxigu12345 2015-02-11 15:04
浏览 59
已采纳

使用php从MySQL返回信息

I'm currently trying to get into php and MySQL and i'm using this little project as a learning curve, but I've hit a bump that I can't get through. I have the following code that is accessing my MySQL database and I am currently trying to echo the data out using the following search function:

<?php
ob_start();
require("config.php");
ob_end_clean();

$req=$_REQUEST['workingDate'];
$req2=$_REQUEST['location'];

mysql_connect("XXXXXXXXXXX",$username,$password);
mysql_select_db($database) or die( "Unable to select database");

if ($req!="all" && $req2!="all")  $query="SELECT * FROM TrackerTable WHERE workingDate='$req' AND location1='$req2'";
else if($req=="all" && $req2!="all" ) $query="SELECT * FROM TrackerTable WHERE location1='$req2'";
else if($req!="all" && $req2=="all" ) $query="SELECT * FROM TrackerTable WHERE make='$req'";
else if($req=="all" || $req2=="all" ) $query="SELECT * FROM TrackerTable";

$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_query($result);
mysql_close();

$i=0;

for ($i; $i < $num; $i++){
        $f12=mysql_result($result,$i,"workingDate");
        $f13=mysql_result($result,$i,"location1");
echo $f12." ".$f13."<br />";
}
?>

The problem I have is that whenever I try to search the database using the following html form:

<form method="post" action="searchFunction.php"  name="input" id="searchform">
    <label class="section">Search Options</label><br />
    <input name="workingDate" type="text" id="workingDate" placeholder="Search by Date">
    <input name="location1" type="text" id="location1" placeholder="Search by Location">
    <input name="submit" type="submit" id="add" value="Find!">
</form>

I get this error thrown at me:

 Warning: mysql_query() expects parameter 1 to be string, resource given in /XXXXXX/XXXXXX/XXXXX/XXXX/XXXXX.com/XXXXXX/searchFunction.php on line 20

I have no idea what could be causing the problem as it all seems fairly in place to me. Is there an obvious mistake i'm making or is it all just completely messed up? I've been looking at it that long that I can't tell what makes sense anymore!

Any help would be greatly appreciated. Thanks guys!

  • 写回答

2条回答 默认 最新

  • duanmeng3126 2015-02-11 15:09
    关注

    You are sure that variable $query is defined? This because in the statement if/elseif is where you define the variable $query.

       // first define default value for your $query variable
       $query="SELECT * FROM TrackerTable";
    
       // then use your if/else statement
    if ($req!="all" && $req2!="all") .....................
    

    You have an error

    $result=mysql_query($query);
    $num=mysql_numrows($result);
    
    mysql_query($result); // error here
    
    mysql_close();
    

    $result is not a string, is resource. mysql_query($result);

    Check http://php.net/manual/en/function.mysql-query.php for details

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?