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
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):
Or, well,... PDO. $stmt = $db->prepare("SELECT `id`, `name` FROM `table`"); $stmt->execute(); $rows = $stmt->fetchAll(); PHP:
$get_schspt itself is a array variable, you can access it via individual column name you have. Like $get_schspt['columnname'].
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):
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.
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.
$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.
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>"; }
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>"; }
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
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.
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.