Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

mysql_query with parameter

Hello,

I know that using PDO class is better and the newer way but my client is stuck with older version of php. I want to do a database query with parameter like that:

 $object_query = mysql_query("SELECT title, info, image_path FROM objects WHERE id=?");

 $element = mysql_fetch_assoc($object_query);

i want to put a variable in the place of the question mark. I know how to do it with PDO prepare but i dont know how to do it without PDO class.

2 Answers

I once had todo something like this. This is how I checked that

$DB_Check = " SELECT * from users Where username = '".$Username."' and password = '".$Password."' " ;

it may be hard to read but you just have to surround your values with ' " . . " ' you have to make sure the first apostrophe or double quote is different to the one you started the query with e.g

  $DB_Check = ' SELECT * from users Where username = "'.$Username.'" and password = "'.$Password.'" ' ;

Np

Thanks for the fast response. It worked without a problem.