MySQL, PHP help wanted
I am quite a beginner to MySQL. I have this simple expression which I need to filter.
$a = "2";
$s1 = sprintf("%s,",$a)."%";
$s2 = "%".sprintf(",%s,",$a)."%";
$s3 = "%".sprintf(",%s",$a);
$res = mysql_query("SELECT busno FROM busroute WHERE route LIKE '$s1' OR route LIKE '$s2' OR route LIKE '$s3' ");
// This basically searches for "2," OR ",2," OR ",2" in route.
If I use the exact same expression in MySQL browser, I get my results. But it simply returns null in PHP. Help! I know I am missing something, but cant figure out what开发者_C百科.
mysql_query
's documentation says:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or
FALSE
on error.
You also have to retrieve the results:
while(($row = mysql_fetch_array($res))) {
print_r($row);
}
Have a look at the documentation for more examples and information.
Update:
Because this solved the problem: Use mysql_error
to find out the problem with the query.
If it's not just a typo, take a look at this bit: s2'
. It should be '$s2'
. Also, PHP should be fine with this string. Have a look at the data in the table.
精彩评论