2014-01-05 15 views
0

我正在使用MySQLi连接,并且我有下面的代码,我试图从数据库表中定义库存ID所定义的行的所有列,但是我无法得到这个工作。我是否完全错误的方式?我希望能够在选择查询之后的代码中使用$row['general_cleanliness']之类的内容。mysqli的问题全部选中

$getScheduleCondition = $db->prepare("SELECT * FROM inventories WHERE inventory_id = ? LIMIT 1",)){ 
$getScheduleCondition->bind_param("i", $inventory); 
$row = $getScheduleCondition->fetch_array(); 
$getScheduleCondition->close(); 

$inventory被定义为一个数字,我知道这工作。任何帮助表示赞赏,对不起,如果这似乎是一个基本问题,只是有点混乱!

编辑: 给大家告诉我,我使用的PDO,我不是:

$db = new mysqli('localhost', 'cl52-system', 'PASS', 'cl52-system'); 
if($db->connect_errno > 0){ 
    die('Unable to connect to database [' . $db->connect_error . ']'); 
} 
+0

“选择数据库中的所有行” - 但为什么在你的代码中,你通过id选择并设置限制1? – sergio

+0

你永远不会执行查询(http://www.php.net/manual/en/mysqli-stmt.execute.php) – Sumurai8

+0

@sergio对不起,我的意思是专栏! –

回答

0

你忘了两件事。首先你需要执行查询。您可以使用your_stmt_object->execute()docs)执行此操作。虽然您的stmt对象没有方法fetch_array(),但您首先需要使用->get_result()docs)将mysqli_result拉出来,然后可以根据需要使用fetch_array()

$getScheduleCondition = $db->prepare("SELECT * FROM inventories WHERE inventory_id = ? LIMIT 1"); 
$getScheduleCondition->bind_param("i", $inventory); 
$getScheduleCondition->execute(); //You need to execute the query 
$row = $getScheduleCondition->get_result()->fetch_array(); //From stmt to mysql_result to array 
$getScheduleCondition->close(); 

如果您不需要使用数组,它可能更容易->fetch()docs)和->bind_result()docs),而不是使用。

$getScheduleCondition = $db->prepare("SELECT * FROM inventories WHERE inventory_id = ? LIMIT 1"); 
$getScheduleCondition->bind_param("i", $inventory); 
$getScheduleCondition->bind_result($column1, $column2, $column3, $etc); 
$getScheduleCondition->execute(); //You need to execute the query 
$getScheduleCondition->fetch(); //Now the first row is in the variables defined above 
echo $column1; //<-- see? 
$getScheduleCondition->close(); 
+0

我的旧查询使用了bind_result。可能有很多列,我想用select *来简化它。我会尝试你的第一个建议 –

+0

我试过第一个选项,现在它只是杀死我的脚本。有任何想法吗? –

+0

起诉谋杀。另外找出它给出了什么错误。如果它没有给出可见的错误(并且你有PHP错误),'var_dump'包含所有变量,看看是否有一个或多个给出奇怪的结果。如果第一行提供了一个错误(准备语句),请手动对数据库执行查询以查看是否有任何mysql错误。或者,如果您在那个时刻之前完成了准备好的查询,那么该查询可能没有被正确关闭。 – Sumurai8

-2

你的传球被确定为参数“I”(命名参数),但在你的查询只?,将您的bind_param改为“:i”并更改?到:我在你的查询。

$getScheduleCondition = $db->prepare("SELECT * FROM inventories WHERE inventory_id = :i LIMIT 1",)){ 
    $getScheduleCondition->bind_param(":i", $inventory, PDO::PARAM_INT); 
    $getScheduleCondition->execute(); // based on Sumurai8 comment, this is missing 
    $row = $getScheduleCondition->fetch_array(); 
    $getScheduleCondition->close(); 

另外,PDO:PARAM_INT明确地将该参数设置为整​​数。

+0

连接工作正常,因为我曾经使用的罚款,我试图简化我的代码,我不相信这是答案,但我会尝试它 –

+0

它被设置为一个整数,即时通讯使用mysqli –

+0

Updated问题,我正在使用mysqli –