Jump to content


Photo

[resolved]How to use a variable in SQL statement.


  • Please log in to reply
4 replies to this topic

#1 althanis

althanis
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 02 October 2006 - 03:41 PM

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:

//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";

I'd like to use $emp_name in the SQL statement. Any help?

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 02 October 2006 - 03:42 PM

Just put {$emp_name} inside the string containing the query.

#3 althanis

althanis
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 02 October 2006 - 03:48 PM

FINAL EDIT: Resolved 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:

SELECT firstname, middlename, surname, emp_id, section_name, dooh, job_title, dob, picture, email, telephone
FROM employee
WHERE firstname LIKE {$emp_name}

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:


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



#4 thedarkwinter

thedarkwinter
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationLondon

Posted 02 October 2006 - 03:57 PM

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, telephone
FROM employee
WHERE firstname LIKE {$emp_name};");

Also, im guessing the %..% for LIKE is in the $emp_name variable already?

Remember - if you don't figure it out yourself, you'll probably forget it tomorrow :)

#5 althanis

althanis
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 02 October 2006 - 03:59 PM

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!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users