1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Change the result of mysql_fetch_array into one variable????

Discussion in 'PHP' started by xampeyan, May 9, 2013.

  1. #1
    I want to retrieve data from database with mysql_fetch_array.
    This is my code.
    $src_get_schspt        = "select * from transaction";
        $query_get_schspt    = mysql_query($src_get_schspt) or die(mysql_error());
     
        while($get_schspt    = mysql_fetch_array($query_get_schspt)) {
    echo $get_schspt["school_support"]."<br>";
    }
    Code (markup):
    For example, the result that i get from $get_schspt["school_support"] is 4 results. Those are bus, catering, uniform, dues. But i want to put all the result from database in one new variable. like this $last_school_support = "bus, catering, uniform, dues";

    Please show me how to do that

    Best regards
    Xampeyan
     
    Solved! View solution.
    Last edited by a moderator: May 9, 2013
    xampeyan, May 9, 2013 IP
  2. dombo

    dombo Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #2
    You can use Array for that.

    Example:

    $r = array();
    $query = mysql_query("select id,name from table");
    while ($row = mysql_fetch_assoc($query)) {
    $r[] = $row;
    }
     
    echo $r[1]['name'];
    echo $r[3]['id'];
    Code (markup):
     
    dombo, May 9, 2013 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    Or, well,... PDO.
    
    $stmt = $db->prepare("SELECT `id`, `name` FROM `table`");
    $stmt->execute();
    $rows = $stmt->fetchAll();
    
    PHP:
     
    nico_swd, May 10, 2013 IP
  4. Lakshmi SEO

    Lakshmi SEO Well-Known Member

    Messages:
    350
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    130
    #4
    $get_schspt itself is a array variable, you can access it via individual column name you have. Like $get_schspt['columnname'].
     
    Lakshmi SEO, May 10, 2013 IP
  5. HowDoYou

    HowDoYou Well-Known Member

    Messages:
    443
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    #5
    or use mysql_fetch_object
     
    HowDoYou, May 10, 2013 IP
  6. #6
    Dombo has it right, Nico_swd even more so -- If you are still using the deprecated mysql_ functions we've been told for EIGHT YEARS to stop using, and that will soon dissapear in a future version of PHP, and that they FINALLY added giant red warning boxes telling you to stop using them...

    IF you were using the SQL connection types you are SUPPOSED to be using now that it's 2013 not 2005, there are functions to pull the complete data set as a single array in PDO. (sadly, mySQLi is still a bit lacking in this department) -- have a good look at:
    http://us3.php.net/manual/en/pdostatement.fetchall.php

    Which does exactly what you are asking for -- though that would mean rewriting your entire codebase to use PDO, but you should be doing that anyways given that again, the mysql_ functions are not long for this earth. (Praise be and Hallelujah!)

    In terms of data fetched there is NO difference between mysql_fetch_array and mysql_fetch_object apart from how they are assigned to the resulting variable. You'd still have to WHILE it, as they still only fetch ONE ROW -- the OP is asking for ALL ROWS as a single result set, so you're either using WHILE to do it hardcoded, or looking at switching to using PDO... in other words, you completely missed the OP's question.

    -- edit -- WAIT, I think dombo, nico and I missed the question TOO! If I'm reading and re-reading the broken engrish correctly, the OP is looking for IMPLODE.

    $last_school_support = implode(',', $get_schspt['school_support']);
    Code (markup):
     
    Last edited: May 10, 2013
    deathshadow, May 10, 2013 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    I think we got it at least partly right. ;p

    If I'm not mistaken, the "4 results" he's referring to are rows. So he would still have to select them all and store them into an array in order to implode().

    Oh, and by the way, MySQLi has a fetch_all() function too, but it relies on the MySQL Native Driver, I think. But I prefer PDO regardless.
     
    nico_swd, May 10, 2013 IP
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #8
    Oops... and I even checked for it before I opened my yap -- silly me checking mysqli_stmt instead of mysqli_result -- though that's part of why I dislike it's syntax, it's a bit too convoluted.

    I think you're right on we were close, since getting an array result from the INDEX of a fetch shouldn't happen. That's the part that had me really confused is how can an index of a result have different values?

    But that's why trying to solve problems using one or two line snippets is like trying to do brain surgery over the telephone in 1876.
     
    deathshadow, May 10, 2013 IP
    nico_swd likes this.
  9. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #9
    
    $src_get_schspt = "select GROUP_CONCAT(school_support) from transaction";
    $query_get_schspt = mysql_query($src_get_schspt) or die(mysql_error());
    $get_schspt = mysql_fetch_row($query_get_schspt)
    echo $get_schspt[0] . "<br>";
    
    Code (markup):
    If you don't need the array, no need to retrieve it.
     
    ThePHPMaster, May 10, 2013 IP
    nico_swd and deathshadow like this.
  10. HussainMHB

    HussainMHB Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #10
    I think this will work

    $src_get_schspt = "select * from transaction";
    $query_get_schspt = mysql_query($src_get_schspt) or die(mysql_error());
    while($get_schspt = mysql_fetch_array($query_get_schspt)) {
    echo $get_schspt["school_support"]."<br>";
    $get_schspt["column name"]."<br>";
    $get_schspt["column name"]."<br>";
    $get_schspt["column name"]."<br>";
    }
     
    HussainMHB, May 11, 2013 IP
  11. HussainMHB

    HussainMHB Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #11
    I think this will work
    $src_get_schspt = "select * from transaction";
    $query_get_schspt = mysql_query($src_get_schspt) or die
    (mysql_error());
    while($get_schspt = mysql_fetch_array($query_get_schspt)) {
    echo $get_schspt["school_support"]."<br>";
    echo $get_schspt["column name"]."<br>";
    echo $get_schspt["column name"]."<br>";
    echo $get_schspt["column name"]."<br>";
    }
     
    HussainMHB, May 11, 2013 IP
  12. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #12
    You think wrong. Also, the problem has been solved in the post right above your's.
     
    nico_swd, May 12, 2013 IP
  13. matt_62

    matt_62 Prominent Member

    Messages:
    1,827
    Likes Received:
    515
    Best Answers:
    14
    Trophy Points:
    350
    #13
    which specific functions are outdated from those examples?




    (sorry op for hi-jacking the thread)
     
    matt_62, May 12, 2013 IP
  14. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #14
    Anything that begins with mysql_*. mysql_query(), mysql_fetch_array(), etc... all of these.
     
    nico_swd, May 12, 2013 IP
  15. matt_62

    matt_62 Prominent Member

    Messages:
    1,827
    Likes Received:
    515
    Best Answers:
    14
    Trophy Points:
    350
    #15
    I just completed a webdesign course like last year, and all of these were taught as being normal. Frick, I even got distinctions in a php driven app that was based on all of these.
    Ahh well, thanks for the heads up, i guess i need to learn the proper code to use to replace these with.

    Thanks
     
    matt_62, May 12, 2013 IP
  16. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #16
    Yeah, a lot of people continue using (and teaching) these like it's the most appropriate thing to do. The mysql_* library will still work for some time, but the sooner you learn how to use the superior alternatives, the better. Take a look at this page, for example.

    http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

    This should get you started.
     
    nico_swd, May 12, 2013 IP
    matt_62 likes this.
  17. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #17
    Part of why I don't consider most 'educators' qualified to open their mouths on the subjects they are allegedly qualified to teach -- and why I consider the average piece of paper that comes with taking such coursework to be worth less than a sheet of bog roll.
     
    deathshadow, May 13, 2013 IP