althanis Posted October 2, 2006 Share Posted October 2, 2006 Hi I'm using php to connect to a Microsoft Access DB and I'd like to use a variable that I'm getting from a search form to the database. This is how I'm currently getting the variable and connecting:[code]//retreving the parameter $emp_name = trim(strtolower($_GET['parameters'])); $conn = new COM("ADODB.Connection") or die("Cannot start ADO"); // Microsoft Access connection string. $conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=H:\\path\mof_employees.mdb"); // SQL statement to build recordset. $rs = $conn->Execute("SELECT firstname, middlename, surname, emp_id, section_name, dooh, job_title, dob, picture, email, telephone FROM employee";[/code]I'd like to use $emp_name in the SQL statement. Any help? Link to comment https://forums.phpfreaks.com/topic/22760-resolvedhow-to-use-a-variable-in-sql-statement/ Share on other sites More sharing options...
Daniel0 Posted October 2, 2006 Share Posted October 2, 2006 Just put {$emp_name} inside the string containing the query. Link to comment https://forums.phpfreaks.com/topic/22760-resolvedhow-to-use-a-variable-in-sql-statement/#findComment-102412 Share on other sites More sharing options...
althanis Posted October 2, 2006 Author Share Posted October 2, 2006 [b]FINAL EDIT: Resolved[/b] I had to put single quotation marks around the {$emp_name} for it to work. Thanks a lot Daniel0!When I try this as the SQL statement:[code]SELECT firstname, middlename, surname, emp_id, section_name, dooh, job_title, dob, picture, email, telephoneFROM employeeWHERE firstname LIKE {$emp_name}[/code]I get the following error:"Parse error: parse error, unexpected ';'"EDIT: Sorry I was forgetting a close bracket that I accidentally deleted, that's what caused that error. However, it's still not working, I get this error instead now:[quote]Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft OLE DB Provider for ODBC Drivers<br/><b>Description:</b> [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.' in H:\XAMMP\xampplite\htdocs\wordpress\empsearch.php:60 Stack trace: #0 H:\XAMMP\xampplite\htdocs\wordpress\empsearch.php(60): com->Execute('SELECT ????????...') #1 {main} thrown in H:\XAMMP\xampplite\htdocs\wordpress\empsearch.php on line 60[/quote] Link to comment https://forums.phpfreaks.com/topic/22760-resolvedhow-to-use-a-variable-in-sql-statement/#findComment-102418 Share on other sites More sharing options...
thedarkwinter Posted October 2, 2006 Share Posted October 2, 2006 Probably missing the ) in the Execute function// SQL statement to build recordset.$rs = $conn->Execute("SELECT firstname, middlename, surname, emp_id, section_name, dooh, job_title, dob, picture, email, telephoneFROM employeeWHERE firstname LIKE {$emp_name};"[b])[/b];Also, im guessing the %..% for LIKE is in the $emp_name variable already? Link to comment https://forums.phpfreaks.com/topic/22760-resolvedhow-to-use-a-variable-in-sql-statement/#findComment-102427 Share on other sites More sharing options...
althanis Posted October 2, 2006 Author Share Posted October 2, 2006 Thanks darkwinter, you were right on both counts, I was missing the close brackets, and then I had to add the % to cater for the whitespace at the ends. Thanks! Link to comment https://forums.phpfreaks.com/topic/22760-resolvedhow-to-use-a-variable-in-sql-statement/#findComment-102431 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.