GunnDawg Posted December 20, 2008 Share Posted December 20, 2008 So I am still rather new to PHP, and MySQL and was making a basic little script to practice with. So far I have a form that when submitted, will save the text in that field and assign it an ID (autoID) How can I go about pulling all of the data from the field "message" and echoing it. Kinda going for a little message board type of look thing. Link to comment https://forums.phpfreaks.com/topic/137778-how-would-i-call-and-show-data-from-db/ Share on other sites More sharing options...
xtopolis Posted December 20, 2008 Share Posted December 20, 2008 A decent tutorial. http://www.tizag.com/mysqlTutorial/ or, fill in your info. mysql_connect('host','username','password'); mysql_select_db('dbname'); $sql = "SELECT message FROM yourTable"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)){ echo $row['message'].'<br />'; } Link to comment https://forums.phpfreaks.com/topic/137778-how-would-i-call-and-show-data-from-db/#findComment-720117 Share on other sites More sharing options...
GunnDawg Posted December 20, 2008 Author Share Posted December 20, 2008 Awesome thanks it seems to work. Now I need to study that code and figure out why it works instead of just copy/pasting it. heh Link to comment https://forums.phpfreaks.com/topic/137778-how-would-i-call-and-show-data-from-db/#findComment-720122 Share on other sites More sharing options...
xtopolis Posted December 20, 2008 Share Posted December 20, 2008 Establish a connection to your database host: mysql_connect('host','username','password'); Select the database by name: mysql_select_db('dbname'); Assign the mysql query string to a php variable: (if you had a mysql console, you could just type in the "SELECT ... " in there) $sql = "SELECT message FROM yourTable"; Query the string; mysql returns a pointer to the result set $result = mysql_query($sql); (This simply means, mysql stores the rows that are affected by your query, and gives you a handle to access them. They are kinda like an array. Here we loop through all the results (since we usually don't know the length) while($row = mysql_fetch_array($result)){ echo $row['message'].'<br />'; } basically, each time we do this, it returns the next row in the result until there is no more (where it returns false) also, on each iteration, we each out this rows value for the column named "message" Link to comment https://forums.phpfreaks.com/topic/137778-how-would-i-call-and-show-data-from-db/#findComment-720135 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.