/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] SQL row manip. + replace

Short version:

[code=php]
$data = mysql_query(‘select * blah blah blah’);
if (mysql_num_rows($data) == 1) {
$row = mysql_fetch_array($data);
foreach ($row as ??? => ???) {
apply nl2br on every value
apply strip slashes on every value
}
}
[/code]

I want to clean up all of the data from a mysql query, but I need the array fetched from the row (the query always returns 1 row) to remain intact… How do I do this. Currently I clean up each value individually, which is stupid since I spent a lot of time trying to structure the pages so the row can be manipulated in a single place, not 10 places.

I would really appreciate any help offered. Is it possible to apply nl2br/strip_slashes to an array in it’s entirety?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 25.2010 — Maybe something like this? (untested)
[code=php]
<?php
$sql = "SELECT * FROM table blah blah blah";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
array_walk($row, 'cleaner');
$sql = "REPLACE INTO table VALUES(" . implode(", ", $row). ")";
$replace = mysql_query($sql);
if(!$replace) {
user_error(mysql_error()."<br />n$sql");
}
}
/**
* callback function for array_walk
*/
function cleaner($key, $value) {
$value = trim($value);
if(!is_numeric($value)) {
$value = "'".nl2br(stripslashes($value))."'";
}
}
[/code]
Copy linkTweet thisAlerts:
@themartySep 25.2010 — if the goal is to 'clean up' the data in the database i would just use:

http://dev.mysql.com/doc/refman/5.0/en/replace.html
Copy linkTweet thisAlerts:
@eval_BadCode_authorSep 25.2010 — Thanks NogDog, I was looking for array_walk.

My first post was unclear, so here's the long version:

The goal was to just clean the data up for printing it, so the structure is more like this:

file:

main.php:
[code=php]
<?php
if (mysql_num_rows($data) == 1) {
$row = mysql_fetch_array($data);
$row = //magical_cleaning_function(nl2br/strip_slashes/html_entities);
} else {
include('mail_admin_error.inc.php');
$error = new Error_Report();
$error->report_sql_error();
die('This page is temporarily unavailable.');
}


if ($_get['page_request']=='contact_information') {
include('includes/contact_info.inc.php'); }
elseif ($_get['page_request']=='form_description') {
include('includes/form_desc.inc.php'); }
elseif (and so on so on...) {
include('...the corresponding page...');
else {
include('mail_admin_error.inc.php');
$error = new Error_Report();
$error->report_request_error();
die('Bad Request.');
}
?>
[/code]


file:

includes/contact_info.inc.php: (it's included in main.php)
[code=php]
{
...
...
echo $row[1].$row[2]; #I refer to it by the column name however
...
...
}
[/code]


Thanks both of you.

Although completely unrelated, I was also wondering about "replace into" queries. The post title is very confusing so I can see exactly why replace into queries would be what you would think of.

I'd probably have asked about the same problem NogDog solved in the future.
×

Success!

Help @eval_BadCode_ spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...