Page 1 of 1

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

Posted: Wed May 27, 2015 9:04 am
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

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

Posted: Wed May 27, 2015 11:56 am
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); 
        }