PHP准备好的语句给我错误0

时间:2013-06-04 12:23:49

标签: php mysql prepared-statement

这让我感到沮丧超过一个小时,任何帮助都会受到赞赏。

我已使用下面的评论“标记”违规代码

        $hostt = "localhost";
$db_database = "dekit_ryan";
$db_username = "dekit_ry";
$db_password= "s%g0oM";


$link = mysqli_connect($hostt , $db_username, $db_password, $db_database);

if (!$link) {
    printf("Unable to connect to DB, the error is: %s\n", mysqli_connect_error());
    exit();
}


// ************* Problem seems to be with the below line ***********
   $stmt = mysqli_prepare($link, "INSERT INTO campaign(cname,public_ref_name,redirect_to) VALUES (?, ?,  ?)");

        mysqli_stmt_bind_param($cname, $pub_ref_name, $url);
        mysqli_stmt_execute($stmt);

        // Check if the row was successfully inserted or spit out an error
        if(mysqli_stmt_affected_rows($stmt) != 1)
        {
            return "Error: Problem inserting a record into the database, exiting..." . mysqli_errno($link);
            exit;
        }

当我运行脚本时,我总是得到:

警告:mysqli_stmt_bind_param()要求参数1为mysqli_stmt,第44行/home/dekit3/public_html/adTracking/classes/AdsTracking.php中给出的字符串

错误:将记录插入数据库时​​出现问题,退出... 0

var dump

object(mysqli)#2 (18) { ["affected_rows"]=> int(0) ["client_info"]=> string(6) "5.1.62" ["client_version"]=> int(50162) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["field_count"]=> int(0) ["host_info"]=> string(25) "Localhost via UNIX socket" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(10) "5.1.62-cll" ["server_version"]=> int(50162) ["stat"]=> string(151) "Uptime: 1210463 Threads: 10 Questions: 45999394 Slow queries: 1585 Opens: 3541810 Flush tables: 57 Open tables: 256 Queries per second avg: 38.1" ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(1861790) ["warning_count"]=> int(0) } 

2 个答案:

答案 0 :(得分:3)

该行

 mysqli_stmt_bind_param($cname, $pub_ref_name, $url);

要求您使用第一个参数$stmt变量。

修改

请查看manual中的示例2。

答案 1 :(得分:3)

您正在为mysqli_stmt_bind_param设置不正确的参数。它应该是:

mysqli_stmt_bind_param($stmt, 'sss', $cname, $pub_ref_name, $url);

根据数据库中的数据类型更改'sss'。

看这里:

http://www.php.net/manual/en/mysqli-stmt.bind-param.php