2017-09-01 79 views
0
$id = $_REQUEST['id']; 

$stmt = $conn->prepare('SELECT * FROM test WHERE id = ?'); 
echo $id; 
$stmt->bind_param('s', $id); 

$stmt->execute(); 

// This is line 12 
while ($row = $stmt->fetch()) { 
    $test = $row['test']; 
} 

好的,我们走吧。此代码不能正常工作,因为我基本上我得到告诉我下面准备声明不提取数据

#0 /example/example.php(2): require() 
#1 {main} 
    thrown in /example/example.inc.php on line 12 

我不知道我做错了一个错误,但我已经尝试过bind_result()$stmt->fetch_assoc()未工作过。我在这里读了很多其他问题,但他们没有帮助我。

这里是

<? 
$servername = "exase"; 
$username = "exaus"; 
$password = "exapw"; 
$dbname = "exa_db"; 

$conn = new mysqli($servername, $username, $password, $dbname); 

if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

$conn = new mysqli($servername, $username, $password, $dbname); 

if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 
+0

什么是您的连接是什么样子? –

+1

什么是实际的错误信息? (堆栈跟踪上方的行) – rickdenhaan

+0

您应该将参数绑定为int,而不是字符串。 '$ stmt-> bind_param('i',$ id);'不知道这是否能解决您的问题,但目前您还没有做好。 – GrumpyCrouton

回答

0

当使用的MySQLi准备好的语句,它比使用标准的查询有一点不同。您需要使用mysqli_stmt::bind_result(),或将结果集存储为mysqli_stmt::get_result(),然后才能在获取结果时使用数据。请注意,在使用get_result()之前,您需要MySQL本机驱动程序mysqlnd - 否则您需要手动将每列与bind_result()绑定。

下面是如何使用bind_result()的示例。请注意,您需要绑定查询中的列数,并且由于您执行的操作是SELECT *,因此您需要绑定表中的所有内容 - 但是,如果稍后向表中添加列,该方法将失败。因此,最好只选择您需要的列,如SELECT id, test FROM..

$id = $_REQUEST['id']; 

$stmt = $conn->prepare('SELECT test, id FROM test WHERE id = ?'); 
$stmt->bind_param('s', $id); 

$stmt->execute(); 
$stmt->bind_result($test, $result_id); 
$stmt->fetch()); 

/* 
* If there was any matching rows, the variables defined in bind_param() 
* now hold the values 
* You can then use '$test' and '$result_id' 
*/ 
echo $test; 

$stmt->close(); 

不过,若你已经安装了MySQL机驱动程序,您可以使用get_result(),并把它作为一个“正规”的查询。那么,如果你做SELECT *(尽管我不建议你选择所有的东西 - 你应该选择你需要的列,除此以外)并不重要。

$id = $_REQUEST['id']; 

$stmt = $conn->prepare('SELECT * FROM test WHERE id = ?'); 
$stmt->bind_param('s', $id); 

$stmt->execute(); 
$result = $stmt->get_result(); // $result is now an object of MySQLi resource, and not MySQLi statement 
           // It can now be used as as the result-set of a regular query 

$row = $result->fetch_assoc()); 
$test = $row['test']; 
echo $test; 

$stmt->close(); 
+0

一件事我不明白的是,为什么我必须做出而如果已经有了在bind_result的结果,或者是不是这样呢? – onebooredguy

+0

因为这只会结合的结果每次取'()'被调用(如果您有多个行)。它们在获取之前不包含价值。如果你只是有一排匹配的ID,你可以删除'while',但你仍然需要'取()'它()在'bind_result定义的变量之前'持有从数据库中的任何值。 – Qirel

+0

id列有一个主键,以便它是独一无二的,但我怎么能取吗? – onebooredguy

0

你没有做准备的语句正确的连接,请参阅本:

$id = $_REQUEST['id']; 

    $stmt = $conn->prepare('SELECT col1,col2,col3 FROM test WHERE id = ?'); 
    $stmt->bind_param('i', $id); 
    $stmt->execute(); 
    $stmt->bind_result($col1,$col2,$col3); // you need to bind the result when using prepared statements 
    $stmt->store_result(); // store it so if the number of rows retrieved is large they won't be dropped during loop or cause the overload error 


    while ($stmt->fetch()) { 
     $test = $col1; 
     echo $col2; 
    } 

$stmt->close(); 
+0

我需要所有列 – onebooredguy

+0

是的,添加它们,你不应该选择* – clearshot66

+0

选择*有什么问题? – GrumpyCrouton