LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-23-2006, 12:34 PM   #1
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Rep: Reputation: 30
PHP MySQL Search Question


I have a PHP script that is monitoring a filename and then searches my MySQL database for info on the file. It works unless there is/are ' or - in the filename, and the search fails. I'm assuming that MySQL uses those symbols to modify the search. Is there a way to tell MySQL to use the entire string as the search? Or do I need to rename my files?

PHP Code:
mysql_connect('localhost',$username,$password);
@
mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM `musicmetadata` WHERE `filename` LIKE '%$string%'";
$result=mysql_query($query); 
$string carries the file name obviously.

example of file names:
Code:
01 - artist - song title

artist song title

artist_song_title

etc
 
Old 05-23-2006, 01:28 PM   #2
michaelsanford
Member
 
Registered: Feb 2005
Location: Ottawa/Montréal
Distribution: Slackware + Darwin (MacOS X)
Posts: 468

Rep: Reputation: 30
Yeah, ' is a reserved symbol and needs to be escaped. I would guess that using mysql_real_escape_string() around the variables would fix that.
 
Old 05-23-2006, 03:30 PM   #3
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
I'm not sure what I'm doing wrong, but I tried both addslashes() and mysql_real_escape_string() and it doesn't seem to be changing the string. I echo out the variable after the line and before the search, but there are no slashes. What could I be doing wrong?
 
Old 05-23-2006, 09:05 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Please show us an example or two.
 
Old 05-24-2006, 06:34 AM   #5
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
Sure thing. Here are my two trys so far:

PHP Code:
function remover($string$sep1$sep2)
{
       
$string substr($string0strpos($string,$sep2));     //Finds end position of filename
       
$string substr(strstr($string$sep1), 14);            //Finds beginning of filename
        
$string trim($string);                                //Trims blank space(s) at the beginning and end of string
        
$string addslashes ($string);
#echo get_magic_quotes_gpc();
#echo "($string)";
$username="mythtv";
$password="mythtv";
$database="mythconverg";
#$string = mysql_real_escape_string($string);
mysql_connect('localhost',$username,$password);
@
mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM `musicmetadata` WHERE `filename` LIKE '%$string%'";
#       mysql_real_escape_string($string));
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close(); 
 
Old 05-25-2006, 07:35 PM   #6
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
Question

Does anyone have a suggestion on what I'm doing incorrectly to escape out the ' in my file names?
 
Old 05-25-2006, 09:03 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Actually we'd like to see the input & output strings so we can see what it's doing... so uncomment your 'echo's and copy/paste the results here.
 
Old 05-26-2006, 06:26 AM   #8
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
I echoed out the data after the addslashes() and mysql_real_escape_string() and It doesn't appear to be doing anything. Here is an example output that just happened:

Code:
(Billy Joel - Don`t Ask Me Why)(Billy Joel - Don`t Ask Me Why)
 
Old 05-29-2006, 04:10 PM   #9
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
Anyone have an idea on what I'm doing wrong?
 
Old 05-31-2006, 10:06 PM   #10
smallville
Member
 
Registered: Dec 2005
Posts: 44

Rep: Reputation: 15
can you please post the error message....
 
Old 06-01-2006, 06:37 AM   #11
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
One suggestion. Have you run your SQL statement directly in MySQL? Your PHP line is given as:
PHP Code:
$query="SELECT * FROM `musicmetadata` WHERE `filename` LIKE '%$string%'"
so echo out $query and then send that line directly through MySQL.

I've not used MySQL for a long time but I wonder why you have ticks around musicmetadata and filename.
 
Old 06-01-2006, 09:43 AM   #12
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
Here is the offical SQL error when I put in the search:

Code:
ERROR: Unclosed quote @ 66
STR: '
SQL: SELECT * FROM `musicmetadata` WHERE `filename` LIKE '%Something's%'
If I add the \ before the ' it does work. So I can't figure out why the addslashes is not working.

Last edited by windisch; 06-01-2006 at 09:51 AM.
 
Old 06-01-2006, 10:14 AM   #13
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
Ok, I think I fixed it. Addslashes doesn't work but I found a little function that appeared that I could use. It seems to be working so far. Thanks for your help!

PHP Code:
function mysql_addslashes($string) {
   
$stringstr_replace("'""\'"$string);
   
$stringstr_replace("`""\'"$string);
return 
$string;
}
$string=mysql_addslashes($string); 
 
Old 06-01-2006, 11:32 AM   #14
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You may want to look at the mysql_escape_string() function
 
Old 06-02-2006, 12:25 AM   #15
smallville
Member
 
Registered: Dec 2005
Posts: 44

Rep: Reputation: 15
ERROR: Unclosed quote @ 66
STR: '
SQL: SELECT * FROM `musicmetadata` WHERE `filename` LIKE '%Something's%'

I'm not sure how you used the addslashes() function but it should work. But if you can;t make it work, try this:

SELECT * FROM `employee` WHERE fname LIKE replace('%".$string."%','\'','');

I used mysql replace() function
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP / Mysql Question mattmc97 Programming 3 02-17-2006 08:46 PM
Php search mysql database jamesjoy Programming 4 12-02-2005 07:28 AM
PHP/MySQL Question Corey Edwards Linux - Software 4 01-14-2005 12:18 PM
php mysql question infected Programming 2 10-31-2004 09:50 PM
mySQL, PHP, Apache Question stardotstar Linux - Newbie 6 09-08-2003 04:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration