Below code will,
1. Make a sql connection to localhost (display error if something goes wrong).
2. Select the database "test" as the working db.
3. Create a table named example2 inside the "test" db.
4. Set the member variables and their data types in the table.
5. Insert 3 rows of data into the table.
6. Retrieve back the data from the table as a resource ($result).
7. Use the resource to print out the contents inside the table, with a while loop.
=============================================================
// 1. Make a MySQL Connection
mysql_connect("localhost") or die(mysql_error());
// 2.
mysql_select_db("test") or die(mysql_error());
// 3. Create a MySQL table in the selected database
mysql_query("CREATE TABLE example2(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
name VARCHAR(30),
age INT)")
or die(mysql_error());
// 5. Insert rows of information into the table "example2"
mysql_query("INSERT INTO example2
(name, age) VALUES('Timmy Mellowman', '23' ) ")
or die(mysql_error());
mysql_query("INSERT INTO example2
(name, age) VALUES('Sandy Smith', '21' ) ")
or die(mysql_error());
mysql_query("INSERT INTO example2
(name, age) VALUES('Bobby Wallace', '15' ) ")
or die(mysql_error());
// 6. Retrieve all the data from the "example2" table
$result = mysql_query("SELECT * FROM example2")
or die(mysql_error());
// 7. store the record of the "example2" table into $row
// Print out the contents of the entry
while($row = mysql_fetch_array($result)){
echo $row['name']. " - ". $row['age'];
echo "
";
}
?>
=============================================================
(( SQL help ))
SQL CREATE TABLE Syntax
CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
....
)
SQL INSERT INTO Syntax
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
SELECT * Example
The SELECT statement is used to select data from a database.
The result is stored in a result table, called the result-set.
The mysql_query() function
Executes a query on a MySQL database.
The mysql_fetch_array() function
Returns a row from a recordset
No comments:
Post a Comment