mysql_query function problem

Discussion in 'PHP' started by phani_kosaraju, Sep 25, 2009.

  1. #1
    Hi All,

    I am generating one query dynamically in php, that query looks like this

    "SELECT sum(a.premium_amt), c.base_premium FROM policy_holders a, policy_status b, ins_plans c WHERE a.login_date <= '$lastDate' AND b.ph_id = a.ph_id AND b.issued_date <= '$issue' AND b.ph_status = 2 AND a.ro_id = '".$ro_id."' AND programecode = 4 AND a.policy_name = ".$policy_row['0']." AND c.plan_id = a.policy_name"

    when i am echoing the query it is showing that query, if i copy that query and run in phpmyadmin it is producing good and correct results. But if i run the same query by using mysql_query function it is giving null values. if you remove a.ro_id = '".$ro_id."' or programecode = 4 or b.ph_id = a.ph_id From that query then it is running perfectly in mysql_query function. But i need to have those conditions.

    If you need any clarification regarding my problem ping me in gtalk at(phanikrishna @ webkuteer.com)
    Thanks in Advance.
     
    phani_kosaraju, Sep 25, 2009 IP
  2. TheOnly92

    TheOnly92 Peon

    Messages:
    17
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What about try running mysql_error() after the query? Any errors?
     
    TheOnly92, Sep 26, 2009 IP
  3. orionoreo

    orionoreo Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    does your b.ph_id = a.ph_id need to be b.ph_id = 'a.ph_id' try changing a.ro_id = '".$ro_id."' to a.ro_id = '$ro_id' and change programecode = 4 to programecode = '4'
     
    orionoreo, Sep 26, 2009 IP
  4. phani_kosaraju

    phani_kosaraju Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I am not getting any error to use mysql_error function. It is returning NULL values.

    i done the changes said by orionoreo but, still the errors remain same.

    Thank you,
     
    phani_kosaraju, Sep 26, 2009 IP
  5. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #5
    OK I would do the following

    firstly you do this one
    
    " some text '".$somevariable."' some more text "
    
    PHP:
    I would do it
    
    'some text "'.addslashes($somevariable).'" Some more text';
    
    PHP:
    note: i swapped the ' with " and added a function to prevent errors in the query

    Then I would try this one.

    
    
    $sql = 'YOUR QUERY GOES HERE';
    echo $sql;
    
    $query = mysql_query($sql);
    
    PHP:
    That way you can simply echo the query and check to see that the query is OK and that the variables had the propper values.

    Hope this helps.
     
    stephan2307, Sep 26, 2009 IP