Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Line breaking in MySQL

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

    PHP Code:
    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';

    Last edited by FlashDance; 11-03-2011 at 03:31 PM.

  • #2
    Regular Coder Stooshie's Avatar
    Join Date
    Mar 2008
    Location
    Dundee, Scotland
    Posts
    378
    Thanks
    9
    Thanked 39 Times in 39 Posts
    I think what you want is
    PHP Code:
    $_SESSION['order'] .= 'ID: '.$content['id'].'\n'
    Although that is a line break in PHP not MySQL.
    Regards, Stooshie
    O

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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:
    Code:
    foreach ($_SESSION['cart']['content'] as $content) {
        $_SESSION['order'] .= "ID: " . $content['id'] . "\n";
    }
    since there is no purpose in the query.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Cool, so is it possible to make a line brake in MySQL? If so, how can I do it?

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    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;

    http://i43.tinypic.com/beavch.png

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Ok, I am now just using MySQL.

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

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Put your foot down really, really hard and maybe it will stop.

    SHOW ME THE SQL you are using.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Oh, wth. It's late. I'm going to bed.

    EXAMPLE:

    Code:
    SELECT CONCAT( someFieldName, '\n' ) AS fieldWithTrailingNewLine FROM table
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    This is the code:
    Code:
    INSERT INTO colours (colours1) VALUES ('yellow green') where id = 1";
    So, instead of the row showing:
    yellow green

    it will show:

    yellow
    green
    Last edited by FlashDance; 11-04-2011 at 07:32 AM.

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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
    Code:
    INSERT INTO colours (colours1) VALUES ('yellow\ngreen')
    But it's still a really rotten database design.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    FlashDance (11-04-2011)

  • #14
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    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.
    Last edited by FlashDance; 11-04-2011 at 08:32 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •