madsovenielsen Posted May 22, 2009 Share Posted May 22, 2009 Hey all.Im fiddling with some PHP and MySQL, the database holds some names and ages. when i want to display the data in a php page. i do this: <?php mysql_connect("localhost", "root", "password") or die(mysql_error()); mysql_select_db("test") or die(mysql_error()); $result = mysql_query("SELECT * FROM example") or die(mysql_error()); $row = mysql_fetch_array( $result ); echo $row['name']; echo $row['age'];?> Its working fine. but im only getting the name and age from the "top" of the database.how do i get all the names and ages in the database as the result?/mads Link to comment Share on other sites More sharing options...
jeffman Posted May 22, 2009 Share Posted May 22, 2009 Subsequent calls to mysql_fetch_array() will return the next row, the one after that, and so on. When the rows are exhausted, mysql_fetch_array() returns false. This function, and ones like it, are typically used in loops. Look at the examples in the online manual: http://www.php.net/manual/en/function.mysql-fetch-array.phpAnd learn to read the online manual. It's a wonderful resource, with information about things you never even imagined. Link to comment Share on other sites More sharing options...
madsovenielsen Posted May 22, 2009 Author Share Posted May 22, 2009 I see how its working now. nice. is it necessary to close the connection to MySQL after the query ?/mads Link to comment Share on other sites More sharing options...
jlhaslip Posted May 22, 2009 Share Posted May 22, 2009 The php script will close the connection for you (if you forget), but it is good programming technique to close the connection yourself. There are no consequences if you do not close the connection. Link to comment Share on other sites More sharing options...
jeffman Posted May 22, 2009 Share Posted May 22, 2009 From the manual (surprise!): Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution.If your script executes for an unusually long amount of time, and the DB portion is near the beginning, then you should close the connection. If your script terminates only a few lines after your last call to the DB, then it's really not necessary.On the other hand, a lot of things (like semi-colons or curly braces) aren't necessary in some context or another. But since they are necessary or useful in some contexts, you're probably better off getting in the habit of doing everything "the long way." That is to say: If doing X is never wrong, and not doing X is sometimes wrong, the safest thing is to always do X.So close your connections. Link to comment Share on other sites More sharing options...
madsovenielsen Posted May 22, 2009 Author Share Posted May 22, 2009 From the manual (surprise!):If your script executes for an unusually long amount of time, and the DB portion is near the beginning, then you should close the connection. If your script terminates only a few lines after your last call to the DB, then it's really not necessary.On the other hand, a lot of things (like semi-colons or curly braces) aren't necessary in some context or another. But since they are necessary or useful in some contexts, you're probably better off getting in the habit of doing everything "the long way." That is to say: If doing X is never wrong, and not doing X is sometimes wrong, the safest thing is to always do X.So close your connections. Thats exactly why is use this forum as a supplement to the manual(s)./mads Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.