PHP - MySQL query with '

software development

#1

Dear,

I have a script which already running well at other server, after I change to dreamhost hosting with PHP5, the script can not running well.

The problem is appear when I try to query like this :

$input=“it’s”;
$query = "SELECT * FROM data WHERE title LIKE ‘$input’ ";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

The result will be error with this message:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in…

This problem is because I use at it’s. If I change with non ’ string, than the result is ok.

How to solve this problem?
Thank you in advance.


#2

Did you try to resolve the problem by “escaping” the apostrophe? Try this:

$input=“it’s”;

…and let me know how that works out :wink:

–rlparker


#3

Does the PHP work if you rename the file .php4?

I think you need ‘addslashes’ in to escape the apostrophe. Also you might (or might not) need ‘stripslashes’ in when reading the data back out. Or use mysql_escape_string() .

See this link.
Also might be worth checking it in the PHP manual page here


Norm


#4

Yes, it work with this one :
$input=“it’s”;
$input=addslashes($input);
$query = "SELECT * FROM data WHERE title LIKE ‘$input’ ";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
Thank you very much for the answer.

Just curious, why this is happen with dreamhost server while this problem doesn’t happen at my other server?

Thank you in advance.


#5

It was probably PHP4 on your old server and is now PHP5.
The 2 versions are running so you can test under PHP4 still.
It may have something to do with magic quotes being on or off as well.


Norm


#6

I got the point now, thank you for your fast reply.

Btw, if I already choose to use PHP5 with a domain, can I change only for php version without delete and re-create the domain at dreamhost panel?

Thank you in advance.


#7

I do not believe you can drop back other than by deleting the domain and starting over. I suppose something might be possible by using an addhandler type in .htaccess but I think the best route would be to go with PHP5.
The two versions are running at the moment but who knows what will happen in a few months.


Norm


#8

Ok, nice reply for help my problem. I’m happy with this community and also the dreamhost.

Thank you very-very much.