Jump to content

Extracting & replacing the SQL Carriage Return from a Query


optimaximal

Recommended Posts

Hiya all, first thread and all...

Basically, I'm pulling data from my company's customer database (running on MSSQL - SQL Server) for the purpose of mailings in order to create a web-based printable label system (don't ask :)).

The problem i'm facing is the SQL Server uses the CHAR(13) ASCII code (aka. a carriage return) and I need a way to grab this in PHP so that I can use the str_replace function to replace it with a form of carriage return that a web browser can understand (i.e. <br>).

Here's the code -
[code]$connection_string = 'DRIVER={SQL Server};SERVER=########;DATABASE=######';

$user = '######';
$pass = '######';

$connection = odbc_connect( $connection_string, $user, $pass );
$sqlquery="SELECT top 10 title, full_name, house, address, postcode, customer FROM cust ORDER BY NEWID();";
$process=odbc_exec($connection, $sqlquery);

//Finally, you extract all of the companies from the Customers table and display them onscreen in a list. Once the entire list has been displayed, the connection to the database is closed:
while(odbc_fetch_row($process)){
$Title = odbc_result($process, 1);
$Name = odbc_result($process, 2);
$House = odbc_result($process, 3);
$Address = odbc_result($process, 4);

//Replace Carriage Return
$Address2 = str_replace("NEED THIS VALUE", "<br>", "$Address");

$Postcode = odbc_result($process, 5);
$custnum = odbc_result($process, 6);
echo "<span class='small'>$custnum</span><p>$Title $Name<br>$House $Address2<br>$Postcode<p><hr>"; }
odbc_close($connection);[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.