SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql database validation

    Okay...

    I am trying to write a php script that queries a mysql database and checks whether a specific value exists in a table of a database. The code I want to use:

    Code:
    if (mysql_query ("SELECT name FROM page WHERE name = 'highschool'")):
    //If true
    else:
    //If false
    endif;
    The problem is no matter what I put in place of 'highschool', it always turns out true, even though there is only 'highschool', 'middleschool', and 'elementary' in the database. Am I doing it wrong? What can I do differently? I don't need to get any information back from the database other than whether the specific field exists.

    Please help!

    -Spirit

  2. #2
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Spirit -- you cant test mysql_query for success/failure of a where clause in a query. You want to test the number of returned rows in your case above. mysql_query returns true for every thing except an error. 0 rows returned in the result set from the query still equals true being returned by this function.
    Erh

  3. #3
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mandibal
    Spirit -- you cant test mysql_query for success/failure of a where clause in a query. You want to test the number of returned rows in your case above. mysql_query returns true for every thing except an error. 0 rows returned in the result set from the query still equals true being returned by this function.
    I'm sure I can, but is it possible to test that with an if() cs?

    What I am doing is I have a url, such as /highschool/athletics/football/, and I need to test whether football is a page in the database or a section of the category athletics in the highschool site. The script will go through multiple
    if()'s, and determine the nature of the request by what the database contains. If I can't do this in the if(), then I will have to do five queries, $results0, $results1, $results2, $results3, $results4, and change my if control structures accordingly.

    (Spirit)

  4. #4
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $result mysql_query ("SELECT name FROM page WHERE name = 'highschool'")
    if (
    mysql_num_rows($result) > 0)
    {
    //true
    }else{
    //false

    is that what you wanted to do?
    Erh

  5. #5
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess so...
    Thanks!

    (Spirit)


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
  •