Need help with mysql query

Discussion in 'PHP' started by yelbom, Dec 31, 2010.

  1. #1
    I need help with php code for sql querys that searches 2 tables based on date range and item. Below is not a attempt to code, its was the best way for me to explain :)


    1. SELECT jobid FROM jobs WHERE date (is between 8/*/2009 and 9/*/2009)

    Then

    2. SELECT * FROM bids WHERE jobid= (the jobids found in the first mysql_query) and item=$_POST['item']


    Database Tables, Field names and data examples


    JOBS
    jid|jobid|description|date
    1|232|Part time work|9/15/2009

    BIDS
    bid|jobid|item|user|price
    1|232|floor cleaning|micky|8


    Form Mockup (Just for a mental picture so you see what Im trying to do)
    [​IMG]

    Thanks for anyhelp
     
    Last edited: Dec 31, 2010
    yelbom, Dec 31, 2010 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    
    SELECT * FROM jobs
    
    INNER JOIN bids 
    ON bids.jobid = jobs.jobid
    
    WHERE jobs.date BETWEEN 2009-08-01 and 2009-09-31
    
    Code (markup):
    This will select all fields for date between the specified range.
     
    ThePHPMaster, Dec 31, 2010 IP
  3. yelbom

    yelbom Greenhorn

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    Thanks that worked! So I tested this query in phpMyadmin and it worked
    
    SELECT *
    FROM jobs
    INNER JOIN bids ON bids.jobid = jobs.jobid
    WHERE jobs.dotdate
    BETWEEN '1/20/2009'
    AND '9/15/2009'
    AND bids.item = 'floor cleaning'
    AND bids.user = 'micky'
    
    Code (markup):
    BUT Im having a problem getting my php code to work [see below]

    
    $result = mysql_query("SELECT * FROM jobs 
    INNER JOIN bids ON bids.jobid = jobs.jobid
    WHERE jobs.dotdate BETWEEN '.$date1.' and '.$date2.' 
    AND bids.item='$item' AND bids.user='$user'");
    
    $num_rows = mysql_num_rows($result);
    if($num_rows != 0){
    	$sum = '0.0000';
    	while($row = mysql_fetch_assoc($result)){
    	$sum = ($sum + $price);
    
    	$bids_id = $row['bids_id'];
    	$jobid = $row['jobid'];
    	$item = $row['item'];
    	$description = $row['description'];
    	$user = $row['user'];
    	$price = $row['price'];
    	}
    }
    
    Code (markup):
    Any Idea what Im doing wrong?
     
    yelbom, Dec 31, 2010 IP
  4. R2DS

    R2DS Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Just at a really quick glance the periods next to the dates might be causing the issue...

    $result = mysql_query("SELECT * FROM jobs
    INNER JOIN bids ON bids.jobid = jobs.jobid
    WHERE jobs.dotdate BETWEEN '{$date1}' and '{$date2}'
    AND bids.item='{$item}' AND bids.user='{$user}'");
     
    R2DS, Dec 31, 2010 IP
  5. ankit_frenz

    ankit_frenz Active Member

    Messages:
    1,111
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    63
    #5
    include a line mysql_query($sql) or die(mysql_error()); that will help you debug what the error is in the sql query
     
    ankit_frenz, Dec 31, 2010 IP
  6. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #6
    As RD2S said, remove those dots:

    
    $result = mysql_query("SELECT * FROM jobs 
    INNER JOIN bids ON bids.jobid = jobs.jobid
    WHERE jobs.dotdate BETWEEN '$date1' and '$date2' 
    AND bids.item='$item' AND bids.user='$user'");
    
    $num_rows = mysql_num_rows($result);
    if($num_rows != 0){
    	$sum = '0.0000';
    	while($row = mysql_fetch_assoc($result)){
    	$sum = ($sum + $price);
    
    	$bids_id = $row['bids_id'];
    	$jobid = $row['jobid'];
    	$item = $row['item'];
    	$description = $row['description'];
    	$user = $row['user'];
    	$price = $row['price'];
    	}
    }
    
    PHP:
     
    ThePHPMaster, Jan 1, 2011 IP
  7. cazort

    cazort Peon

    Messages:
    276
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #7
    A general word of wisdom: when you're working with PHP, a good way to debug is:

    $query="SELECT ... (insert query text here);"
    echo $query; // Or something of the sort, then comment it out when your code goes live
    $result=mysql_query($query);
    
    Code (markup):
    Then paste the output of your query into MySQL or PHP My Admin or whatever tool you're using, and debug the query. This separates the MySQL debugging from the PHP debugging and will greatly speed up your efforts, and will save you the need for visiting forms like this, as much as we love having you here!

    =)
     
    cazort, Jan 2, 2011 IP