Hi is there a cleaner way to do this? (Mysqli fetch and preg

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
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Hi is there a cleaner way to do this? (Mysqli fetch and preg

Post by hybris »

Hi I have a function to replace some stuff from strings.

I do my mysqli prepare, execute, bind and store

then for fetch I do this
while($row=$stmt->fetch()){
cleanData($ID);
cleanData($artnr);
cleanData($skapadav);
..
.

printf ("%d; %s; %s; %d; %s; %s; %s; %s; %s; %s; %s; %s; %s; %s; %s; %d; %s; %d; %s; %s; %s; %d; %s; %s; %s; %s; %s; %s; %s; %s; %s; %s; %s; %s; %d; %s; %s; %s; %s; %s; %s; %s; %s; %s; %s; %s; %s; %d; %d; %s; %s; %s; %s; %s; %s; %s; %s; %s; \n", $ID, $artnr, $skapadav, ...and so on
}
I know there must be a more elegant way than to having to name all strings for the cleanData function but I cannot get it to work
(Im thinking something like

Code: Select all

while....{
$row = cleanData($row);
printf (...
}
Im not so good at the fetch command... maby

Code: Select all

while bla bla fetch_row($result) {
$result=cleanData($result);
printf...
}
My code works but it is ugly solution

Thanks in advance
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Hi is there a cleaner way to do this? (Mysqli fetch and

Post by Christopher »

Instead of bound variables, just fetch into an array:

Code: Select all

        $stmt->execute();
        $result = $stmt->get_result();
        while ($row = $result->fetch_array(MYSQLI_NUM)) {
            foreach ($row as $key => $val) {
                $row[$key] = cleanData($val);
            }
            $output = implode('; ', $row); 
        }
 
(#10850)
Post Reply