›Insert and Select data from Mysql Database
In this tutorial i will teach how to INSERT and SELECT data from a MySql Database, this is a beginner tutorial, this tutorial will also teach you the using of @mysql_fetch_array Function in PHP.
Lets Start
First of all we have to create a new database “mydb” (if not exist):
CREATE DATABASE mydb
Now lets add a table “data” to the “mydb” database:
CREATE TABLE `mydb`.`data` ( `fname` VARCHAR( 225 ) NOT NULL , `lname` VARCHAR( 225 ) NOT NULL ) ENGINE = MYISAM ;
Explainations:
fname: it will show the First Name.
lname: it will show the Last Name.
Connectting and Selecting Database
Click here to learn how to connect and select a database.
Inserting Data in Database
before inserting you must be connected to the “mydb” database, to learn how to connect click on the above link^.
Method 1:
@mysql_query("INSERT INTO data SET fname='Abdul', lname='Khaliq'");
@mysql_query("INSERT INTO data SET fname='Shah', lname='Khan'");
Method 2:
@mysql_query("INSERT INTO data (fname, lname)
VALUES ('Abdul', 'Khaliq')");
@mysql_query("INSERT INTO data (fname, lname)
VALUES ('Shah', 'Khan')");
[NOTE] Method 1 & 2 boths are working fine…
Selecting Data from Database
$result = @mysql_query("SELECT * FROM data");
while($show = @mysql_fetch_array($result)){
echo $show['fname'];
echo " ";
echo $show['lname'];
echo "
";
Output
Abdul Khaliq Shah Khan
Explainations:
*: it means ‘all’
$result: it is a variable.
@mysql_query: it is a function, This function is used to send a query or command to a MySQL connection.
SELECT * FROM data: it takes all the data from “data” table.
@mysql_fetch_array: we use the mysql_fetch_array() function to return the first row from the recordset as an array. Each subsequent call to mysql_fetch_array() returns the next row in the recordset.
$show: it is a variable.
Enjoy, if you need help, just write it below





