对于那些熟悉本书的读者,我正在阅读本书“Luke Welling Laura Thomson第四版的PHP和MySQL Web开发”第751页。然而,本书提供的解决方案使用MySQLi DB Connector,它在测试时工作正常。我想通过这个解决方案来使用我的一个使用PHP PDO连接器的项目,但是我试图得到与课本相同的结果。我正在寻求一些帮助来转换MySQLi连接器来处理PDO过程。这两个例子都使用MySQL DB。我不确定我做错了什么,并寻求一点帮助。如何将MySQLi连接器转换为PHP PDO
我试图让我的PDO程序产生相同的结果扩展数组作为原始文本书阵列做它的工作。
// Example taken from the text book
function expand_all(&$expanded) {
// mark all threads with children as to be shown expanded
$conn = db_connect();
$query = "select postid from header where children = 1";
$result = $conn->query($query);
$num = $result->num_rows;
for($i = 0; $i<$num; $i++) {
$this_row = $result->fetch_row();
$expanded[$this_row[0]]=true;
}
}
// The print_r form the text book example looks like this:
// result:
mysqli_result Object ( [current_field] => 0 [field_count] => 1
[lengths] => [num_rows] => 3 [type] => 0
)
// expended:
Array ([0] => 1) Array ([0] => 2) Array ([0] => 4)
//--------------------------------------------------------------//
// Know, here is my new adopted changes for using PHP PDO connector
function expand_all(&$expanded)
{
// mark all threads with children to be shown as expanded
$table_name = 'header';
$num = 1;
$sql = "SELECT postid FROM $table_name WHERE children = :num";
try
{
$_stmt = $this->_dbConn->prepare($sql);
$_stmt->bindParam(":num", $num, PDO::PARAM_INT);
$_stmt->execute();
$result = $_stmt->fetchAll(PDO::FETCH_ASSOC);
// get the $expanded children id's
foreach ($result as $key => $value)
{
foreach ($value as $k => $val)
{
$expanded[$k] = $val;
}
}
return $extended;
}
catch(PDOException $e)
{
die($this->_errorMessage = $e);
}
//close the database
$this->_dbConn = null;
}
// The print_r for the result looks like this:
Array ([0] => Array ([children_id] => 1)
[1] => Array ([children_id] => 2)
[2] => Array ([children_id] => 4)
)
// The return extended print_r for the children id's
// look like this:
Array ([children_id] => 4);
那么什么是你看到的区别? – 2015-02-05 23:11:17