echo结果作为bind_param的一部分

时间:2015-07-22 15:41:32

标签: php mysqli

我正在尝试使用此代码来回显一些信息,这些信息需要有一个bind_param才能生成数据。我需要对此代码进行哪些更改才能使其正常工作?

function get_header_link($sql, $image)
{
    include 'connect.php';

    $id = $_GET['id'];
    $select = $conn->prepare($sql);
    $select->bind_param('s', $id);
    if ($result = $select->execute()) {
        foreach($select as $value => $row) {
            echo "<h4>" . $row['display'] . "</h4>\n
                    <div class='placeholder' id='large'>\n
                        <img src='" . $row[$image] . "'/>\n
                        <div class='name'>
                            <h1>" . $row['display'] . "</h1>\n
                        </div>
                  </div>";
        }
    }
}

在运行上面的代码时,使用:

get_header_link("SELECT * FROM homelinks WHERE linkID=?", "large_image");

我不再收到错误,数据库中没有数据正在打印。

2 个答案:

答案 0 :(得分:0)

阅读this后,您应该使用mysqli,如下所示:

function get_header_link($sql, $image)
{
    include 'connect.php';

    $id = $_GET['id'];
    $select = $conn->prepare($sql);
    $select->bind_param('s', $id);
    $select->execute();
    $select->store_result();
    $meta = $select->result_metadata();
    while ($field = $meta->fetch_field()) {
        $params[] = & $row[$field->name];
    }

    // bind all columns in $row
    call_user_func_array(array(
        $select,
        'bind_result'
    ) , $params);

    while ($select->fetch()) {
        echo "<h4>" . $row['display'] . "</h4>\n
                <div class='placeholder' id='large'>\n
                    <img src='" . $row[$image] . "'/>\n
                    <div class='name'>
                        <h1>" . $row['display'] . "</h1>\n
                    </div>
                </div>";
    }

    /* close statement */
    $select->close();
}

答案 1 :(得分:0)

function get_header_link($sql)
{
    include 'connect.php';

    $id = $_GET['id'];
    $select = $conn->prepare($sql);
    $select->bind_param('s', $id);
    $select->execute();
    $meta = $select->result_metadata();
    while ($field = $meta->fetch_field()) {
        $params[] = & $row[$field->name];
    }

    call_user_func_array(array(
        $select,
        'bind_result'
    ) , $params);
    while ($select->fetch()) {
        echo "<h4>" . $row['display'] . "</h4>\n
            <div class='placeholder' id='large'>\n
                <img src='" . $row['large_image'] . "'/>\n
                <div class='name'>
                    <h1>" . $row['display'] . "</h1>\n
                </div>
          </div>";
    }

    $select->close();
}