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


Selecting ID from url returns NULL data

我有函数page和details.php。

功能页面:

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);

当我在home.php中时,我按下一张图片,然后被重定向到details.php。当我在那里的时候,我在我的url中得到了ID,然后我var dump,但得到了一个NULL。

正因为如此,我在details.php中的代码不起作用:

while ($row = $result) {
    <tr>
               <td><?php echo $row['merk']; ?> </td>

我真的很感激你的帮助。因为我不知道如何让它发挥作用。所以基本上我不会在详细信息页面上看到任何内容。除了var_dump的NULL之外。

您执行的是showDetails而不是getDetails?此外,函数getDetails不会返回任何内容。我怀疑您必须执行getDetails并在函数中放入一个return才能使其工作。

功能页面:

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()只返回一个关于它是否成功的布尔值。您需要使用get_result()来获得查询的实际结果