Hello everyone,

I have a MSSQL db with PHP front end.
The data in the table is similiar to:

"Two spaces" with 2 white spaces in the middle.

When I use this code:

$sql = "select Name FROM Name_TBL  WHERE Number = '$Num";
  $rs = mssql_query($sql);
   $RT = mssql_fetch_array($rs);
     $Name= $RT[Name'];

In the DB, and when I use another query analyzer, I get the correct return of "Two spaces" with 2 white spaces in the middle.

BUT...in PHP when I echo $Name, one of the two spaces has been deleted.
So I echo "Two spaces" with 1 white spaces in the middle.
I need the varchar to be exactly like it is in the DB, and I CANNOT change the data to one space.

Does anyone know what is causing this? Is there someting in the php.ini file that would do this?

PLEASE HELP I AM STUCK!!

-Joe

Recommended Answers

All 5 Replies

Hi joe,

There isn't any problem at all because PHP really outputs series of spaces as just one. I suggest that you either configure the script that inserts the records in the database to automatically converts series of spaces to just one or explode your string to an array such that 1 character string = 1 array element and out each element separately. Hope this helps.

Member Avatar for Rhyan

Another option is to pre-format the string using substring_replace($string, $replacement) before posting to the DB, where the replacement should be  
In this way you will have the string pre-formated inside the DB, so the echo will come out with   and you'll have the 2 psaces.

Thanks everyone, but the Data in the Database CANNOT be changed.

The job of any database extension is the display the data EXACTLY as it is written in the DB. It should not re-write the data based on arbitrary rules.

This page is dynamic. Some of the data returned has one space, this one record has two. I cannot make a rule that changes all of the dat to 2 spaces.

The actual data has 4 parts, each separated by a space, and one, separated by 2 spaces. (on this record only). The other records have 1 space.

The explode does not work, because I need to reconstruct the name, and I cannot do this dynamically, because programatically, the computer does not know where the 2 spaces will go.

Does anyone know if there is another way to get the data other than mssql_fetch_* (row, array, etc) from a MSSQL DB that will NOT change the data from what is written in the DB?

Thanks!!!
Please keep the suggestions comming.

Member Avatar for Rhyan

Normally both PHP and HTML are ignoring multiple spaces and display them as one single space.

If you're sure you get the correct data from the database, trya simple test over the result.

$name = str_replace (' ', ':', RT['yourresult here']);
echo $name;

If you receive the data correctly from the DB, your spaces should be replaced by columns. So if you have returned 2 spaces from the DB, you should see a result like this

value1::value2

Then, if you get the correct result, run the same function but like this:
str_replace(' ', ' ', rt);

NOTE THAT THERE IS A SPACE BETWEEN THE FIRST QUOTES

YOU ARE THE BEST!!! That works perfectly!!!!

Thank you so much. I have been pounding my head on this one. I never thought to use the &nbsp. I kept trying to replace it with spaces, and got the same result.

THANKS!!!!

-Joe

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.