Jump to content

How would I call and show data from db?


GunnDawg

Recommended Posts

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

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 />';
}

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"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.