Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Sql Mysql-alter MySQL PHP Update

Related Tutorials


 
 

Share on Google+Share on Google+

MySQL PHP Update

Advertisement
MySQL PHP Update uses mysql_update function ( ) that is used to update the records from table in the database.

MySQL PHP Update

     

MySQL PHP Update uses mysql_update function ( ) that is used to update the records from table in the database. 

Understand with Example

The Tutorial illustrate an example from 'MySQL PHP Update'. To understand and grasp the example we create a table 'MyTable' with required fieldname and datatypes respectively.

Query for creating table named 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 insert into keyword adds the records to table 'MyTable'.

mysql>   insert into MyTable values(01,'Girish','20000');
Query OK, 1 row affected (0.02 sec)
mysql>   insert into MyTable values(10,'Komal','20000');
Query OK, 1 row affected (0.01 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 |
| 10    | Komal   |  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 |
+-------+---------+--------+
10 rows in set (0.00 sec)

updating table data using php:

 The PHP include the update mysql function query () that is used to update the table 'mytable' and set a new value 'Amit'  for empid '2' as specified in Where Clause. The mysql_fetch_array function ( ) is used to return the first row records. Each call to this function fetches the next row values from table 'mytable'. The While loop iterate the records value from recordset in the table 'mytable'. The echo print the required output records from table 'mytable'.

<?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="update mytable set empname=\"Amit\" where empid=2";
mysql_query($sql,$link) or exit("Sql Error".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:-

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 6, 2010

Related Tutorials

Discuss: MySQL PHP Update  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com