Variable trouble with mysql and php

Discussion in 'PHP' started by debz89uk, Mar 28, 2010.

  1. #1
    I am using hidden fields to transfer variables from several pages to this one "email.php" page.
    The variable is called $class.
    And the page holds the following query :

    
     $sql = " select student_email.student_id, student_email.info_2, student_email.info_3, student_email.info_3, '$class'
    	from student_email, AllClasses 
    	where student_email.student_id = AllClasses.student_id
    	and $class = 1";
    Code (markup):
    This query doesnt work, but it works if I leave the first '$class' (after student_email.info_3) and replace the second (at the very end of the query) with the actual column name.
    I don't understand why it would work in the first part but not the second (and I have tried putting it as '$class' = 1 with no such luck either).
     
    debz89uk, Mar 28, 2010 IP
  2. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #2
    You have wrong query
    
     $sql = "select student_email.student_id, student_email.info_2, student_email.info_3, student_email.info_3, `$class`
    	from student_email, AllClasses 
    	where student_email.student_id = AllClasses.student_id
    	and $class = 1";
    
    PHP:
    Better
    
    sprintf("select student_email.student_id, student_email.info_2, student_email.info_3, student_email.info_3, %s
    	from student_email, AllClasses 
    	where student_email.student_id = AllClasses.student_id
    	and %s = 1",mysql_real_escape_string($class),mysql_real_escape_string($class));
    
    PHP:
     
    guardian999, Mar 28, 2010 IP
  3. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #3
    what's the variable type of $class? if its not integer use single quotes e.g. '$class' = '1'
     
    javaongsan, Mar 28, 2010 IP
  4. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #4
    You can't use single quotes with the field. but this `$class`='1'
     
    guardian999, Mar 29, 2010 IP
  5. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #5
    $class is a variable so we can do single quote.
     
    javaongsan, Mar 29, 2010 IP
  6. debz89uk

    debz89uk Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks guys, I've tried all of your suggestions, unfortunately none of them are working :/

    I changed the "error query database" to mysql error and it came back with this as the problem

    "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from student_email, AllClasses where student_email.student_id = AllClasses.stu' at line 1"

    so PF is getting changed to stu?
     
    Last edited: Mar 29, 2010
    debz89uk, Mar 29, 2010 IP
  7. debz89uk

    debz89uk Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Okay, fixed.

    The problem was that because the form and the php were all in the one document, and because I was using hidden fields for variables (coming from the previous page) I had to declare the variable again in the second form. :D
     
    debz89uk, Mar 29, 2010 IP