为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
拉莫斯之舞 2022-10-22 16:50:34
这是当前代码,但它所做的只是从 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']);$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?我将在下面显示我的错误尝试:<?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']);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);?>
查看完整描述

1 回答

?
牛魔王的故事

TA贡献1830条经验 获得超3个赞

我认为你只是有一些语法错误。


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

?>

你也可以这样做


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

?>


查看完整回答
反对 回复 2022-10-22
  • 1 回答
  • 0 关注
  • 74 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信