PHP 3 Steps

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Select data From mysql

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();
?>

replace with this


$rows=mysql_fetch_array($result);
.........
<?php
mysql_close();
?>

STEP1: Create table "test_mysql"


In this step, you have to create a table and insert data for testing our code.

CREATE TABLE `test_mysql` (


`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default '',
`lastname` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=7 ;
--- Dumping data for table `test_mysql`
-INSERT
INSERT
INSERT
INSERT
INSERT
INSERT

INTO
INTO
INTO
INTO
INTO
INTO

`test_mysql`
`test_mysql`
`test_mysql`
`test_mysql`
`test_mysql`
`test_mysql`

VALUES
VALUES
VALUES
VALUES
VALUES
VALUES

STEP2: Create file - Select.php

(1,
(2,
(3,
(4,
(5,
(6,

'Billly', 'Blueton', '[email protected]');


'Jame', 'Campbell', '[email protected]');
'Mark', 'Jackson', '[email protected]');
'Linda', 'Travor', '[email protected]');
'Joey', 'Ford', '[email protected]');
'Sidney', 'Gibson', '[email protected]');

<?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);
?>

<table width="400" border="1" cellspacing="0" cellpadding="3">

<?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();
?>

STEP3: Run the Code

Insert data into mysql


This tutorial will show you how to insert data into mysql database.

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

STEP1: Create table "test_mysql"


CREATE TABLE `test_mysql` (
`id` int(4) NOT NULL auto_increment,

`name` varchar(65) NOT NULL default '',


`lastname` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)

) TYPE=MyISAM AUTO_INCREMENT=0 ;

STEP2: Create file insert.php


View In Browser

############### Code

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">


<tr>
<td><form name="form1" method="post" action="insert_ac.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><strong>Insert Data Into mySQL Database </strong></td>
</tr>
<tr>
<td width="71">Name</td>
<td width="6">:</td>
<td width="301"><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td>Lastname</td>
<td>:</td>
<td><input name="lastname" type="text" id="lastname"></td>
</tr>

<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>

STEP3: Create file insert_ac.php


Diagram

############### 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();
?>

Update data in mysql


Update/Edit data from mysql database, can do it easily.

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

STEP1: Set Up Database

Create database and table using phpMyAdmin

CREATE TABLE `test_mysql` (


`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default '',
`lastname` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=7 ;
--- Dumping data for table `test_mysql`
-INSERT
INSERT
INSERT
INSERT
INSERT
INSERT

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,

'Billly', 'Blueton', '[email protected]');


'Jame', 'Campbell', '[email protected]');
'Mark', 'Jackson', '[email protected]');
'Linda', 'Travor', '[email protected]');
'Joey', 'Ford', '[email protected]');
'Sidney', 'Gibson', '[email protected]');

STEP2: Create file - list_records.php

############### 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>

<table width="400" border="1" cellspacing="0" cellpadding="3">


<tr>
<td colspan="4"><strong>List data from mysql </strong> </td>
</tr>
<tr>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
<td align="center"><strong>Update</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['name']; ?></td>
<td><? echo $rows['lastname']; ?></td>
<td><? echo $rows['email']; ?></td>
// link to update.php and send value of id
<td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>

STEP3: Create file - update.php

############### 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);
?>

<table width="400" border="0" cellspacing="1" cellpadding="0">


<tr>

<form name="form1" method="post" action="update_ac.php">

<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td align="center">&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</td>
</tr>
</table>
</td>
</form>

</tr>
</table>
<?php

// close connection
mysql_close();
?>

STEP4: Create file - update_ac.php

############### 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

You might also like