Jump to content

Archived

This topic is now archived and is closed to further replies.

gjwolf

php mysql special characters

Recommended Posts

I'm having a problem trying to get my sql query to exectute correctly with Php
I can show you what works in phpadmin and what I want to do to make it clearer to articulate.
The following query works good for me in phpmyadmin but when I try to substitue the serach string in php
it doesn't work no matter what I try.  I know its tricky because of my field name has the backspace in it
and using special characters and I tried.. any help appreciated on what xxxxx should be would be very helpful.

here is the query that works





SELECT * FROM `bw` WHERE int_name like ('sfd-br01-tps-Serial0\/%:0\(%')

which returns what I want

sfd-br01-tps-Serial0/0:0(In) 
sfd-br01-tps-Serial0/0:0(Out)
sfd-br01-tps-Serial0/1:0(In)
sfd-br01-tps-Serial0/1:0(Out)
sfd-br01-tps-Serial0/2:0(In)
sfd-br01-tps-Serial0/2:0(Out)


the problem I'm having is on how to put this in php and make it work correctly as a variable in
my array.  No matter what I try I cannot get it to work any help appreciated.  Here is my
loop and code  I put xxxx for the variable as whatever I have tried doesn't work.



$hosts = array('xxxxx');

// Loop through each element in the array
foreach ($hosts as $host_str){

// Request the text of all records that meet this criteria
$query = "SELECT * FROM cpu WHERE name LIKE '$host_str' order by name";
$result = mysql_query($query);

if (!$result) {
  $message  = 'Invalid query: ' . mysql_error() . "\n";
  $message .= 'Whole query: ' . $result;
  die($message);
}


g

Share this post


Link to post
Share on other sites
You probably just need to SQL-escape your string.

Share this post


Link to post
Share on other sites
Hi fenway I'm searching for what you said but how would I do that ?

G

Share this post


Link to post
Share on other sites
PHP has a few built-in functions for this; I've always done it myself.

Share this post


Link to post
Share on other sites
Check out the addslashes() PHP function.

Share this post


Link to post
Share on other sites
Fenway, I really appreciate you giving me the tips.. it worked
thanks a bunch!

G

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.