954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP Variable in MySQL Statement

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.

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


Any help is much appreciated

Thanks
NH

twelvetwelve
Newbie Poster
9 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

someting like this:

$query = sprintf("SELECT Personal_tutor FROM Student WHERE student_ref='%s' ",
    mysql_real_escape_string( $username));

$tutorname = @mysql_query($query); 
if (!$tutorname) {
exit ('<p> Error performaing query: ' . mysql_error() . '</p>');
}
pzuurveen
Posting Whiz in Training
229 posts since Sep 2006
Reputation Points: 32
Solved Threads: 47
 

try this

$tutorname = mysql_query("SELECT Personal_tutor FROM Student WHERE student_ref = '".$username."'");
ProfessorPC
Posting Whiz in Training
270 posts since Dec 2007
Reputation Points: 31
Solved Threads: 29
 
$Search=$_POST['Search'];

$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.

Peterpan71
Newbie Poster
2 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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.

$col = $_POST['column'];
$search = $_POST['search'];
$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());
while($row = mysql_fetch_assoc($query)){
echo $row['Model']."";
echo $row['Prime_kVA']."";
//continue to display remainder of fields
}


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.

ProfessorPC
Posting Whiz in Training
270 posts since Dec 2007
Reputation Points: 31
Solved Threads: 29
 

sorry i changed the code lets try that again.

$Search=$_POST['Search'];

$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]."%')";
ProfessorPC
Posting Whiz in Training
270 posts since Dec 2007
Reputation Points: 31
Solved Threads: 29
 
$query_Retrieve = "SELECT Model, Prime_kVA, Prime_kW, Engine, Alternator, Cylinders, Bore, Stroke, Governer, Type,Retail 
   
  
  FROM Import WHERE 
  ((`".$Search."` = '%".$_POST[Engine]."%') AND (`Prime_kVA` = '%".$_POST[Prime_kVA]."%'))
                  OR 
  (`".$Search."` = '%".$_POST[Prime_kVA]."%')
                   
  ";
  //OR (`Engine`like '%{$_POST[Engine]}%') OR (`Alternator`like '%{$_POST[Alternator]}%') OR 
  mysql_select_db($database_generators, $generators);
  $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.

Peterpan71
Newbie Poster
2 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

Hi
I am very new to PHP an MYSQL
I have almost the same problem,
how can I

$q=$_GET["q"];
$CMY=$get["CMY"];
...

$query=("INSERT INTO the_array($CMY, model)
VALUES('$q','dddd')");

kukie
Newbie Poster
3 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 
$tutorname = @mysql_query("SELECT Personal_tutor FROM Student WHERE student_ref='$username'"); 
if (!$tutorname)
rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
 

Use Single Quotes for PHP Variables in mysql.

i.e. '$username'

Ankit_Parmar
Light Poster
47 posts since Jul 2010
Reputation Points: 11
Solved Threads: 8
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You