1

I'm sorry if this question frustrates anyone ... I am truly a beginner to PHP + MYSQL but I would love to make some progress on this one!

My Goal:

  1. To create a table that I can view in a web page.
  2. To be able to add data (rows) to this table and still have the data be there the next time the page is loaded.

Step 1: Creating the table

<html>
<body>

<?php

// Conect to MYSQL
$con = mysql_connect("localhost", "My_Username", "My_Password") or die(mysql_error());
echo "Connected to MYSQL </br>";

mysql_select_db("My_Database") or die(mysql_error());
echo "Connected to Database";

// Create a MySQL table in the selected database (called ExampleTable)

$sql = "CREATE TABLE ExampleTable
 (
// Set the primary key (personID)
 personID int NOT NULL AUTO_INCREMENT, 
 PRIMARY KEY(personID),
 ColumnOne varchar(15)
 ColumnTwo varchar(15)
 ColumnThree varChar(30)
 )";

// Execute query
mysql_query($sql,$con);

mysql_query("INSERT INTO ExampleTable(ColumnOne, ColumnTwo, ColumnThree)
VALUES ('Data1', 'Data2', 'Data3')");

mysql_query("INSERT INTO ExampleTable(ColumnOne, ColumnTwo, ColumnThree) 
VALUES ('Data_Data1', 'Data_Data2', 'Data_Data3')");

mysql_close($con);

?>

</body>
</html>

So, I have now created a MYSQL table with two three columns, and two rows of data. How can I get this table to show up on a web page?

Step 2: Saving / Retrieving saved data Is there some way that I can add data to a table, so that the data will be there permanently? - or is this how it works by default?

For example: Let's say that I have a form with a button on it. When the button is clicked a new row is added to the table 'ExampleTable'. The next time the user visits the page the table will be updated with his newly added data.

Thank you very much for any help. I understand that I am a beginner and do not fully understand these topics yet. Any responses will be greatly appreciated.

3
  • 1
    Too generic. There lots of tutorial out there for this Commented Jun 8, 2011 at 23:05
  • I'm sorry - I was afraid of that. I'm just trying to get all of my confusion out on paper! sometimes a little brainstorming goes a long way. Commented Jun 8, 2011 at 23:07
  • Note: for table , you need to create only once. if you run this script again it will throw mysql error because the table already created. Commented Jun 8, 2011 at 23:11

3 Answers 3

1

I'd recommend creating the table in a separate step, not from PHP; you don't want every time your web page is refreshed to create a new table.

Once your table is created, you can get the data from it by executing (within your PHP, using mysql_query) a query like "SELECT * from {tablename}". Once you've got that query result from mysql, then you can use the various PHP looping and mysql record reading methods to output the results from your query in the form you want into the page your PHP script will be serving to your client.

Sign up to request clarification or add additional context in comments.

2 Comments

Will the table remain there "for ever"? If I were to manually build a table using a form and a submit button (which is what I eventually plan on doing) I could then load the data from a php file?
Yes, tables that are created in any way remain in the database until they are manually deleted. I think you're worried about tables being created as "temporary"; that doesn't normally happen.
1

There are SELECT and UPDATE queries to do this. For example if you want to show data in a table, you would use query looking like this:

SELECT * FROM ExampleTable

In PHP, you can work with these data for example like this:

$query = mysql_query("SELECT * FROM ExampleTable");
while ($row = mysql_fetch_array($query)) {
    echo $row["ColumnOne"];
}

And to the UPDATE query:

UPDATE ExampleTable SET ColumnOne = 'some value'

Usage in PHP is also with mysql_query. You can also use WHERE conditions in the query.

1 Comment

This was extremely useful - thank you! I especially appreciate the 'update' tip. thanks again
0

1 - Displaying All Data

<?php

// Conect to MYSQL
$con = mysql_connect("localhost", "My_Username", "My_Password") or die(mysql_error());
echo "Connected to MYSQL </br>";

mysql_select_db("My_Database") or die(mysql_error());
echo "Connected to Database";

// Get all the data from the "ExampleTable" table
$result = mysql_query("SELECT * FROM ExampleTable") or die(mysql_error());  

echo "<table border='1'>";
echo "<tr> <th>ColumnOne</th> <th>CoumnTwo</th> <th>CoumnThree</th> </tr>";

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {

    // Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $row['ColumnOne'];
    echo "</td><td>"; 
    echo $row['ColumnTwo'];
    echo "</td><td>"; 
    echo $row['ColumnThree'];
    echo "</td></tr>"; 
} 

echo "</table>";
?>

From: http://www.tizag.com/mysqlTutorial/mysqlselect.php

2 - Data Storage

Anything INSERT-ed into a database will remain* in the database until it is explicitly deleted.

*with few exceptions - but you'll learn about them a little later in your database journey :)

8 Comments

I've modified it so without too much trouble you should be able to drop it in.
Evan - perhaps ask another question to clarify your confusion reading the article.
Ok, so is he actually looping through each row here? I take back what I said i did not find THIS exact page before.
Yes, the tutorial uses a while loop to iterate all rows returned from the SELECT * FROM ... query.
I've edited my question to include hopefully an answer to your second part
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.