# MySql array and str replace



## thecoalman (Mar 6, 2006)

I have two colums in a MySql data base, using this it returns the values (just so you know I'm getting the data out of the database):


```
<?php
//mysql connect
$smartresult = mysql_query('SELECT * FROM phpbb_smart_tags'); 
while($smartrow = mysql_fetch_array($smartresult)){
	echo $smartrow['smart_tag']. " - ". $smartrow['smart_tag_url'];
	echo "<br />";
}
?>
```
smart_tag is value for a name, smart_tag_url is for a URL. I want to replace the name in a string with the URL. This does not work, it returns the original string but should change Bri to a URL:


```
<?php
$string = ("something something Bri something else");
//mysql connect
$smartresult = mysql_query('SELECT * FROM phpbb_smart_tags'); 
while($smartrow = mysql_fetch_array($smartresult));
$text1 = str_replace($smartrow['smart_tag'] , $smartrow['smart_tag_url'] , $string);
echo $text1;
?>
```
I'm fairly new to coding php so I really have no clue. I've tried a few different things and I can't find an example of eaxctly what I'm trying to do as the terms are so prevalent.


----------



## DrP (Jul 23, 2005)

I can't see where you've defined $string...


----------



## DrP (Jul 23, 2005)

You haven't defined the string you want to replace the name in, so the str_replace won't find your name so can't replace it with the url.
http://www.phpdig.net/ref/rn58re1200.html
That help?


----------



## thecoalman (Mar 6, 2006)

My question is not in reference to my first example, I only put that there to demonstrate I was sucessfully gettin the data out of the database. $string it's defined in the second one which is the one that doesn't work. The first example will return:

Bri - URL1
Steve - URL2
etc.... until it hits the end

I'm pulling the string from a post in a database represented here by $string, I want to strip the words from that string that are in column 1 (smart_tag) and replace them with the URL's in (column 2).

My trouble is gettigt the arrays into the code. This for example works:


```
<?php
$string  = "something something Bri something else.";
$smart_tag = array("Bri", "else");
$smart_tag_url = array("URL1", "URL2");
$text1 = str_replace($smart_tag, $smart_tag_url, $string);
echo $text1;
?>
```
It will return: something something URL1 something URL2.

The above code is basically the same as the one I cannot get to work except the arrays are defined in the code. My problem is getting the arrays pulled from the database to work the same way.


----------



## DrP (Jul 23, 2005)

Sorry, you have defined the $string. Forget that.
Have you tested to see what happens if you echo $smart_tag and $smart_tag_url before the str_replace? Are they showing correctly?

Or, either you are using a shortcut I'm unaware of (I've not been using PHP long so that's very possible), shouldn't the while loop look like this:

while($smartrow = mysql_fetch_array($smartresult)) {
$text1 = str_replace($smartrow['smart_tag'] , $smartrow['smart_tag_url'] , $string);
echo $text1;
}

I didn't think a loop would work without those.


----------

