I have a MySQL table that looks like this:
index | tag | posts ------------------------- 1 | cats | 9,10 2 | a cat | 9,10 3 | kitty | 9,10 4 | meow | 9,10
I am trying to just return a string matching the search query. I went through the search parameter using a simple ?search=cats . This is the PHP I'm using:
$search = $_GET['search']; $query = mysql_query("SELECT * FROM tags WHERE tag = '$search'"); echo(mysql_num_rows($query)); $result = mysql_fetch_array($query); $print = $result['posts']; echo($print);
However, mysql_num_rows($query) prints 0 and $print returns NULL. I can check it with ($print == "") , it evaluates to TRUE and mysql_num_rows($query) returns 4. I tried to configure the search query for something that was not in the table, and it reconfigured FALSE as expected. I also tried deleting the WHERE tag = '$search' and it will return the table as it should.
Is there something I'm missing out on?
Edit
Took all the tips and the code I'm using now:
$search = mysql_real_escape_string($_GET['search']); var_dump($search); //prints string(4) "cats" just like it should $queryText = "SELECT * FROM tags WHERE tag = '%".$search."%'"; echo($queryText); //SELECT * FROM tags WHERE tag = '%cats%' $query = mysql_query($queryText) or die(mysql_error()); //no error $rows = mysql_num_rows($query); //this returns 0 and I know it should match 1 row echo('rows: '.$rows); $result = mysql_fetch_array($query); $print = $result['posts']; echo($print); //empty
Still the same problem. mysql_query NULL instead of a string or FALSE if it does not match.
(in the future I will use the mysqli API, but I would like to finish this project in mysql . thanks for your suggestions and tips)
source share