PHP Variable in MySQL Statement

Reply

Join Date: Jan 2007
Posts: 9
Reputation: twelvetwelve is an unknown quantity at this point 
Solved Threads: 0
twelvetwelve twelvetwelve is offline Offline
Newbie Poster

PHP Variable in MySQL Statement

 
0
  #1
May 30th, 2008
When I use the following PHP code I get this error

Error performing query: Unknown column 'nh12134' in 'where clause'

Yet when I use 'nh12345', which is the Value that the variable $username contains (i've checked that it does by printing it out elsewhere) instead of the $username in the query it returns the correct value.

  1. $tutorname = @mysql_query("SELECT Personal_tutor FROM Student WHERE student_ref=$username;");
  2. if (!$tutorname) {
  3. exit ('<p> Error performaing query: ' . mysql_error() . '</p>');
  4. }

Any help is much appreciated

Thanks
NH
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 13
Reputation: pzuurveen is an unknown quantity at this point 
Solved Threads: 1
pzuurveen pzuurveen is offline Offline
Newbie Poster

Re: PHP Variable in MySQL Statement

 
0
  #2
May 30th, 2008
someting like this:

  1. $query = sprintf("SELECT Personal_tutor FROM Student WHERE student_ref='%s' ",
  2. mysql_real_escape_string( $username));
  3.  
  4. $tutorname = @mysql_query($query);
  5. if (!$tutorname) {
  6. exit ('<p> Error performaing query: ' . mysql_error() . '</p>');
  7. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 252
Reputation: ProfessorPC is an unknown quantity at this point 
Solved Threads: 27
ProfessorPC ProfessorPC is offline Offline
Posting Whiz in Training

Re: PHP Variable in MySQL Statement

 
0
  #3
May 30th, 2008
try this

  1. $tutorname = mysql_query("SELECT Personal_tutor FROM Student WHERE student_ref = '".$username."'");
Last edited by ProfessorPC; May 30th, 2008 at 8:50 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 2
Reputation: Peterpan71 is an unknown quantity at this point 
Solved Threads: 0
Peterpan71 Peterpan71 is offline Offline
Newbie Poster

Re: PHP Variable in MySQL Statement

 
0
  #4
May 30th, 2008
  1.  
  2. $Search=$_POST['Search'];
  3.  
  4. $query_Retrieve = "Select Model,Prime_kVA,Prime_kW,Engine, Alternator,Cylinders,Bore, Stroke,Governer,Type, Retail WHERE (`$Search` like '%{$_POST[Engine]}%') OR (`$Search` like '%{$_POST[Type]}%')";

The above code produces: Wrong results

I have checked that the values are passed correctly.

The idea is that on the refering page the client selects the field to use when doing the query. I have never used variable to indicate the table in the query. Can someone please help me with this.
Last edited by Peterpan71; May 30th, 2008 at 4:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 252
Reputation: ProfessorPC is an unknown quantity at this point 
Solved Threads: 27
ProfessorPC ProfessorPC is offline Offline
Posting Whiz in Training

Re: PHP Variable in MySQL Statement

 
0
  #5
May 30th, 2008
so you want to display everything from this table depending on the users search criteria. you are allowing the user to specify what field to search through. so you will need to grab 2 var's from other page.

  1. $col = $_POST['column'];
  2. $search = $_POST['search'];
  3. $query = mysql_query("Select Model,Prime_kVA,Prime_kW,Engine, Alternator,Cylinders,Bore, Stroke,Governer,Type, Retail WHERE ".$col." like '%".$search."%'") or die(mysql_error());
  4. while($row = mysql_fetch_assoc($query)){
  5. echo $row['Model']."<br>";
  6. echo $row['Prime_kVA']."<br>";
  7. //continue to display remainder of fields
  8. }

this is assuming you have the column in the other page for the user to select. and the textfield for the user to place the search text.
Last edited by ProfessorPC; May 30th, 2008 at 4:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 252
Reputation: ProfessorPC is an unknown quantity at this point 
Solved Threads: 27
ProfessorPC ProfessorPC is offline Offline
Posting Whiz in Training

Re: PHP Variable in MySQL Statement

 
0
  #6
May 30th, 2008
sorry i changed the code lets try that again.
  1. $Search=$_POST['Search'];
  2.  
  3. $query_Retrieve = "Select Model,Prime_kVA,Prime_kW,Engine, Alternator,Cylinders,Bore, Stroke,Governer,Type, Retail WHERE (`".$Search."` like '%".$_POST[Engine]."%') OR (`".$Search."` like '%".$_POST[Type]."%')";
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 2
Reputation: Peterpan71 is an unknown quantity at this point 
Solved Threads: 0
Peterpan71 Peterpan71 is offline Offline
Newbie Poster

Re: PHP Variable in MySQL Statement

 
0
  #7
May 30th, 2008
  1. $query_Retrieve = "SELECT Model, Prime_kVA, Prime_kW, Engine, Alternator, Cylinders, Bore, Stroke, Governer, Type,Retail
  2.  
  3.  
  4. FROM Import WHERE
  5. ((`".$Search."` = '%".$_POST[Engine]."%') AND (`Prime_kVA` = '%".$_POST[Prime_kVA]."%'))
  6. OR
  7. (`".$Search."` = '%".$_POST[Prime_kVA]."%')
  8.  
  9. ";
  10. //OR (`Engine`like '%{$_POST[Engine]}%') OR (`Alternator`like '%{$_POST[Alternator]}%') OR
  11. mysql_select_db($database_generators, $generators);
  12. $Table = mysql_query($query_Retrieve, $generators) or die(mysql_error());

When I use "like" instead of the "=" I get results, but if I make the Prime_kVA (10) the the results that comes back is 10, 100 etc. when using the "=" I get no results.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC