PHP MySQL query returns NULL

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)

+4
source share
5 answers

Well, therefore, referring to the above board, you have created a solution

Use "LIKE" instead of "=" when using the wildcard character "%"

So your request should be

 $queryText = "SELECT * FROM tags WHERE tag LIKE '%" . $search . "%'"; 

[I created the same db on my local system and executed the same code that you gave, by doing the above changes, it works as expected]

0
source

Try this code.

Remeber , when you want to debug something in PHP, the faster way is var_dump is not an echo. Also you should avoid mysql_api because they are deprecated, use PDO PDO in PHP.net instead

 var_dump($_GET); // Just for debuggin if as something $search = $_GET['search']; $query = mysql_query("SELECT * FROM tags WHERE tag = '".mysql_real_escape_string($search)."'"); // echo(mysql_num_rows($query)); $result = mysql_fetch_array($query); var_dump($result); //$print = $result['posts']; //echo($print); 
+1
source
 $search = $_GET['search']; echo $select_query="SELECT * FROM tags WHERE tag = '".mysql_real_escape_string($search)."'"; $query = mysql_query($select_query); echo(mysql_num_rows($query)); while($result = mysql_fetch_array($query)) { print_r($result); } 
0
source

Note:

 $search = $_GET['search']; $query = mysql_query("SELECT * FROM tags WHERE tag = '$search'"); 

This is very dangerous: it allows you to use sql conversion code in your database. You should always avoid everything that you receive from the client.

 $search = mysql_real_escape_string($_GET['search']); //It require open database connection. 

Note 2: mysql_query is deprecated, use mysqli instead; -)

Answer:

If you do not have an answer, you probably have an error in another part. Try

 //1) Look if your search has a correct value var_dump($search); //2) Replace the query with (just for debugging): $query = mysql_query("SELECT * FROM tags WHERE tag = 'cats';"); 

You can also use "tag like"% cats% "" if you want a more flexible search.

0
source

If you delete WHERE tage = '$ search', it will not be able to return the table as it should, because your mysql_fetch_array is not in the while loop ... but aside ...

 // make sure before you execute the code to check that $_GET['search'] is not empty // start with escaping the search-value (for mysql-injection) $search = msyql_real_escape_string($_GET['search']); // changed the query so it searches for tags containing the search value. // if you would have records with tags "blue cat" and "red cat" it shows them both // when searching for "cat" $query = mysql_query("SELECT * FROM tags WHERE tag LIKE '%".$search."%'"); // put the number of rows in a var $num = mysql_num_rows($query); // check this var if it not 0 if ($num != '0'){ while ($row = mysql_fetch_array($query){ echo $row['posts']; // etc... } } else { // 0 rows found echo "nothing found"; } 
0
source

All Articles