Jump to content

string replace of variable in database


dnk37482

Recommended Posts

I have a fields in my database that have spaces in them like "New York"

 

I want them to be able to be called by

 

states.php?id=newyork // with no space

 

Here is what I have so far...it's not working at all though:

 

<?php

            $name4 = str_replace(" ","", $_REQUEST['id']);

            $query='SELECT * FROM states WHERE

state="'.$name4.'"';

            $result= mysql_query($query);

            $myrow=@mysql_fetch_array($result);

            if($myrow)

            {

                $state=$myrow['state'];

 

            }

            ?>

 

I thank you all in advance for any help I can get..

Link to comment
https://forums.phpfreaks.com/topic/70194-string-replace-of-variable-in-database/
Share on other sites

<?php
$sql = "SELECT * FROM states
        WHERE state LIKE '% %';";
?>

 

this code doesn't seem to work, but thanks for your help.  I am going to try and be more specific.  I am trying to replace strings of a variable already in the database, is that even possible?  like changing "New York" to "NewYork"

 

so that I can pull states.php?id=newyork

 

instead of states.php?id=new york

Is this what you're looking for?

 

SELECT REPLACE(state, ' ', '') AS state
FROM states;

 

isnt this just the same thing as a string replace though, I cant call this from states.php?id=newyork

 

I would still have to call states.php?id=new york

 

Or am I wrong?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.