View Full Version : Line breaking in MySQL

11-03-2011, 04:28 PM
Is it possible to make a line brake in MySQL? If so, how can I do it?

foreach ($_SESSION['cart']['content'] as $content) {
$query = "select * from products where id='{$content['id']}'"; $result = mysql_query($query); $row = mysql_fetch_array($result);
$_SESSION['order'] .= 'ID: '.$content['id'].'LINE-BRAKE-HERE';

11-03-2011, 05:38 PM
I think what you want is

$_SESSION['order'] .= 'ID: '.$content['id'].'\n';

Although that is a line break in PHP not MySQL.

11-04-2011, 03:18 AM
No, my post says a line brake for MySQL, not PHP... which is why it has been posted in the MySQL area of the forum. :rolleyes:

Old Pedant
11-04-2011, 04:56 AM
Your entire code makes no sense.

You run through that collection.

You execute a query.

You get one row from the result of that query.

Then you don't use the row in any way.

So if you don't use the contents of the row, even in MySQL did return a line break, how would you know it? Or get it?

So far as I can see, that code you show could be simplified down to this:

foreach ($_SESSION['cart']['content'] as $content) {
$_SESSION['order'] .= "ID: " . $content['id'] . "\n";

since there is no purpose in the query.

11-04-2011, 05:42 AM
Cool, so is it possible to make a line brake in MySQL? If so, how can I do it?

Old Pedant
11-04-2011, 07:38 AM
I still don't know WHERE you want the line break (not "brake"--you use that to stop your car).

So you want it *IN* the data, as it is stored?

Or do you want to append it to a field that you SELECT?

Or or or ...

You certainly can't append it to a field when you do "SELECT *". At a minimum, you would need to start listing the field names you want returned.

11-04-2011, 07:46 AM
In the code itself:

ID: 1 - Small: 1; Large: 1;ID: 2 - Large: 0;

- to -

ID: 1 - Small: 1; Large: 1;
ID: 2 - Large: 0;


Old Pedant
11-04-2011, 08:02 AM
If I had the foggiest idea where you were getting that data from, out of MySQL, maybe I could help. But you will notice that you still haven't explained why you have any SQL query in there at all. Given that so far the code you have showed isn't using it, at all.

11-04-2011, 08:07 AM
Ok, I am now just using MySQL.

Is it possible to make a line brake in MySQL? If so, how can I do it?

Old Pedant
11-04-2011, 08:12 AM
Put your foot down really, really hard and maybe it will stop.

SHOW ME THE SQL you are using.

Old Pedant
11-04-2011, 08:14 AM
Oh, wth. It's late. I'm going to bed.


SELECT CONCAT( someFieldName, '\n' ) AS fieldWithTrailingNewLine FROM table

11-04-2011, 08:27 AM
This is the code:

INSERT INTO colours (colours1) VALUES ('yellow green') where id = 1";

So, instead of the row showing:
yellow green

it will show:


Old Pedant
11-04-2011, 09:07 AM
Well, for starters you should *NEVER NEVER NEVER* put a LIST of values into a single field in a relational database.

So your whole concept there is bad.

On top of that, you can't use a WHERE clause with an INSERT query, so that code would never run.

But no, it will *NOT* do that.

You could do it with

INSERT INTO colours (colours1) VALUES ('yellow\ngreen')

But it's still a really rotten database design.

11-04-2011, 09:26 AM
So \n is the magic code. Awesome! Thanks!

Yeah its generally a bad database design as it does not allow one to "select from" a single entity (I guess this is included with what you mean by "relational database"), but I am not using it for database reasons. Its simply acting as a functionless list that PHP will be pulling into HTML, so its perfect for what I need.

I could of also just used <BR>, but i'd rather use an internal line brake so its interoperabal with other things should I ever be accessing the database through another medium.