substr not doing what the manual says for me! [SOLVED]

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
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

substr not doing what the manual says for me! [SOLVED]

Post by andym01480 »

Code: Select all

echo "Uncut query: $query2 <p>";
$query2=substr($query2,0,-1); //should just get rid of the last character?
echo "Cut query: $query2<p>";
produces:

Uncut query: INSERT INTO `caldate`(`startdate`,`enddate`,`starttime`,`endtime`,`event_id`) VALUES('2007-03-19','2007-03-19','00:00','12:00','41'), ('2007-03-20','2007-03-20','00:00','12:00','41'), ('2007-03-21','2007-03-21','00:00','12:00','41'), ('2007-03-22','2007-03-22','00:00','12:00','41'), ('2007-03-23','2007-03-23','00:00','12:00','41'), ('2007-03-24','2007-03-24','00:00','12:00','41'),

Cut query: INSERT INTO `caldate`(`startdate`,`enddate`,`starttime`,`endtime`,`event_id`) VALUES('2007-03-19','2007-03-19','00:00','12:00','41'), ('2007-03-20','2007-03-20','00:00','12:00','41'), ('2007-03-21','2007-03-21','00:00','12:00','41'), ('2007-03-22','2007-03-22','00:00','12:00','41'), ('2007-03-23','2007-03-23','00:00','12:00','41'), ('2007-03-24','2007-03-24','00:00','12:00','41'),

which kills the mysql_query because of the last comma.

So what have I done wrong? The manual has this!

Code: Select all

$rest = substr("abcdef", 0, -1);  // returns "abcde"
Last edited by andym01480 on Sat Mar 17, 2007 4:54 pm, edited 3 times in total.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

maybe collect them in an array then implode(',',$vals); ?
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

I built the query in the first place using a loop, so could just do individual queries.
I thought it would be neater that way, but would need to loose the last comma. Why doesn't substr do it?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

likely because you have an invisible \n character in there after the last ,
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

Afraid not!

Code: Select all

$query2=trim(substr($query2,0,-1));
didn't clear it!
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

you have those in the wrong order...

Code: Select all

$query2=substr(trim($query2),0,-1);
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

Absolutely :roll: and it worked. Thanks!
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Kieran Huggins wrote:maybe collect them in an array then implode(',',$vals); ?
Agreed...

Code: Select all

for ($i=this;$i<=that;$i++) {
  $values[] = "('2007-03-19','2007-03-19','00:00','12:00','41')";
}
$sql = 'INSERT INTO ..... blah blah ..... VALUES '.implode(',',$values);
Works much nicer in my opinon. :D
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

Agreed - looks like tidier code to me! 'Tis modified.
Post Reply