943,598 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3679
  • PHP RSS
May 30th, 2008
0

PHP Variable in MySQL Statement

Expand Post »
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.

php Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
twelvetwelve is offline Offline
9 posts
since Jan 2007
May 30th, 2008
0

Re: PHP Variable in MySQL Statement

someting like this:

PHP Syntax (Toggle Plain Text)
  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. }
Reputation Points: 32
Solved Threads: 39
Junior Poster
pzuurveen is offline Offline
196 posts
since Sep 2006
May 30th, 2008
0

Re: PHP Variable in MySQL Statement

try this

php Syntax (Toggle Plain Text)
  1. $tutorname = mysql_query("SELECT Personal_tutor FROM Student WHERE student_ref = '".$username."'");
Last edited by ProfessorPC; May 30th, 2008 at 8:50 am.
Reputation Points: 31
Solved Threads: 29
Posting Whiz in Training
ProfessorPC is offline Offline
270 posts
since Dec 2007
May 30th, 2008
0

Re: PHP Variable in MySQL Statement

PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Peterpan71 is offline Offline
2 posts
since Jul 2007
May 30th, 2008
0

Re: PHP Variable in MySQL Statement

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.

php Syntax (Toggle Plain Text)
  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.
Reputation Points: 31
Solved Threads: 29
Posting Whiz in Training
ProfessorPC is offline Offline
270 posts
since Dec 2007
May 30th, 2008
0

Re: PHP Variable in MySQL Statement

sorry i changed the code lets try that again.
php Syntax (Toggle Plain Text)
  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]."%')";
Reputation Points: 31
Solved Threads: 29
Posting Whiz in Training
ProfessorPC is offline Offline
270 posts
since Dec 2007
May 30th, 2008
0

Re: PHP Variable in MySQL Statement

php Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Peterpan71 is offline Offline
2 posts
since Jul 2007
Jul 2nd, 2010
0
Re: PHP Variable in MySQL Statement
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')");
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kukie is offline Offline
3 posts
since Jul 2010
Jul 5th, 2010
0
Re: PHP Variable in MySQL Statement
PHP Syntax (Toggle Plain Text)
  1. $tutorname = @mysql_query("SELECT Personal_tutor FROM Student WHERE student_ref='$username'");
  2. if (!$tutorname)
Reputation Points: 167
Solved Threads: 239
Nearly a Posting Virtuoso
rajarajan07 is offline Offline
1,445 posts
since May 2008
Jul 5th, 2010
0
Re: PHP Variable in MySQL Statement
Use Single Quotes for PHP Variables in mysql.

i.e. '$username'
Reputation Points: 11
Solved Threads: 7
Light Poster
Ankit_Parmar is offline Offline
42 posts
since Jul 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: How to Grab the data from other websites?
Next Thread in PHP Forum Timeline: finding a match





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC