dtoka218420 2019-03-07 08:23
浏览 30
已采纳

我怎么能用PDO做mysqli查询?

I am try to build with recursive function that show option menu categories doing indent. It is working perfect in mysqli query, but how can i do that in PDO query?

For any helps thanks.

I am changed some lines and i also changed correct database connection to PDO, but not worked.

changed lines:

$dbc = $db->prepare("SELECT * FROM categories ORDER BY title");
$dbc->execute(array());

while (list($id, $parent_id, $category) = $dbc->fetchAll(PDO::FETCH_ASSOC)) {

My mysqli query need change to PDO:

$db = mysqli_connect("localhost","","","");

echo '<select name="parent_id">
      <option value="">Select</option>';

function make_list ($parent,$depth) {

    global $option;


    foreach ($parent as $id => $cat) {

        $whitespace = str_repeat(' - ', $depth * 1);
        echo '<option value="' . $cat['id'] . '">'. $whitespace . $cat['category'] . '</option>';

        if (isset($option[$id])) {

            make_list($option[$id], $depth+1);

        }
    }
}

$dbc = mysqli_query($db, "SELECT * FROM categories ORDER BY title");

$option = array();

while (list($id, $parent_id, $category) = mysqli_fetch_array($dbc, MYSQLI_NUM)) {

    $option[$parent_id][$id] =  array('category' => $category, 'id' => $id, 'parent_id' => $parent_id);

}
make_list($option[0], $depth = 0);

echo '</select>';

Here error messages:

Line 36 : foreach ($parent as $id => $cat) {

Line 56: while (list($id, $parent_id, $category) = $dbc->fetchAll(PDO::FETCH_ASSOC)) {

Line 58: $option[$parent_id][$id] = array('category' => $category, 'id' => $id, 'parent_id' => $parent_id);

Line 61: make_list($option[0], $depth = 0);

<select name="parent_id">
      <option value="">Select</option><br />
<b>Warning</b>:  Illegal offset type in <b>/Users/test/Documents/functions/pdo-optionmenu.php</b> on line <b>58</b><br />
<br />
<b>Notice</b>:  Undefined offset: 0 in <b>/Users/test/Documents/functions/pdo-optionmenu.php</b> on line <b>56</b><br />
<br />
<b>Notice</b>:  Undefined offset: 1 in <b>/Users/test/Documents/functions/pdo-optionmenu.php</b> on line <b>56</b><br />
<br />
<b>Notice</b>:  Undefined offset: 2 in <b>/Users/test/Documents/functions/pdo-optionmenu.php</b> on line <b>56</b><br />
<br />
<b>Notice</b>:  Undefined offset: 0 in <b>/Users/test/Documents/functions/pdo-optionmenu.php</b> on line <b>61</b><br />
<br />
<b>Warning</b>:  Invalid argument supplied for foreach() in <b>/Users/test/Documents/functions/pdo-optionmenu.php</b> on line <b>36</b><br />
</select>
  • 写回答

1条回答 默认 最新

  • douquan2023 2019-03-07 08:45
    关注

    As you have no insecure data in your query, there's no need to use prepare, use simple query function:

    $dbc = $db->query("SELECT * FROM categories ORDER BY title");
    

    Next, fetchAll fetches all results immediately. You need to fetch row by row. In case of query, this can be done as

    foreach ($dbc as $row) {
        print_r($row);
    }
    

    Or with fetch method:

    while ($row = $dbc->fetch()) {
        print_r($row);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算