views:

52

answers:

5

Howdy,

I'd like to select out of one table all the entries that match two criteria

SELECT * WHERE field1 IS $a AND field2 IS $b FROM TablaA

something like that ...

+3  A: 

How about:

$query = "SELECT * FROM `TableA` WHERE `field1` = '$a' AND `field2` = '$b'";

Remember to mysql_real_escape_string() on $a and $b.

thephpdeveloper
cheers, and the quotes around the $b solve the question I was about to ask (why email addresses aren't working)
Daniel
cherios Daniel!
thephpdeveloper
+1  A: 

SELECT * from tableA where field1 = $a and field2 =$b

Nettogrof
+1  A: 
SELECT * FROM TablaA WHERE `field1` = $a AND `field2` = $b

$a and $b would need quotes if they might not be numeric. I had numbers in my head for some reason.

Frank DeRosa
+1  A: 

Your query is a bit malformed, but you're close:

$a = mysql_real_escape_string($foo);
$b = mysql_real_escape_string($bar);

$sql = "
SELECT
    *
FROM
    `TablaA`
WHERE
    `field1` = '{$a}'
    AND `field2` = '{$b}'
";

Using prepared statements would be a lot better for escaping, but you're probably not ready for that wrench to be thrown into your plans. Just remember, as soon as you feel confident with this stuff, check out "Prepared Statements" and the "mysqli" extension.

Dereleased
A: 
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'petstore';
mysql_select_db($dbname);

$a = mysql_real_escape_string($input1);
$b = mysql_real_escape_string($input2);

$q = mysql_query("SELECT * FROM `TableA` WHERE `field1`='$a' AND `field2`='$b'");

?>

didn't know if you needed the connection stuff too.

Brandon H