| | |
PHP Variable in MySQL Statement
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jan 2007
Posts: 9
Reputation:
Solved Threads: 0
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.
Any help is much appreciated
Thanks
NH
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)
$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
•
•
Join Date: Sep 2006
Posts: 13
Reputation:
Solved Threads: 1
someting like this:
PHP Syntax (Toggle Plain Text)
$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>'); }
•
•
Join Date: Dec 2007
Posts: 252
Reputation:
Solved Threads: 27
try this
php Syntax (Toggle Plain Text)
$tutorname = mysql_query("SELECT Personal_tutor FROM Student WHERE student_ref = '".$username."'");
Last edited by ProfessorPC; May 30th, 2008 at 8:50 am.
•
•
Join Date: Jul 2007
Posts: 2
Reputation:
Solved Threads: 0
PHP Syntax (Toggle Plain Text)
$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.
Last edited by Peterpan71; May 30th, 2008 at 4:03 pm.
•
•
Join Date: Dec 2007
Posts: 252
Reputation:
Solved Threads: 27
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.
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.
php Syntax (Toggle Plain Text)
$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']."<br>"; echo $row['Prime_kVA']."<br>"; //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.
Last edited by ProfessorPC; May 30th, 2008 at 4:04 pm.
•
•
Join Date: Dec 2007
Posts: 252
Reputation:
Solved Threads: 27
sorry i changed the code lets try that again.
php Syntax (Toggle Plain Text)
$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]."%')";
•
•
Join Date: Jul 2007
Posts: 2
Reputation:
Solved Threads: 0
php Syntax (Toggle Plain Text)
$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.
![]() |
Similar Threads
- Supplied argument is not a valid MySQL result resource (MySQL)
- mysql_fetch_array(): supplied argument is not a valid MySQL (PHP)
- get html element value using php (PHP)
- Can a text link pass php variables? (PHP)
- Using MySQL with PHP - Tutorial by Herong (MySQL)
- Error message (MySQL)
- Populating a fields on a form base on a selected item from a droplist. (PHP)
- mySQL database searching for registration (MySQL)
- Find and Replace (Database Design)
Other Threads in the PHP Forum
- Previous Thread: How to pass Variables retrieved from MySQL in PHP to next page
- Next Thread: how to reset md5 encrypted password
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary body broken cakephp checkbox class cms code cron curl database date date/time directory display download dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla limit link list login loop mail menu mlm mod_rewrite msqli_multi_query multiple mycodeisbad mysql oop parse paypal pdf php problem query radio random recursion regex remote script search send seo server sessions sms soap source space sql static structure syntax system table tutorial update upload url validation validator variable video web webdesign wordpress xml youtube





