0

Evening,

I have piece of code:

for ($i=0; $i < $c; $i++){
    $result = mysql_query("SELECT ad_id,title FROM `ads` WHERE `ad_id`='".$new[$i]."' LIMIT 10");
    if(mysql_num_rows($result) > 0){
        while($row = mysql_fetch_array($result)){
            if(($c > 3) && ($i < 2)){
                echo '
                <div class="pins best">
                     <div class="holder">

                         '.$row['title'].'
                     </div>
                </div>';
            } else {
                echo '
                <div class="pins">
                     <div class="holder">
                         '.$row['title'].'
                     </div>
                </div>';
            }
        }
    } else {
        echo "No results";
    }
}

Now what i am doing is checking each id which i have in $new array with id coresponding in db.

What I can not accomplish is to output some message to user if there are no results matching db records. I tried to write after an if statement, else statement, but it doesn't work because this db selection is in for loop.

Any suggestions?

EDITED: I added the else statement to my code,because everyone suggesting what I already tried, and that's where the problem is.

4
  • 1
    Do not use mysql_query. It will be removed in the future: php.net/manual/en/function.mysql-query.php
    – Daniel
    Jan 22, 2015 at 3:31
  • Yes, I know that,Daniel. The reason why I am using, because I don't really know really good mysqli yet and this is like a prototype of my project, after I will have all piecies working together I will make it with mysqli Jan 22, 2015 at 9:28
  • 1
    You should follow some tutorials on PDO. Why waste your energy on the old school. It will take some effort, but after that you'll love it. I would suggest catching the id that fails in another array. See below.
    – Daniel
    Jan 22, 2015 at 18:18
  • I did some project with PDO and to be honest I hated it, it is way too confusing for me, but I will have to learn it anyway, but for me doing with mysql is like making a sketch..After all stuff works, I simply conwert all the code to mysqli Jan 22, 2015 at 19:39

4 Answers 4

0

Did you try

if(mysql_num_rows($result) > 0){
...
}
else {
    //message here 
}
4
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. Jan 22, 2015 at 7:09
  • yes its an answer, phrased as a question, thx for ur input tho
    – makapaka
    Jan 22, 2015 at 8:46
  • Yes I did,I mentioned it in my question as well Jan 22, 2015 at 9:21
  • well you haven't been very clear - "does not work" , does not tell us anything
    – makapaka
    Jan 22, 2015 at 10:15
0

Your code is 99% there:

    for ($i=0; $i < $c; $i++) { 
        $result = mysql_query("SELECT ad_id,title FROM `ads` WHERE `ad_id`='".$new[$i]."' LIMIT 10");
        if(mysql_num_rows($result) > 0){
            while($row = mysql_fetch_array($result)){
                if(($c > 3) && ($i < 2)){
                     echo '
                            <div class="pins best">
                                 <div class="holder">

                                     '.$row['title'].'
                                 </div>
                            </div>
                        ';
                } else {
                echo '
                        <div class="pins">
                             <div class="holder">

                                 '.$row['title'].'
                             </div>
                        </div>

                    ';

                }

            }
        }
        else
        {
            echo 'No results found';
        }

    }

The while loop never gets executed at all.

0
0

You can collect the ones that fail in an array$failedId, after that you can do something with that collection and iterate your $new array to generate the feedback.

$failedId = array();

for ($i=0; $i < $c; $i++){
    $result = mysql_query("SELECT ad_id,title FROM `ads` WHERE `ad_id`='".$new[$i]."' LIMIT 10");
    if(mysql_num_rows($result) > 0){
        while($row = mysql_fetch_array($result)){
            if(($c > 3) && ($i < 2)){
                echo '
                <div class="pins best">
                     <div class="holder">

                         '.$row['title'].'
                     </div>
                </div>';
            } else {
                echo '
                <div class="pins">
                     <div class="holder">
                         '.$row['title'].'
                     </div>
                </div>';
            }
        }
    } else {
        $failedId[] = $i;
    }
}
0

Well I sorted out myself. If someone is interesting, after I make an array with id's which are matching the search word in database, I simply check if array or result is empty (that means no match was found) and then print out the message and this check is done before given code above. Thanks for those who took their time to help me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.