• Retrieving data from MySQL using PHP • CONNECTIONS: login functionality. • Deleting records in MySQL using PHP[r]
Trang 22
Trang 4• Creating database in MySQL using WAMP
database
web application with file upload
4
Trang 5• Retrieving data from MySQL using PHP
• CONNECTIONS: login functionality
Trang 6• Connection with database
• Make display structure
• Write data
6
Trang 7mysql_connect( “localhost” , ”root” , ”” ) or die( “Error in connection” );
die( “Error in Selection” );
?>
Trang 8select data from one or more tables:
• SELECT command in SQL:
FROM table-name
SELECT user_Name
FROM users
SELECT *
FROM users
Trang 9• Condition selection:
FROM table-name
SELECT *
Trang 10<?php
include(‘connection.php’);
$sql = ‘ select * from users ’;
$result = mysql_query($sql);
?>
Trang 11• Counting rows:
– mysql_num_rows(variable);
<?php
include(‘connection.php’);
$sql = ‘ select * from users ’;
$result = mysql_query($sql);
$users = mysql_num_rows($result);
Trang 12<table border=‘1’>
<tr>
<th> User Name</th>
<th> User Email</th>
<th> User Password</th>
<th> User Picture</th>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
Trang 13• mysql_fetch_array(result-resource);
– mysql_fetch_array( $result );
Trang 14$result=
$row = mysql_fetch_array($result);
1 Ali ali@yahoo.com 123 upload/123ali.jpg
2 Umar umar@yahoo.com 123 upload/123umar.jpg
$row= 1 Ali ali@yahoo.com 123 upload/123ali.jpg
user_I d
user_Nam e
user_Emai l
user_Passwo rd
user_Pictur e
echo $row
[ 1 ];
echo
$row[ ‘user_Name’ ];
Trang 15<table border=‘1’>
<tr>
<th> User Name</th>
<th> User Email</th>
<th> User Password</th>
<th> User Picture</th>
</tr>
<tr>
<td> <?php echo $row[1]; ?> </td>
<td> <?php echo $row[2]; ?> </td>
<td> <?php echo $row[3]; ?> </td>
<td> <img src= “<?php echo $row[4]; ?
User Name
User Email User
Password
User Picture Ali ali@yahoo.co
Trang 16<table border=‘1’>
Heading Row
<?php
while($rows =
mysql_fetch_array($result))
{
?>
<tr>
<td> <?php echo $row[1]; ?> </td>
<td> <?php echo $row[2]; ?> </td>
<td> <?php echo $row[3]; ?> </td>
<td> <img src= “<?php echo $row[3]; ?
>”> </td>
</tr>
<?php } ?>
</table>
Name Password Picture Ali ali@yahoo.co
Umar umar@yahoo.
com
123
Trang 17Starts a HTML page
Connection to database
Select command Query executed
Trang 18Headin
g row
Loop starts Keeps row
Trang 19Displays name
Displays email Displays
password
Displays
image Sets source
Ends loop
Trang 20Records in user’s table
Output from the table