[SOLVED] adding a percent sign into the variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

[SOLVED] adding a percent sign into the variable

Post by nhan »

hope anyone can help me...

i have submitted a data using post and i just want to add a percent sign into that variable, how can i add that to the query? thanks! below are my code..

Code: Select all

$date1 = $_POSTї'date1'];

// i just want to add a percent sign at the end of the value of date 1
so that i can do this query...

select * from tbl where userName = '$select' and date like '$date1'

//which would appear something like this one:
select * from tbl where userName = 'name' and date like '03-05-05%'

//where name and 03-05-05 are submitted from a form
how would i do that adding the percent sign in the end?

thanks!
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Code: Select all

select * from tbl where userName = '{$select}' and date like '{$date1}%'
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

When handling strings, you have a handy "String Cocatenation Operator", your friendly period.

If $datel is your variable, then:

Code: Select all

$datel = $datel."%";
//or
$datel .= "%";
Adds a Percent to the end.
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

adding a percent sign into the variable

Post by nhan »

swamps code is working but it seem that i had an error on the query...

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /usr/local/apache/login/user.php on line 24

Code: Select all

$query = &quote;select * from tbl where userName = '$select' and date like '{$date1}%'&quote;;
$result = mysql_query($query); 
$total_rows = mysql_num_rows($result); 
if (!$total_rows) { .....

line 24 is $total_rows = mysql_num_rows($result);

any idea? thanks!
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

try :

Code: Select all

$result = mysql_query($query) or die(MySQL_Error());
adding the " or die ... " clause will present you with the actual error you have in your query... very good idea to use it whenever you are gonna query just about anything ;)
User avatar
nhan
Forum Commoner
Posts: 95
Joined: Sun Feb 27, 2005 8:26 pm
Contact:

Post by nhan »

thaks so much everybody, i made some tweaks and its all ok....

thanks all!
Post Reply