Mysqli准備聲明 - 返回錯誤,但為什么?

[英]Mysqli Prepare Statement - Returning False, but Why?


I have a function that generates a prepared INSERT statement based on an associative array of column names and values to be inserted into that column and a table name (a simple string):

我有一個函數,它根據要插入該列的列名和值的關聯數組以及一個表名(一個簡單的字符串)生成一個准備好的INSERT語句:

function insert ($param, $table) {
        $sqlString = "INSERT INTO $table (".implode(', ',array_keys($param)).') VALUES ('.str_repeat('?, ', (count($param) - 1)).'?)';
        if ($statement = $this->conn->prepare($sqlString)):
            $parameters = array_merge(array($this->bindParams($param), $param));
            call_user_func_array(array($statement, 'bind_param', $parameters));
            if (!$statement->execute()):
                die('Error! '.$statement->error());
            endif;
            $statement->close();
            return true;
        else:
            die("Could Not Run Statement");
        endif;
    }

My problem is that $this->conn->prepare (it's part of a class, conn is a NEW mysqli object, which works with no issues) returns false, but does not give me a reason why!

我的問題是$ this-> conn-> prepare(它是一個類的一部分,conn是一個新的mysqli對象,它沒有問題)返回false,但是沒有給我一個理由!

Here is a sample $sqlString that gets built for the prepare statement:

以下是為prepare語句構建的示例$ sqlString:

INSERT INTO students (PhoneNumber, FirstName, MiddleInit, LastName, Email, Password, SignupType, Active, SignupDate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)

Can anyone see a problem with this parameterized statement? Any reason the prepare function would return false?

任何人都可以看到這個參數化語句的問題? prepare函數返回false的原因是什么?

1 个解决方案

#1


44  

I'm copying the solution into this answer so this can be given an upvote, otherwise the question will appear in the "unanswered questions" forever. I'm marking this answer CW so I won't get any points.

我正在將解決方案復制到這個答案中,因此可以給予一個upvote,否則問題將永遠出現在“未答復的問題”中。我將這個答案標記為CW,所以我不會得到任何積分。

@Andrew E. says:

@Andrew E.說:

I just turned on mysqli_report(MYSQLI_REPORT_ALL) to get a better understanding of what was going on - turns out that one of my field names was incorrect - you'd think that prepare() would throw an exception, but it fails silently.

我只是打開mysqli_report(MYSQLI_REPORT_ALL)來更好地理解發生了什么 - 事實證明我的一個字段名稱是不正確的 - 你認為prepare()會拋出異常,但它會無聲地失敗。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/08/02/c0b854d542ccd4bc6eb38324d096f4a9.html



 
  © 2014-2022 ITdaan.com 联系我们: