How to check for NULL column, then do something, in prepared SELECT statement?

James Bargon :

Here is the current code, but all it does is select the columns from 1 row.

<?php
session_start();
include 'sqlconnection.php';
$conn = OpenCon();

$stmt = $conn->prepare('SELECT uid,coverphoto,profilepic,bio FROM profile WHERE uid = ?');
$stmt->bind_param('i', $_SESSION['uid']);
$stmt->execute();
$stmt->bind_result($uid,$coverphoto,$profilepic,$bio);
while($stmt->fetch()) {
    $output=array(
        'uid' => $uid,
        'coverphoto' => $coverphoto,
        'profilepic' => $profilepic,
        'bio' => $bio
    );
}
$json=json_encode($output);
echo $json;
$stmt->close();
CloseCon($conn);
?>

How do I add an if statement to this so that if coverphoto and/or profilepic = null, manually assign the value to coverphoto and profile pic for the output array? I will show my incorrect attempt below:

<?php
session_start();
include 'sqlconnection.php';
$conn = OpenCon();

$stmt = $conn->prepare('SELECT uid,coverphoto,profilepic,bio FROM profile WHERE uid = ?');
$stmt->bind_param('i', $_SESSION['uid']);
if ($stmt->execute()){
   if (coverphoto=null){
     $coverphoto = 'coverphoto.jpg';
    }else{
     if (profilepic=null){
     $profilepic = "profilepic.jpg";
    }else{
$stmt->bind_result($uid,$coverphoto,$profilepic,$bio);
while($stmt->fetch()) {
    $output=array(
        'uid' => $uid,
        'coverphoto' => $coverphoto,
        'profilepic' => $profilepic,
        'bio' => $bio
    );
}
}
}
}
$json=json_encode($output);
echo $json;
$stmt->close();
CloseCon($conn);
?>
user3585659 :

I think you just have some syntax error.

<?php
session_start();
include 'sqlconnection.php';
$conn = OpenCon();

$stmt = $conn->prepare('SELECT uid,coverphoto,profilepic,bio FROM profile WHERE uid = ?');
$stmt->bind_param('i', $_SESSION['uid']);
$result = $stmt->execute();
if ($result){
   if ($result['coverphoto']){
     $coverphoto = 'coverphoto.jpg';
    }else{
     if ($result['profilepic']){
     $profilepic = "profilepic.jpg";
    }else{
$stmt->bind_result($uid,$coverphoto,$profilepic,$bio);
while($stmt->fetch()) {
    $output=array(
        'uid' => $uid,
        'coverphoto' => $coverphoto,
        'profilepic' => $profilepic,
        'bio' => $bio
    );
}
}
}
}
$json=json_encode($output);
echo $json;
$stmt->close();
CloseCon($conn);
?>

You can also do it this way

<?php
<?php
session_start();
include 'sqlconnection.php';
$conn = OpenCon();

$stmt = $conn->prepare('SELECT uid,coverphoto,profilepic,bio FROM profile WHERE uid = ?');
$stmt->bind_param('i', $_SESSION['uid']);
$stmt->execute();
$stmt->bind_result($uid,$coverphoto,$profilepic,$bio);
while($stmt->fetch()) {
    $output=array(
        'uid' => $uid,
        'coverphoto' => $coverphoto ? $coverphio : 'coverphoto.jpg',
        'profilepic' => $profilepic ? $profilepic : 'profilepic.jpg',
        'bio' => $bio
    );
}
$json=json_encode($output);
echo $json;
$stmt->close();
CloseCon($conn);
?>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=169100&siteId=1