duanhuancong1969 2016-04-12 09:34
浏览 12
已采纳

从url中选择ID将返回NULL数据

I have functions page and details.php.

Functions Page:

function getDetails(){
    $con = new Core();
    $con->connect();    
    $param = $_GET['autoid'];
    $sql = 'SELECT * FROM auto WHERE autoid=?';
    $stmt = $con->myconn->prepare($sql);
    $stmt->bind_param("i", $param);
    $stmt->execute();
}

home.php:

    <?php
        $details = new Details();
        $result = $details->showDetails();
        while ($row = mysqli_fetch_assoc($result)) {
        $id=$row['autoid'];
    ?>
    <tr>
        <td><?php echo '<a href="details.php?autoid='.$id.'">
            <img src="data:image/jpeg;base64,'.base64_encode($row['foto'] ).'"/></a>'; ?> 
        </td>
    </tr>
   <?php }
    mysqli_free_result($result); 
    ?>

details.php:

$details = new Details();
$result = $details->getDetails();
var_dump($result);

When I'm in my home.php I press on a picture and get redirected to details.php. When I'm there I got the ID in my url and then I var dump but get returned a NULL.

And because of that my code in details.php doesn't work :

while ($row = $result) {

    <tr>
               <td><?php echo $row['merk']; ?> </td>

I'd really appreciate some help. Since I can't figure how to let it work. So basically I don't get anything shown on the details page. Besides a NULL of the var_dump.

  • 写回答

1条回答 默认 最新

  • dongpeihui1051 2016-04-12 10:11
    关注

    You're executing showDetails and not getDetails? Also your function getDetails doesn't return anything. I suspect you have to execute getDetails and put a return in the function to make it work.

    Functions Page:

    function getDetails(){
        $con = new Core();
        $con->connect();    
        $param = $_GET['autoid'];
        $sql = 'SELECT * FROM auto WHERE autoid=?';
        if(!($stmt = $con->myconn->prepare($sql))) {
            echo "Prepare failed: (" . $con->myconn->errno . ") " . $con->myconn->error;
        }
        if(!($stmt->bind_param("i", $param))) {
            echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
        }
        if(!($stmt->execute()) {
            echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
        }
        if(!($result = $stmt->get_result()) {
            echo "Getting result set failed: (" . $stmt->errno . ") " . $stmt->error;
        }
        return $result; // added return here
    }
    

    home.php:

    $details = new Details();
        $result = $details->getDetails(); //Changed this to getDetails
        while ($row = mysqli_fetch_assoc($result)) {
        $id=$row['autoid'];
        ?>
    <tr>
        <td><?php echo '<a href="details.php?autoid='.$id.'"><img src="data:image/jpeg;base64,'.base64_encode($row['foto'] ).'"/></a>'; ?> </td>
        </tr>
    

    execute() only returns a Boolean on whether it was successful or not. you need to use get_result() to get the actual results of the query

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 socket通信实现多人聊天室疑惑
  • ¥15 DEV-C++编译缺失
  • ¥33 找熟练码农写段Pyhthon程序
  • ¥100 怎么让数据库字段自动更新
  • ¥15 antv g6 力导向图布局
  • ¥15 quartz框架,No record found for selection of Trigger with key
  • ¥15 锅炉建模+优化算法,遗传算法优化锅炉燃烧模型,ls-svm会搞,后面的智能算法不会
  • ¥20 MATLAB多目标优化问题求解
  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数