MySQL Query with two values matching
I'd like to select out of one table all 开发者_如何学Cthe entries that match two criteria
SELECT * WHERE field1 IS $a AND field2 IS $b FROM TablaA
something like that ...
How about:
$query = "SELECT * FROM `TableA` WHERE `field1` = '$a' AND `field2` = '$b'";
Remember to mysql_real_escape_string()
on $a
and $b
.
SELECT * from tableA where field1 = $a and field2 =$b
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.
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.
<?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.
精彩评论