PHP 3 Steps
PHP 3 Steps
PHP 3 Steps
When you need data from your mysql database to show in your web page, you need to select
database, table and what's row you want to pull its data first.
Syntax
// Select all columns from all rows.
"SELECT * FROM table_name";
or
// Select some column from all rows.
"SELECT column_name1, column_name2 FROM table_name";
or
// Select all coulumns from one row.
"SELECT * FROM table_name WHERE column_name=' value in column '";
Overview
In this tutorial, we'll create only 1 file.
1. select.php
Steps
1. Create table "test_mysql" in database "test".
2. Create file select.php.
3. test it!
If you don't want looping rows in mysql, replace
while($rows=mysql_fetch_array($result)){
........
<?php
}
mysql_close();
?>
INTO
INTO
INTO
INTO
INTO
INTO
`test_mysql`
`test_mysql`
`test_mysql`
`test_mysql`
`test_mysql`
`test_mysql`
VALUES
VALUES
VALUES
VALUES
VALUES
VALUES
(1,
(2,
(3,
(4,
(5,
(6,
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Retrieve data from database
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
<?php
// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td width="10%"><?
<td width="30%"><?
<td width="30%"><?
<td width="30%"><?
</tr>
<?php
// close while loop
}
</table>
?>
echo
echo
echo
echo
$rows['id']; ?></td>
$rows['name']; ?></td>
$rows['lastname']; ?></td>
$rows['email']; ?></td>
<?php
// close MySQL connection
mysql_close();
?>
Syntax
"INSERT INTO table_name(column_name1, column_name2)VALUES('value1, 'value2')" ;
Overview
In this tutorial, create 2 files
1. insert.php
2. insert_ac.php
Steps
1. Create table "test_mysql" in database "test".
2. Create file insert.php.
3. Create file insert_ac.php.
If you don't know how to create database and table click here to learn
) TYPE=MyISAM AUTO_INCREMENT=0 ;
############### Code
<tr>
<td>Email</td>
<td>:</td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
############### Code
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$name=$_POST['name'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
// Insert data into mysql
$sql="INSERT INTO $tbl_name(name, lastname, email)VALUES('$name', '$lastname',
'$email')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
<?php
// close connection
mysql_close();
?>
Syntax
"UPDATE table_name SET column_name1=' value', column_name2=' value' WHERE column_name='
value' ";
Overview
In this tutorial, we create 3 PHP files for testing our code.
1. list_records.php
2. update.php
3. update_ac.php
Steps
1. Create table "test_mysql" in database "test"
2. Create file list_records.php
3. Create file update.php
4. Create file update_ac.php
INTO
INTO
INTO
INTO
INTO
INTO
`test_mysql`
`test_mysql`
`test_mysql`
`test_mysql`
`test_mysql`
`test_mysql`
VALUES
VALUES
VALUES
VALUES
VALUES
VALUES
(1,
(2,
(3,
(4,
(5,
(6,
############### Code
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
############### Code
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<tr>
<td> </td>
<td align="center">
<input name="name" type="text" id="name" value="<? echo $rows['name']; ?>">
</td>
<td align="center">
<input name="lastname" type="text" id="lastname" value="<? echo $rows['lastname']; ?>"
size="15">
</td>
<td>
<input name="email" type="text" id="email" value="<? echo $rows['email']; ?>" size="15">
</td>
</tr>
<tr>
<td> </td>
<td>
<input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit">
</td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// close connection
mysql_close();
?>
############### Code
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database
$sql="UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' WHERE
id='$id'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}
else {
echo "ERROR";
}
?>
Reference: http://www.phpeasystep.com/index.php