If data exists in mysql database, use other data placeholder

What I want to do is display some data from the mysql database if it is installed by the user and if it is not installed, I just want to display some placeholder or "dummy" data. For example, if a user creates a profile, he will by default receive some random image or placeholder image as a profile image, and when he changes it, I want to display this image from the database. The same can be said about the user’s description or about me.

This is what I got so far

<?php 
$getInfo = $con->prepare("SELECT * FROM user_info WHERE userid=:userid");
$getInfo->execute(array(':userid'=>$userID));
$data = $getInfo->fetch(PDO::FETCH_ASSOC);

$description = $data['description'];
?>

<div class="container">
  <div class="row">
    <div class="col-sm-12">
      <h3 class="page-title">USER DESCRIPTION</h3>
      <?php if(!empty($description)) {
        echo '<p>' . $description . '</p>';
      } else { ?>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tenetur incidunt, at error. Provident veritatis ratione laborum modi laudantium, quidem inventore facilis at qui blanditiis, sunt tempore labore sit, eligendi libero?</p>
      <?php } ?>
    </div>
  </div>
</div>

, !empty($description), , , . mysql php, puropses. $userID - , , .

+4
2

, $data ['description'] . , , .

<?php 
$getInfo = $con->prepare("SELECT * FROM user_info WHERE userid=:userid");
$getInfo->execute(array(':userid'=>$userID));
$data = $getInfo->fetch(PDO::FETCH_ASSOC);

$description = !empty($data['description']) ? $data['description'] : 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tenetur incidunt, at error. Provident veritatis ratione laborum modi laudantium, quidem inventore facilis at qui blanditiis, sunt tempore labore sit, eligendi libero?';
?>

<div class="container">
  <div class="row">
    <div class="col-sm-12">
      <h3 class="page-title">USER DESCRIPTION</h3>
        <p>
          <?php echo $description; ?>
        </p>
    </div>
  </div>
</div>
+2

MySQL

. userid , 2 SELECT UNION

SELECT * FROM (
  SELECT * FROM j WHERE userid =99999
  UNION ALL
  SELECT 0,'NOOOOOOOOO'
  ) AS c ORDER BY userid DESC LIMIT 1;

MariaDB [tmp]> select * from j;
+--------+-------+
| userid | bame  |
+--------+-------+
|      0 | 222   |
|      1 | a     |
|     22 | b     |
|     47 | c     |
|    333 | d     |
|    334 | fff   |
|    335 | hhh   |
|   NULL | hallo |
+--------+-------+
8 rows in set (0.00 sec)

2

MariaDB [tmp]> SELECT * FROM (   SELECT * FROM j WHERE userid =1   UNION ALL   SELECT 0,'NOOOOOOOOO'   ) AS c ORDER BY userid DESC LIMIT 1;
+--------+------+
| userid | bame |
+--------+------+
|      1 | a    |
+--------+------+
1 row in set (0.00 sec)

MariaDB [tmp]> SELECT * FROM (   SELECT * FROM j WHERE userid =99999   UNION ALL   SELECT 0,'NOOOOOOOOO'   ) AS c ORDER BY userid DESC LIMIT 1;
+--------+------------+
| userid | bame       |
+--------+------------+
|      0 | NOOOOOOOOO |
+--------+------------+
1 row in set (0.00 sec)

MariaDB [tmp]>
+1

Source: https://habr.com/ru/post/1610321/


All Articles