Mysqli query without any results doing something else something else

I am running a mysqli query.

this is my code

<?php $SM_pro_info="SELECT * FROM special_marketing_ads ORDER BY id desc LIMIT 4"; $QSM_pro_info = $db->query($SM_pro_info)or die($db->error); if($QSM_pro_info->num_rows > 0){ while($SM_pro=$QSM_pro_info->fetch_object()){ ?> <table width="208" border="0"> <tr> <td width="129" height="35" align="right"><span style="color:#361800; font-size:14px; font-weight:bold;"><?php echo $SM_pro->pro_title; ?></span></td> <td width="69" rowspan="2" align="center"><a rel="lightbox" href="includes/Cpanel/projectImages//images.jpg" ><img src="<?php echo $SM_pro->image_1; ?>" alt="" width="60" height="60" border="0" /></a></td> </tr> <tr align="right"> <td><span style="color:#361800; font-size:14px; font-weight:bold;">الغرض</span> : <span style="color:#da6e19; font-size:15px; font-weight:bold;"><?php echo $SM_pro->pro_purpose; ?></span></td> </tr> </table> <?php } }else{ for ($x=0; $x<(4 - $QSM_pro_info->num_rows); $x++) { echo 'Your ad here'; // or, whatever your ad text is } } ?> </div> 

now all i want to do is if there are any results coming from mysqli, echo if there are no results it needs an echo image that adds your ads here. The fact is that I have 4 ads, so if there are 1 Ads in the mysqli line and the rest are empty, so I want this echo, this is one ad, and the third ad should be this image, add your ads here. any help on this?

+4
source share
1 answer

Change this:

 if($QSM_pro_info->num_rows ==1){ 

For this:

 if ($QSM_pro_info->num_rows > 0) { 

The problem is that you checked exactly one result. What you want is more than zero results.

Now, in the part where you display "your ads here", try the following:

 for ($x=0; $x<(4 - $QSM_pro_info->num_rows); $x++) { echo 'Your ad here'; // or, whatever your ad text is } 

Also remember to use htmlspecialchars() around your HTML variable data to avoid broken HTML (and possible XSS attacks, depending on your data source).

+3
source

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


All Articles