Hey, I'm running a mysqli query, and would like to run a script if the query returns no results.
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);

        $SM_pro=mysqli_fetch_array($QSM_pro_info);
        if($SM_pro){
            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;">pour</span> : <span style="color:#da6e19; font-size:15px; font-weight:bold;"><?php echo $SM_pro->pro_purpose; ?></span></td>
            </tr>
          </table>
          <?php
            }
                }else{
                echo"done";
            }
          ?>

now all I want to do is if there is any results comeing from mysqli echo it if there is no results it should
echo an image which is add your ads here. the point is I have 4 Ads so if there is 1 Ads in mysqli row and the others is empty so I want it to echo this single Ads and the other 3 Ads should be this image add your Ads here.
any Help on this please?

Recommended Answers

This code:

if($SM_pro){

Basically is saying if SM_pro == true, however, DM_pro is an array, so, therefore it won't return true/false. You could try:

if($SM_pro <= 0) {

Alternatively, you could use the mysqi_num_rows() function..

Jump to Post

All 3 Replies

This code:

if($SM_pro){

Basically is saying if SM_pro == true, however, DM_pro is an array, so, therefore it won't return true/false. You could try:

if($SM_pro <= 0) {

Alternatively, you could use the mysqi_num_rows() function.. http://www.nusphere.com/kb/phpmanual/function.mysqli-num-rows.htm

Hope this helps :)

thanks phorce for replay yes it was very helpfully but my question was how make it preint "put your ads here" if there is no results and they should be 4 if there is 2 records in my table so it should print the two records and the other two "put your ads here"

here you are what I done for this.

<?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 style="border:1px #9D8B64 solid; margin-top:5px; background:#fcf3df;" width="208" border="0" cellpadding="0" cellspacing="0">
            <tr>
              <td width="129" height="30" 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="includes/Cpanel/<?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
            }
            }
                for ($x=0; $x<(4 - $QSM_pro_info->num_rows); $x++) {
                echo "<img style='margin-top:5px;' src='images/SM_empty.gif' width='206' height='50' />";
                }
          ?>

and the good news it worked fine with me.
Thanks again for the replay.
and regards

I don't understand what you're trying to say? Erm, screenshot / example?

I'm guessing you mean, you have 4 places for advertisements and you want to display the advertisement if there is a record, if there isn't then it leaves it blank? So, if there is 2 advertisements, then, there would it would display "place your advertisement here" etc.. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.