简单的SQL& PHP查询失败

时间:2014-02-02 13:26:40

标签: php mysql sql

我暂时没有使用PHP或SQL,似乎无法弄清楚为什么这个查询失败了。可能会变得很傻:)。

<php?
    $dbconn = mysql_connect("localhost","xxx","xxx");
    if (!$dbconn)
    {
    die('Error connecting to DB!');
    }
    if (! @mysql_select_db('rdrkictj_rsvp') ) 
    { 
        die(mysql_error()); 
    } 
    if(isset($_GET['id'])){
        $ID = $_GET['id'];
        $stockcount = $_GET['stockcount'] - 1;
    }
    else
        die(mysql_error()); 

    mysqli_query($dbconn,'UPDATE products SET stockcount = "5" WHERE id = "1"');

    mysqli_close($dbconn);
?>

我收到以下错误:

  

警告:mysqli_query()要求参数1为mysqli,resource   在第19行的/home/rdrkictj/public_html/test/buyit.php中给出

     

警告:mysqli_close()要求参数1为mysqli,resource   在第21行的/home/rdrkictj/public_html/test/buyit.php中给出

非常感谢任何建议。

3 个答案:

答案 0 :(得分:1)

<php?应为<?php,您也可以将mysql函数与mysqli函数混合使用。选择其中之一(mysqli)。所以,改变:他

mysql_connect("localhost","xxx","xxx");

mysqli等价物:

mysqli_connect("localhost","xxx","xxx");

同时将mysql_error()更改为mysqli_error()

最后改变:

@mysql_select_db

为:

@mysqli_select_db

答案 1 :(得分:0)

使用mysqli或mysql(建议使用mysqli)

例如:

  

$ dbconn = mysql_connect(“localhost”,“xxx”,“xxx”);

应该是

  

$ dbconn = mysqli_connect(“localhost”,“xxx”,“xxx”);

其他人也是如此

完整代码:

<?php
    $dbconn = mysqli_connect("localhost","xxx","xxx") or die('Error connecting to server');

    if (! @mysqli_select_db($dbconn, 'rdrkictj_rsvp') ) 
    { 
        die(mysqli_error($dbconn)); 
    } 
    if(isset($_GET['id'])){
        $ID = $_GET['id'];
        $stockcount = $_GET['stockcount'] - 1;
    }
    else
        die(mysqli_error($dbconn)); 

    mysqli_query($dbconn,'UPDATE products SET stockcount = "5" WHERE id = "1"');

    mysqli_close($dbconn);
?>

答案 2 :(得分:0)

好的,这就是当你将两个教程混合在一起时会发生什么-_-

感谢两位受访者。以下代码有效:

$con=mysqli_connect("localhost","xxx","xxx","xxx");

    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    mysqli_query($con,'UPDATE products SET stockcount = "5" WHERE id = "1"');

    mysqli_close($con);