maccame
maccame

Reputation: 45

php/mysql database searching

Can anyone see why the code below is not working? Is it because of the deprecated sql syntax? I'm stumped with this one.

<?php
mysql_connect("localhost",$username,$password);  
@mysql_select_db($database) or die( "Unable to select database");

$usersearch=$_POST['search'];
$query="SELECT * FROM tracks WHERE 'artist' LIKE '%$usersearch%'";  
$result=mysql_query($query);

$num=mysql_numrows($result); 

mysql_close();

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

echo $row["artist"]." | ".$row["name"];
echo "<br>";

}  

?>

Upvotes: 0

Views: 488

Answers (3)

Kuf
Kuf

Reputation: 17846

add to the top of page:

 error_reporting(E_ALL);
 ini_set("display_errors", 1);

and open the page in an browser. are there any errors?

change (this mutes errors)

@mysql_select_db($database) or die( "Unable to select database");

to :

mysql_select_db($database) or die( "Unable to select database");

A word of advice though, better to use the MySQLi or [PDO_MySQL][3] instead of mysql_connectsince it will soon be deprecated:

Warning

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

mysqli_connect()

you can find more info of how to use them on this answer.

UPDATE

check if you get an empty result, add :

$num=mysql_numrows($result); 
if ($num === 0) echo "Empty result"

and open the page again. if the page is still blank, change:

echo $row["artist"]." | ".$row["name"];
echo "<br>";

to:

vardump($row);

UPDATE 2

If you did get Empty result, than add

echo $query;

and try to open the page and run the query manually

Upvotes: 0

sgulseth
sgulseth

Reputation: 21

Probably beacuse you have single quotes(') and not ` when selecting your table. And as pointed out don't use mysql_* use PDO or mysqli, even if its just a school project(old habits die hard). Also you could add

mysql_query($query) or die($mysql_error()); 

This will probably point out whats wrong..

and for last, please escape dynamic input variables

Upvotes: 1

F__M
F__M

Reputation: 1598

First, do not use mysql_* please. This extension is deprecated as of PHP 5.5.0. http://php.net/manual/en/function.mysql-query.php

You can use mysqli_query() or PDO::query().

Upvotes: 4

Related Questions