0

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>";

}  

?>
4
  • What doesn't work? Have you checked your error log? What errors do you get? What steps have you taken to troubleshoot this? Commented Feb 27, 2013 at 15:14
  • 1
    First of all, don't use '@', it's a bad practice. Also, please specify, what output you get. Have you tried print_r($row);? what does it say? Finally, yes, mysql_ is deprecated, but it still works though Commented Feb 27, 2013 at 15:15
  • John, it's for a school project - i know it doesn't work because when i type a listed artist from the database into the searchbox it just returns a blank page. What's an error log? Commented Feb 27, 2013 at 15:16
  • Since you found your answer, please mark it as accepted. Thanks! :) Commented Dec 18, 2013 at 15:20

3 Answers 3

4

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().

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

sgulseth - you fixed it!! it was the quote marks around artist - i have taken them out and its searching, many many thanks EVERYONE for suggestions.
0

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

4 Comments

Thanks, i've made the changes - no reported errors - just a white screen
Kuf, i don't know which php version it's on a school server? I am getting an empty result returned?
sorry, i don't follow, did you get "Empty result"?
Still completely blank after vardump

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.