Mysql PHP is used to execute the select statement using mysql_query ( ) function. The function send a query or command to a MySQL connection.
Understand with Example
The Tutorial illustrate an example from 'Mysql PHP Select'. To understand and elaborate example we create a table 'MyTable' that has the required fieldnames and datatypes respectively.
Query for creating table named MyTable :
The Query insert into is used to add the records or rows to the table 'MyTable'.
mysql> CREATE TABLE MyTable (
-> Empid int(10),
-> Empname varchar(60)
-> Salary int(90)
-> );
Query OK, 0 rows affected (0.13 sec)
Query for inserting data in table:
The Query insert into is used to add the records or rows to the table 'MyTable'.
mysql> insert into MyTable values(01,'Girish','20000'); Query OK, 1 row affected (0.02 sec) mysql> insert into MyTable values(02,'A','21000'); Query OK, 1 row affected (0.01 sec) mysql> insert into MyTable values(03,'C','22000'); Query OK, 1 row affected (0.00 sec) mysql> insert into MyTable values(04,'V','23000'); Query OK, 1 row affected (0.00 sec) mysql> insert into MyTable values(05,'B','24000'); Query OK, 1 row affected (0.00 sec) mysql> insert into MyTable values(06,'E','25000'); Query OK, 1 row affected (0.00 sec) mysql> insert into MyTable values(07,'Q','26000'); Query OK, 1 row affected (0.01 sec) mysql> insert into MyTable values(08,'W','27000'); Query OK, 1 row affected (0.01 sec) mysql> insert into MyTable values(09,'AS','28000'); Query OK, 1 row affected (0.00 sec)`
Query to view data inserted in table:
mysql> select * from mytable; +-------+---------+--------+ | Empid | Empname | Salary | +-------+---------+--------+ | 1 | Girish | 20000 | | 2 | A | 21000 | | 3 | C | 22000 | | 4 | V | 23000 | | 5 | B | 24000 | | 6 | E | 25000 | | 7 | Q | 26000 | | 8 | W | 27000 | | 9 | AS | 28000 | +-------+---------+--------+ 9 rows in set (0.01 sec)
Viewing table data using php select:
To view the table data using php, you need to connect with database to access the records. The mysql _connect function ( ) is used to connect the database. As your connection is built, you can retrieve the records from table 'mytable' using select query. Now we make use of mysql_fetch_array ( ) function to return the first row from the recordset of an array. Each call make to mysql_fetch_array ( ) returns you the next rows in the recordset. The While loop iterate through all the records in a data. The PHP $ROW variable is used to print the value of each row from a table.
<?php
$database="girish";
$user="root";
$password="root";
$host="192.168.10.126";
$link= mysql_connect($host,$user,$password) or die("Could not connect: ".mysql_error());
mysql_select_db($database,$link) or die("Error in selecting the database:".mysql_error());
$sql="Select * from mytable";
$sql_result=mysql_query($sql,$link) or exit("Sql Error".mysql_error());
$sql_num=mysql_num_rows($sql_result);
echo "<table border=\"1\" bgcolor=\"#CCCCFF\" >";
echo "<tr><td><b>Empid</b></td><td><b>Empname</b></td><td><b>Salary</b></td>" ;
while($sql_row=mysql_fetch_array($sql_result))
{
$Empid=$sql_row["Empid"];
$Empname=$sql_row["Empname"];
$Salary=$sql_row["Salary"];
echo "<tr>";
echo "<td>".$Empid."</td>";
echo "<td>".$Empname."</td>";
echo "<td>".$Salary."</td>";
echo "</tr>";
}
echo "</table>";
?>
Output:-
![]() |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Mysql PHP Select View All Comments
Post your Comment