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.
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