0

I have a news box that I only want to show if the number of rows is not existing or there is one row inserted. If there is two rows, I'm showing something else.

<?php

$getUsers = mysql_query("SELECT * 
                        FROM users 
                        WHERE id = '".$_SESSION['user']['id']
                        ."' Order by id ASC"); 
while($usersinfo = mysql_fetch_array($getUsers)) 
{

    $getProject = mysql_query("SELECT * 
                                FROM `cms_prosjekt` 
                                WHERE code = '".$usersinfo['motto']
                            ."' Order by id DESC");
    while($projectinfo = mysql_fetch_array($getProject))  
    {     
        $result = mysql_query("SELECT * 
                                FROM cms_news_front 
                                WHERE userid = '".$projectinfo['userid']
                            ."' AND code = '".$usersinfo['motto']
                            ."' OR userid = '".$projectinfo['userid']
                            ."' AND code = 'alle' 
                                Order by title ASC 
                                LIMIT 0, 1");
        if(mysql_num_rows($result) == 1 || 0)
        {
            echo'<ul class="grid cs-style-3">
                     <li style="margin-top:-15px;">
                         <div id="box">Ingen nyhet opprettet2.</div>
                         <img src="http://media.istockphoto.com/photos/blue-background-picture-id518094392?k=6&m=518094392&s=170667a&w=0&h=jFq7AAr7Uu2yyTBEtyjAbV477WgWwXCrWgDD5zLz4UU=" alt="img01" style="max-width:100%; height:270px; width:100%; border-radius:5px;"></a>

                         </figure>
                     </li>
                 </ul>';

        }
    }
}
?> 

The main problem is that when I have two rows inserted, it still shows the ouput from the above query. I only want to show it when I have no inserts and one inserted row.

Any suggestions?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
pledgeco
  • 21
  • 3
  • Side notes: don't' use `mysql()` function :http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php - use `mysqli` or `pdo` instead. Also the HTML doesn't match - suggest tidying that up as it may confuse what the problem is. – Robbie Dec 07 '16 at 23:25
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Dec 07 '16 at 23:28
  • 1
    I think you will need to add some brackets into the `WHERE .. AND .. OR .. AND` query condition to get what you actually want from it – RiggsFolly Dec 07 '16 at 23:35

2 Answers2

1

if(mysql_num_rows($result) == 1 || 0)

literally means

if(mysql_num_rows($result) == 1 || false)

which is semantically the same as

if(mysql_num_rows($result) == 1)

What you probably want is

if(mysql_num_rows($result) == 1 || mysql_num_rows($result) == 0)

but could be represented more easily as

if(mysql_num_rows($result) < 2)

Robbie
  • 17,605
  • 4
  • 35
  • 72
0

first of all separate and group the conditions of last sql statement. and post if you are getting result to $result variable every time or not.

BetaDev
  • 4,516
  • 3
  • 21
  • 47
  • Can you expand on this? There's obviously some thinking/logic (that's much clearer than the typo correction I posted) but it's not clear as to what you mean. – Robbie Dec 07 '16 at 23:46
  • I mean i think the last sql statement , need to group the AND, OR condition using '()' and remove the LIMIT and then see the result. This is what i meant. – BetaDev Dec 08 '16 at 00:09
  • Or just simplify it as the `userid` bit is there twice. so should be `WHERE userID= AND (code = OR code = )` . You should add that to your answer to make it more helpful ;) – Robbie Dec 08 '16 at 00:13