Results 1 to 6 of 6

Thread: Database with Column "Approved"

  1. #1
    Join Date
    Jun 2009
    Posts
    62
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Database with Column "Approved"

    So I have a database,
    id | title | approved
    1 | title1 | yes
    2 | title2 | yes
    3 | title3 | no

    Then I have this PHP script:
    PHP Code:
    <?php
    //opens connection to mysql server
    $dbc mysql_connect("localhost""username""password");
    if (!
    $dbc)
        {
        die(
    'Not connected :' mysql_error());
        }
    //select database
    $db_selected mysql_select_db("mydatabase"$dbc);
    if (!
    $db_selected)
        {
        die(
    "cant connect :" mysql_error());
        }

    $result mysql_query("SELECT * FROM mytable ORDER BY title");

    while(
    $row mysql_fetch_array($result))
      {
      echo 
    "" $row['title'] . "";
      echo 
    "<BR>";
      }
    mysql_close($dbc);
    ?>
    That displays each Title. How do I make it so that only approved=yes titles show? Thanks in advanced for any help.

  2. #2
    Join Date
    Feb 2008
    Posts
    90
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default

    Try this...
    Code:
    	
    $result = mysql_query("SELECT * FROM mytable ORDER BY title WHERE approved='yes'");

  3. The Following User Says Thank You to SChaput For This Useful Post:

    onestopplay (06-30-2009)

  4. #3
    Join Date
    Jun 2009
    Posts
    62
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    One more thing...
    what if I want to say
    WHERE approved='yes'

    AND somethingelse='something'

    Can I say AND?

  5. #4
    Join Date
    Feb 2008
    Posts
    90
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default

    Yes you can.

    Code:
    $result = mysql_query("SELECT * FROM mytable ORDER BY title WHERE approved='yes' AND another='no'");

  6. The Following User Says Thank You to SChaput For This Useful Post:

    onestopplay (07-01-2009)

  7. #5
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    ei.. w8 a minute... I think it's not possible that in your query, the order by will be put first then the where clause... Because there is a sequence which clause will be put first.... I think the complete (not a complete one, complete only in the clause part) syntax of the select statement is:

    Select * from Table_name
    [where clause]
    [group by]
    [having]
    [order by]
    [limit];

    You must follow the sequence else it will generate an error.. though you can omit some of the clauses, like I will only use the [order by] clause, But if you use the [order by] clause, make sure that the [where],[group by],and [having] clause is not put in the same select statement afterward.

  8. The Following User Says Thank You to heavensgate15 For This Useful Post:

    onestopplay (07-02-2009)

  9. #6
    Join Date
    Jun 2009
    Posts
    62
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    So I was working on my site today and I kept getting this error message that was really annoying me on most of my scripts. Finally, I fixed it by doing WHERE first, then ORDER BY. Thanks for your help.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •