如何在准备好的 SELECT 语句中检查 NULL 列,然后做一些事情?

这是当前代码,但它所做的只是从 1 行中选择列。


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

?>

如何为此添加 if 语句,以便如果 coverphoto 和/或 profilepic = null,手动将值分配给输出数组的 coverphoto 和 profilepic?我将在下面显示我的错误尝试:


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

?>


拉莫斯之舞
浏览 75回答 1
1回答

牛魔王的故事

我认为你只是有一些语法错误。<?phpsession_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){&nbsp; &nbsp;if ($result['coverphoto']){&nbsp; &nbsp; &nbsp;$coverphoto = 'coverphoto.jpg';&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp;if ($result['profilepic']){&nbsp; &nbsp; &nbsp;$profilepic = "profilepic.jpg";&nbsp; &nbsp; }else{$stmt->bind_result($uid,$coverphoto,$profilepic,$bio);while($stmt->fetch()) {&nbsp; &nbsp; $output=array(&nbsp; &nbsp; &nbsp; &nbsp; 'uid' => $uid,&nbsp; &nbsp; &nbsp; &nbsp; 'coverphoto' => $coverphoto,&nbsp; &nbsp; &nbsp; &nbsp; 'profilepic' => $profilepic,&nbsp; &nbsp; &nbsp; &nbsp; 'bio' => $bio&nbsp; &nbsp; );}}}}$json=json_encode($output);echo $json;$stmt->close();CloseCon($conn);?>你也可以这样做<?php<?phpsession_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()) {&nbsp; &nbsp; $output=array(&nbsp; &nbsp; &nbsp; &nbsp; 'uid' => $uid,&nbsp; &nbsp; &nbsp; &nbsp; 'coverphoto' => $coverphoto ? $coverphio : 'coverphoto.jpg',&nbsp; &nbsp; &nbsp; &nbsp; 'profilepic' => $profilepic ? $profilepic : 'profilepic.jpg',&nbsp; &nbsp; &nbsp; &nbsp; 'bio' => $bio&nbsp; &nbsp; );}$json=json_encode($output);echo $json;$stmt->close();CloseCon($conn);?>
打开App,查看更多内容
随时随地看视频慕课网APP