turrier.fr

Source : ct|09.01.09

< Tutorials Computer, Multimedia, Chinese

Adding data in a MySQL database
since a PHP page

In this tutorial we will suppose that you know how to:
- Install and use Easyphp;
- Create a mysql database with easyphp and phpmyadmin;
We will use the database "test" and the table "visiteurs" created in the tutotial 03: Create a mysql database with easyphp and phpmyadmin.

Writing the PHP program

Study then write the following sample program. This program allows to add two lines of data in the table "visiteurs" of the mysql database "test". Record it in a text file named "essai.php", for example, and place it in the directory "www" of easyphp.

image1

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Document sans nom</title>
</head>
<body>
<?php
$result=mysql_connect("localhost", "root", "mysql");
mysql_select_db("test");
print "ma base est ouverte";echo "<br/>";
mysql_query("DELETE FROM visiteurs ");
mysql_query("INSERT INTO visiteurs (id, nom, motdepasse) VALUES(1,'tata','11111')");
mysql_query("INSERT INTO visiteurs (nom, motdepasse) VALUES('toto','22222')");
$reponse = mysql_query("SELECT * FROM visiteurs");
while ($ligne=mysql_fetch_array($reponse))
{
echo $ligne['nom']; echo "&nbsp;&nbsp;";
echo $ligne['motdepasse']; echo "<br/>";
}
mysql_close();
?>
</body>
</html>

WARNING!
It is to note that, in the version 3.0 of easyphp, the password of access by default to local mysql databases is "mysql". It is not any more "" as in previous versions. By default, it is necessary to use the password "mysql" and not "". For example, the writing of the following line gives the error message indicated below.

Warning: mysql_connect() [function.mysql-connect]: Accès refusé pour l'utilisateur: 'root'@'@localhost' (mot de passe: NON) in C:\Program Files\EasyPHP 3.0\www\essai.php on line 10

Running the PHP program

From local web, launch the execution of the page "essai.php".

image2

Both recordings writed in the php program are immediately created in the table "visiteurs" in the database "test".

image3

Consult the table with phpmyadmin

Open phpmyadmin, select the datbase "test" and the table "visiteurs", then click the small picture "afficher".

image4

Phpmyadmin displays The contents of the table "visiteurs". The datas contained in this table are well the ones which were inserted with the php program contained in the page "essai.php".

image5


Valid XHTML 1.0 Transitional

© http://turrier.fr (2007)