/    Sign up×
Community /Pin to ProfileBookmark

Str_Replace with database?

Is it possible to pull information from the datase and do a str_replace to display something else?

For example:

[code=php]‘.$myrow[‘Field1′].’[/code]

This is what I use to pull information, can I use the script below below with the $myrow to pull data and display it as something else?

[code=php]
<? $Example = str_replace(“”, “notselected”, $Example); echo “$Example”; ?>
[/code]

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@pyroMay 09.2004 — Yep.
Copy linkTweet thisAlerts:
@d_brandusaauthorMay 09.2004 — Okay...could you show me a sample code pyro, I can't get it to work!?

DB
Copy linkTweet thisAlerts:
@pyroMay 09.2004 — Does this not work?

[code=php]<?PHP
echo str_replace("foo", "bar", $myrow['Field1']);
?>[/code]
Copy linkTweet thisAlerts:
@d_brandusaauthorMay 10.2004 — Here is what my script looks like, I receive a parse error with your code? Am I pluging it in wrong? How would you incorporate this into my code, for instance, adjust my "Employee_Name" with the str_replace:

[code=php]
<?php

//connection information
$ser="server"; $db="database"; $user="username"; $pass="password";

//sql connection
$conn = odbc_connect($db, $user, $pass);

if(isset($_GET["id"]) && !empty($_GET["id"])) {

$id = $_GET["id"];

$query = "SELECT * FROM webtest WHERE Company_Name = '" . $id . "'";

$result=odbc_do($conn, $query);

?>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Form</title>
</head>
<body>

<p>INSERT RECORD</p>

<form action="business_update.php" method="post" enctype="multipart/form-data">

<?php

while($myrow=odbc_fetch_array($result)) {

echo('

Company Name: <input name="Company_Name" value="'.$myrow['Company_Name'].'"><br>
Employee Name: <input name="Employee_Name" value="'.$myrow['Employee_Name'].'"><br>
City: <input name="City" value="'.$myrow['City'].'"><br>
How Many Employees: 1-50 <input type="checkbox" name="How_Many_Employees" value="1-50"> 51-100 <input type="checkbox" name="How_Many_Employees" value="51-100"> 100+ <input type="checkbox" name="How_Many_Employees" value="100+"><p>

');
}
}
?>

<input type="submit" value="Submit">
</form>

</body>
</html>
<?php

odbc_close($conn);

?>
[/code]
Copy linkTweet thisAlerts:
@pyroMay 10.2004 — Try it like this:

[code=php]<?php

while($myrow=odbc_fetch_array($result)) {

echo('
Company Name: <input name="Company_Name" value="'.$myrow['Company_Name'].'"><br>
Employee Name: <input name="Employee_Name" value="'.str_replace('search', 'replace', $myrow['Employee_Name'].'"><br>
City: <input name="City" value="'.$myrow['City'].'"><br>
How Many Employees: 1-50 <input type="checkbox" name="How_Many_Employees" value="1-50"> 51-100 <input type="checkbox" name="How_Many_Employees" value="51-100"> 100+ <input type="checkbox" name="How_Many_Employees" value="100+"><p>
');
}

}
?>[/code]
Copy linkTweet thisAlerts:
@d_brandusaauthorMay 10.2004 — I did a straight copy and past and I get this error.

Parse error: parse error, unexpected ';' in business_details.php
Copy linkTweet thisAlerts:
@pyroMay 10.2004 — Line numbers always help. ?
Copy linkTweet thisAlerts:
@d_brandusaauthorMay 10.2004 — on line 37

It's the line with the code you provided, it did not like str_replace in that format.
Copy linkTweet thisAlerts:
@pyroMay 10.2004 — Oops, sorry. I forgot to close the parenthesis. Add ) after $myrow['Employee_Name']
Copy linkTweet thisAlerts:
@d_brandusaauthorMay 10.2004 — thanks alot pyro, it worked!
Copy linkTweet thisAlerts:
@d_brandusaauthorMay 10.2004 — How would you have more then one?

Say the option is blank, no information, then change to:

Is my code right?

[code=php]
'.str_replace('1-50', 'checked', $myrow['How_Many_Employees']); str_replace('', 'unchecked', $myrow['How_Many_Employees']).'
[/code]
×

Success!

Help @d_brandusa 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,
)...