|
|
| DATABASE AND PHP |
|
When you want to store some data, either how many viewers your page have registered,
or you want to make guestbooks or forums, you have to store the data. By using database you
get well structured data, that you easily can access. In this tutorial I will show
how you can connect, read, update and write to a mysql database.
|
The connection
|
|
In order to connect to a mysql database you have to know the host, the username and password
for editing the database and the name of the database. The connection code is given below:
|
$host = "localhost";
$username = "thename";
$password = "mypsw";
$database = "ApronBase";
$server = mysql_connect($host, $username, $password);
$connection = mysql_select_db($database, $server);
|
Disconnect
|
|
When you have have updated, inserted or read the recordsset you wanted, it
is important to close your connection. The disconnect code is given below:
|
Get data
|
|
When you want to get data from a database, you use sql statements. The sql statement
then return a recordset which you may list. In my database I have a table called
'Users' where I have stored all the users registered in my site, in a Name colomn. To get hold of
the users I first use a sql statement and then I use php scripting to go true the
recordset.
|
//get the recordset with sql
$rs = mysql_query("SELECT * FROM USERS");
//print out all the names in table users
while ($row=mysql_fetch_assoc($rs))
{
$name = $row['Name'];
echo($name);
}
|
Insert Data
|
|
When you want to insert data from a database, you also use sql statements. By calling
the sql statement below you get inserts a new name into the Users table:
|
$name = "a new name";
$sql = mysql_query("INSERT INTO Users VALUES ( '$name' ");
|
Insert Data
|
|
When you want to insert data from a database, you also use sql statements. By calling
the sql statement below you get inserts a new name into the Users table:
|
$name = "a new name";
$sql = mysql_query("INSERT INTO Users VALUES ( '$name' ");
|
Delete Data
|
|
When you want to delete data from a database, you also use sql statements. By calling
the sql statement below you delete a new name from the Users table:
|
$name = "a new name";
$rs = mysql_query("DELETE FROM Users WHERE Name LIKE '$name'");
|
Insert Data
|
|
When you want to insert data from a database, you also use sql statements. By calling
the sql statement below you get inserts a new name into the Users table:
|
$name = "a new name";
$sql = mysql_query("INSERT INTO Users VALUES ( '$name' ");
|
Delete Data
|
|
When you want to delete data from a database, you also use sql statements. By calling
the sql statement below you delete a new name from the Users table:
|
$name = "a new name";
$rs = mysql_query("DELETE FROM Users WHERE Name LIKE '$name'");
|
Insert Data
|
|
When you want to insert data from a database, you also use sql statements. By calling
the sql statement below you get inserts a new name into the Users table:
|
$name = "a new name";
$sql = mysql_query("INSERT INTO Users VALUES ( '$name' ");
|
Update Data
|
|
When you want to update data from a database, you also use sql statements. By calling
the sql statement below you replace the name 'Jack' with 'Jackson' from the Users table:
|
$oldname = "Jack";
$newname = "Jackson";
$sql = mysql_query("UPDATE tbForum SET Name = '$newname' WHERE Name LIKE '$oldname' ");
|
|
Category
|
Programming with PHP
|
|
Author
|
Tommy Johannessen
|
|
|
|