formatting mysql fetch with "'"

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
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

formatting mysql fetch with "'"

Post by vincenzobar »

now i am running into a problem with 's

when i do a fetch_rows and then go to move info to anothe DB it doesn't take the rows that contain words like America's

Here is the code

Code: Select all

$move = array();
			$f_move = array();
			if(in_array($p_id, $DB_IDList)) { 
				$db_row = mysql_fetch_row($all_rows);
					$move[] = "'', "."'".implode("', '", $db_row)."'";
					$f_move[] = "(".implode(', ', $move). ")";
					$moved = implode(", ", $f_move);
					print $moved;
				$archive = 'INSERT INTO archive_rateplan VALUES '.$moved.'';
				mysql_query($archive) or die(mysql_error());
				mysql_query($delete) or die(mysql_error());
				
			}
Where do i or how do i do a addslashes? i triedvarious places but non seemed to work
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

try mysql_real_escape_string() on the input variable
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

everything but the last call is an array for implode to work and if i attach it to the last call it \ all ' and that won't work!!

Arg it's the last step

scheit!!!!!!!!!!!!!!!!!!!!!!!
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Did you try this?

Code: Select all

$move = array();
            $f_move = array();
            if(in_array($p_id, $DB_IDList)) {
                $db_row = mysql_fetch_row($all_rows);
                    $move[] = "'', "."'".implode("', '", $db_row)."'";
                    $f_move[] = "(".implode(', ', $move). ")";
                    $moved = implode(", ", $f_move);
                    print $moved;
                $archive = 'INSERT INTO archive_rateplan VALUES '.mysql_real_escape_string($moved).'';
                mysql_query($archive) or die(mysql_error());
                mysql_query($delete) or die(mysql_error());
                
            }
EDIT: NVM this is wrong... I hang on a minute.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Maybe this?

Code: Select all

$move = array();
            $f_move = array();
            if(in_array($p_id, $DB_IDList)) {
                $db_row = mysql_fetch_row($all_rows);
                    $move[] = "'', "."'".implode("', '", mysql_real_escape_string($db_row))."'";
                    $f_move[] = "(".implode(', ', $move). ")";
                    $moved = implode(", ", $f_move);
                    print $moved;
                $archive = 'INSERT INTO archive_rateplan VALUES '.$moved.'';
                mysql_query($archive) or die(mysql_error());
                mysql_query($delete) or die(mysql_error());
                
            }
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

tried that this is the error
mysql_real_escape_string() expects parameter 1 to be string, array given...
everything in there is an array except the last which makes everything \'

there has to be a way around this!!!
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

You are imploding twice on a one dimensional array?

Anyway..

Code: Select all

array_map('mysql_real_escape_string', $db_row);
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

where does that go and how would you implode it otherwise??

it needs to look like this

('asd', 'asd'), (...), (...)
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

i did this

Code: Select all

$move = array();
			$f_move = array();
			if(in_array($p_id, $DB_IDList)) { 
					$db_row = mysql_fetch_row($all_rows);

					array_map('mysql_real_escape_string', $db_row);

					$move[] = "'', "."'".implode("', '", $db_row)."'";
					$f_move[] = "(".implode(', ', $move). ")";
					$moved = implode(", ", $f_move);
					print $moved;
				$archive = 'INSERT INTO archive_rateplan VALUES '.$moved.'';
				mysql_query($archive) or die(mysql_error());
				//mysql_query($delete) or die(mysql_error());
				
			}
and output still didn't do it
('', '192', 'America's Choice 450', '450', '39.9900.....
wow this is proving difficult!!!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$db_row = array_map('mysql_real_escape_string', $db_row);
:wink:
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

LOL I feel like a retard... My brain is fried!!

I read over array_map and realized that about 2 seconds after i posted that!!!

code now looks like

Code: Select all

$move = array(); 
            $f_move = array(); 
            if(in_array($p_id, $DB_IDList)) { 
                    $db_row = mysql_fetch_row($all_rows); 

                    $dbf_row = array_map('mysql_real_escape_string', $db_row); 

                    $move[] = "'', "."'".implode("', '", $dbf_row)."'"; 
                    $f_move[] = "(".implode(', ', $move). ")"; 
                    $moved = implode(", ", $f_move); 
                    print $moved; 
                $archive = 'INSERT INTO archive_rateplan VALUES '.$moved.''; 
                mysql_query($archive) or die(mysql_error()); 
                //mysql_query($delete) or die(mysql_error()); 
                 
            }
Thanks!
Post Reply