MySQL Insert Query请求资源

时间:2013-04-16 08:31:09

标签: php mysql parameters insert resources

我在xampp上运行以下代码(完美安装)。我已经用mysqli替换了mysql并重新安排了查询(连接fisrt,稍后插入到语句中),但都无济于事。请帮忙。

错误: 警告:mysql_query()期望参数2是资源,在第19行的C:\ xampp \ htdocs \ test.php中给出字符串

<?php


    $hname = "localhost";
    $username = "root";
    $password = "";
    $db = "test";

    $connect = mysql_connect($hname, $username, $password, $db);


    if ($connect) {
        echo "Connected to Database <br /> <br />";

        $ins = "INSERT INTO 'test'.'testtable' (name) VALUES ('james')";

        //mysql_select_db ($connect, "users");

        if (mysql_query("INSERT INTO 'test'.'testtable' (name) VALUES ('james')", "mysql_connect($hname, $username, $password, $db)")) {
            echo "Values Entered Successfully. Your Account was created";
        } else {
            echo "Your Account was not created";

        }

    }   else {
            echo "Failed to Connect :(";
    }

?>

3 个答案:

答案 0 :(得分:2)

"mysql_connect($hname, $username, $password, $db)"不应该在引号中。您希望调用该函数并将结果作为参数传递,而不是传递字符串。

但你真的应该把它称为单独的声明,这样你就可以检查错误:

$con = mysql_connect($hname, $username, $password) or die ("Could not connect to DB: ".mysql_error());
mysql_select_db($db, $con);

mysql_query("INSERT INTO 'test'.'testtable' (name) VALUES ('hunaid')", $con);

您的代码的另一个问题是mysql_query的成功并不表示是否插入了任何行,它只是表明查询在语法上是有效的。您需要调用mysql_affected_rows()来查找插入的行数。

你真的不应该使用mysql扩展,它们已被弃用。你应该使用mysqli或PDO。这将允许您使用预准备语句,以避免SQL注入问题。

答案 1 :(得分:0)

尝试连接。

mysql_connect("localhost", "root", "password") or die(mysql_error());
    echo "Connected to MySQL<br />";
mysql_select_db("test") or die(mysql_error());


For the MYSQL INSERT try:


mysql_query("INSERT INTO testtable (name) VALUES('hunaid') ") 
or die(mysql_error());  

希望它有所帮助!

答案 2 :(得分:0)

mysql_connect中没有$db参数。删除它。

点击mysql_connect参数http://php.net/manual/en/function.mysql-connect.php

的此链接

为什么要使用mysql_query传递mysql_connect($hname, $username, $password)。您已在MySQL查询之前创建连接。如果未指定链接标识符,则假定mysql_connect()打开的最后一个链接。

但是如果您特别将connect语句传递给查询,则应该作为资源传递。目前您正在传递一个字符串。请勿将connect语句括在引号中。

同时删除数据库和表名称周围的单引号。

您需要的是:

if (mysql_query("INSERT INTO test.testtable (name) VALUES ('hunaid')", mysql_connect($hname, $username, $password))) {
    echo "Values Entered Successfully. Your Account was created";
} else {
   echo "Your Account was not created";

}

但我认为这已经足够了

if (mysql_query("INSERT INTO test.testtable (name) VALUES ('hunaid')")) {
    echo "Values Entered Successfully. Your Account was created";
} else {
   echo "Your Account was not created";
}

无需指定connect语句,因为您已在顶部创建了它。

此外,不是在查询中指定db。在运行查询之前,添加mysql_select_db以选择dB。

所以完整的事情将是:

$hname = "localhost";
$username = "root";
$password = "";
$db = "test";

$connect = mysql_connect($hname, $username, $password);

if ($connect) {

    echo "Connected to Database <br /> <br />";

    mysql_select_db($db);

    $ins = "INSERT INTO testtable (name) VALUES ('james')";

    if (mysql_query($ins)) {
         echo "Values Entered Successfully. Your Account was created";
    } else {
        echo "Your Account was not created";

    }
}   else {
    echo "Failed to Connect :(";
}

更多的是避免使用mysql *函数。

自PHP 5.5.0起,此扩展程序已弃用,将来将被删除。相反,应使用MySQLiPDO_MySQL扩展程序

希望这可以帮助你:)