trying to check for db entry

Discussion in 'PHP' started by billybrag, Jul 17, 2006.

  1. #1
    Hello all,

    I am trying to query my db based on a job_id that i get from the url, then if it exists in the db display the page, otherwise display a message. It will basically stop the page having blank details if the job does not exist.

    I have this which is what I want to do.

    <?
    $q = "select job_id from job_post;
    $w = mysql_query($q) or die(mysql_error());
    $e = mysql_fetch_array($w);
    
    
    if ($_GET[job_id] is not in $e) //this is the bit i need help with :)
    {
    Sorry that job does not exist.
    } 
    else
    {
    show the page normally
    }
    ?>
    Code (markup):
     
    billybrag, Jul 17, 2006 IP
  2. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if (!$e=mysql_fetch_array($w)){
    sorry
    }
    else
    {
    OK
    }
    PHP:
     
    mad4, Jul 17, 2006 IP
  3. billybrag

    billybrag Peon

    Messages:
    324
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Mad4

    do you mean
    if (!$_GET[job_id]=mysql_fetch_array($w)){
    sorry
    }
    else
    {
    OK
    }
    PHP:
     
    billybrag, Jul 17, 2006 IP
  4. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No. I meant what I posted. Does it not work?
     
    mad4, Jul 17, 2006 IP
  5. billybrag

    billybrag Peon

    Messages:
    324
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    no but it doesnt look at the job_id from the url so it cant see if that is in the db
     
    billybrag, Jul 17, 2006 IP
  6. ahref

    ahref Well-Known Member

    Messages:
    1,123
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    170
    #6
    You can try this query
    select count(job_id) as num from job_post
     
    ahref, Jul 17, 2006 IP
  7. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You've completely missed taking the ID from the GET values and doing the WHERE in the query. You are pulling ALL the data and then check against the GET. That's wasting a lot of bandwidth. Query ONLY what you need and then count num rows.
     
    T0PS3O, Jul 17, 2006 IP
  8. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Yeah something like
     
    mad4, Jul 17, 2006 IP
  9. rosytoes

    rosytoes Peon

    Messages:
    230
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    $jobid=mysql_escape_string($_GET(jobid));
    $q = "select job_id from job_post where job_id='$jobid'";
    $w = mysql_query($q) or die(mysql_error());
    if (!mysql_num_rows($w)) 
    echo "not in db";
    else {
    $e = mysql_fetch_array($w);
    
    //show details
    }
    
    Code (markup):
     
    rosytoes, Jul 17, 2006 IP
  10. billybrag

    billybrag Peon

    Messages:
    324
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #10
    thanks all I appreciate the assistance
     
    billybrag, Jul 17, 2006 IP