Danbannan Posted August 5, 2010 Share Posted August 5, 2010 Hello, im new here, and i have little experience to php and mysql as i started for 2 weeks ago. I started out with some tutorials and feeling im getting the hang of it. Enough of me, lets get to the point: <?php $con = mysql_connect('localhost',$user,$pass)or die(mysql_error()); $selectdb = mysql_select_db($selectdb)or die(mysql_error()); $sql = "SELECT * From table"; $result = mysql_query($sql); $num = mysql_num_rows($result); $myarray = array($result); $i =0; while ($i < $num){ echo $myarray[$i]; $i++; } ?> Here i have written a dummyscript that does what the original script does, it tries to fetch the keys from the table and then trying to loop it and echo out the results. The output in the browser is this: Resource id #3 I know this probably is a simple fix but i cant seem to get it sorted out. Hope some of you could help me get this baby work, or maybe have another way of doing it more "simple". Thanks in advance! Dan-Levi Quote Link to comment https://forums.phpfreaks.com/topic/209896-retrieve-results-from-mysql-as-array-and-echo-it-out/ Share on other sites More sharing options...
dolrichfortich Posted August 5, 2010 Share Posted August 5, 2010 array($result) will not give you the data in the resource. You will need to use mysql_fetch_array or similar function to get those data. Here is a sample code. <?php $con = mysql_connect('localhost',$user,$pass)or die(mysql_error()); $selectdb = mysql_select_db($selectdb)or die(mysql_error()); $sql = "SELECT * From table"; $result = mysql_query($sql); //This loops until mysql_fetch_array returns false //which means you have reached the last row while ($row = mysql_fetch_array($result)) { print_r($row); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/209896-retrieve-results-from-mysql-as-array-and-echo-it-out/#findComment-1095581 Share on other sites More sharing options...
Danbannan Posted August 5, 2010 Author Share Posted August 5, 2010 Very interesting dolrichfortich :-) Thank you for clearing up some. So i did not exactly use your example to go fourth, instead i kept the original while loop and thought about designing it somewhat different. This is what i came up with: <?php //row details for the while loop (mysql_result()) $dbrow1 = "row1"; $dbrow2 = "row2"; $con = mysql_connect('localhost','$dbuser','$dbpass'); $select_db = mysql_select_db($dbname); $sql = "SELECT * FROM testtable"; $result = mysql_query($sql); $num = mysql_num_rows($result); $i = 0; while ($i < $num) { $dbresult1 = mysql_result($result, $i, $dbrow1); $dbresult2 = mysql_result($result, $i, $dbrow2); $i++; } //put the results automatically into an array ?> What i am struggeling here is to make this piece of code automatically make an array of the results. Im pretty sure that isnt a big of a problem either, i know to make an array of this you need to: array($dbresult1, $dbresult2); But i cant seem to came up with how to approach this. Again, thank you dolrichfortich, here is yet a challenge! hehe Quote Link to comment https://forums.phpfreaks.com/topic/209896-retrieve-results-from-mysql-as-array-and-echo-it-out/#findComment-1095620 Share on other sites More sharing options...
wildteen88 Posted August 5, 2010 Share Posted August 5, 2010 To add items to an array, do $var[] = 'new item here'; Adding [] after a variable will add the value on the right to the array ($var). Rather than use mysql_result it is much cleaner to use mysql_fetch_assoc, eg $data = array(); while($row = mysql_fetch_assoc($result)) { $data[] = $row; } Quote Link to comment https://forums.phpfreaks.com/topic/209896-retrieve-results-from-mysql-as-array-and-echo-it-out/#findComment-1095638 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.