我最近从mysql切换到mysqli并开始使用预处理语句。 在mysql中我们做
$result = mysql_query("SELECT * FROM table WHERE id = ?");
因此我们将整个表的数组放在一个变量中。
但是在mysqli中我们做了
mysqli_stmt_bind_result($stmt, $result);
所以基本上这里只有一个变量绑定到变量结果。
我们如何获得从mysql获得的相同变量(数组)?
PS - 希望我的问题很清楚。我知道他们的方法可能不多,但我正在寻找最好的方法。
PSS - 我对程序方式更加满意。
答案 0 :(得分:5)
最后这段代码正在工作!!!!!
<?php
$host = 'localhost';
$user = 'root';
$pass = '1234';
$data = 'test';
$mysqli = new mysqli($host, $user, $pass, $data);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if ($stmt = $mysqli->prepare("SELECT * FROM sample WHERE t2 LIKE ?")) {
$tt2 = '%';
$stmt->bind_param("s", $tt2);
$stmt->execute();
$meta = $stmt->result_metadata();
while ($field = $meta->fetch_field())
{
$params[] = &$row[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), $params);
while ($stmt->fetch()) {
foreach($row as $key => $val)
{
$c[$key] = $val;
}
$result[] = $c;
}
$stmt->close();
}
$mysqli->close();
print_r($result);
?>
答案 1 :(得分:2)
像这样使用
$con=mysqli_connect( "$host", "$mysql_u", "$mysql_p","$mysql_db");/*these variables are ur host,username, password and db name*/
$val="SELECT * FROM table";
$ex=mysqli_query($con,$val);
while ($row = mysqli_fetch_assoc($ex)) {
$ans=$row['col1'];
....
}
答案 2 :(得分:2)
将准备好的陈述用作..
$stmt = $mysqli->prepare("SELECT id, label FROM test WHERE id = 1");
$stmt->execute();
$res = $stmt->get_result();
$row = $res->fetch_assoc();
printf("id = %s (%s)\n", $row['id'], gettype($row['id']));
printf("label = %s (%s)\n", $row['label'], gettype($row['label']));
答案 3 :(得分:0)
<强> MySQL的强>:
$result = mysql_query("SELECT * FROM table");
<强>的mysqli 强>:
$result = mysqli_query("SELECT * FROM table");
请注意您提供的示例不使用预先准备好的声明。
但是如果你想使用mysqli_stmt_bind_result
并且当时返回一个数组而不是一列,你可能需要将它包装成你自己的自定义函数。
好消息是bottom of the docs的例子很少,但它们是在OOP中,(时间要切换?)。
$result = mysql_query("SELECT * FROM table WHERE id = ?");
/* create a prepared statement */
if ($stmt = mysqli_prepare($link, "SELECT col1, col2 FROM table WHERE id = ?")) {
/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "s", $city);
/* execute query */
mysqli_stmt_execute($stmt);
/* fetch associative array */
while ($row = mysqli_fetch_assoc($stmt)) {
printf ("%s (%s)\n", $row["col1"], $row["col2"]);
}
}else{
echo "prepared failed";
}
答案 4 :(得分:0)
PHP的variable argument list (ellipsis) operator可以方便地动态替换已准备好的语句:
_tkinter.TclError: couldn't open "2.jpg": no such file or directory
>>> canv.create_image(20,20, anchor=NW, image=img)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'img' is not defined
一个例子:
mysqli_stmt_bind_param($stmt, $params_types, ...$params);
更好地结合到函数中
$sql = 'SELECT * FROM Table WHERE one = ? AND two = ?';
$params_types = 'ii';
$params = array(1, 2);
// prepare
$stmt = mysqli_stmt_init($dbconn_read);
if ( mysqli_stmt_prepare($stmt, $sql) ) {
// bind and execute
mysqli_stmt_bind_param($stmt, $params_types, ...$params); // unpack array into arguments
mysqli_stmt_execute($stmt);
// get column names for binding return results
$resultmeta = mysqli_stmt_result_metadata($stmt);
list($columns, $columns_vars) = array(array(), array());
while ( $field = mysqli_fetch_field($resultmeta) ) {
$columns[] = $field->name;
$columns_vars[] = &${'column_' . $field->name};
}
// call bind function with arguments in array
call_user_func_array('mysqli_stmt_bind_result', array_merge(array($stmt), $columns_vars));
// get return results
$return_array = array();
while ( mysqli_stmt_fetch($stmt) ) {
$row = array();
foreach ( $columns as $col ) {
$row[$col] = ${'column_' . $col}; // populate assoc. array with data
}
$return_array[] = $row; // push row data onto return array
}
}
这样称呼它:
function db_query(&$dbconn, $sql, $params_types, ...$params) { // pack dynamic number of remaining arguments into array
// GET QUERY TYPE
$query_type = strtoupper(substr(trim($sql), 0, 4));
$stmt = mysqli_stmt_init($dbconn);
if ( mysqli_stmt_prepare($stmt, $sql) ) {
mysqli_stmt_bind_param($stmt, $params_types, ...$params); // unpack
mysqli_stmt_execute($stmt);
if ( 'SELE' == $query_type || '(SEL' == $query_type ) {
$result = mysqli_stmt_result_metadata($stmt);
list($columns, $columns_vars) = array(array(), array());
while ( $field = mysqli_fetch_field($result) ) {
$columns[] = $field->name;
$columns_vars[] = &${'column_' . $field->name};
}
call_user_func_array('mysqli_stmt_bind_result', array_merge(array($stmt), $columns_vars));
$return_array = array();
while ( mysqli_stmt_fetch($stmt) ) {
$row = array();
foreach ( $columns as $col ) {
$row[$col] = ${'column_' . $col};
}
$return_array[] = $row;
}
return $return_array;
} // end query_type SELECT
else if ( 'INSE' == $query_type ) {
return mysqli_insert_id($dbconn);
}
return 1;
}
}