• My PHP Pages

    This is a complete and free PHP programming course for beginners. Everything you need to get started is set out in section one below.

Jul 2, 2013

Posted by Unknown
1 comment | 9:34 AM

You can easily update and delete data in mysql tables using php. This update & delete functionality is almost same as insert data. just sql query is difference.

Lets look at how to update data. for this you have to use UPDATE sql statement. lets look at the code below.



//make connection
$con = mysql_connect("localhost","root","123");
//check connection
if(!$con)
{
die('Could not connect to the database:'.mysql_error());
}

//select database
$db=mysql_select_db("my_database",$con);
 if(!$db)
{
die('Could not connect to the database:'.mysql_error());

}
//making sql update query string.
$sql="UPDATE my_table SET name = 'Nimal' WHERE tp_num = 0771239845";

//Execute the mysql query and check is execution process is succeeded
$values = mysql_query($con,$sql);
if(isset($values))
{
echo "Successfully Updated..!";
}
else
{
die('Error Occurred'.mysql_error($con));
}

//close connection
mysql_close($con);

I think above script is not hard to understand  so lets move to the Delete statement. to delete a value we need to use sql DELETE statement. lets roll to the cording part.




//make connection
$con = mysql_connect("localhost","root","123");
//check connection
if(!$con)
{
die('Could not connect to the database:'.mysql_error());
}

//select database
$db=mysql_select_db("my_database",$con);
 if(!$db)
{
die('Could not connect to the database:'.mysql_error());

}
//making sql delete query string.
$sql="DELETE FROM my_table WHERE tp_num = 0771239845";

//Execute the mysql query and check is execution process is succeeded
$values = mysql_query($con,$sql);
if(isset($values))
{
echo "Successfully Deleted..!";
}
else
{
die('Error Occurred'.mysql_error($con));
}

//close connection
mysql_close($con);

So basically we have covered basic things that you can do using mysql and php. On this tutorials we've been using mysql_xxx functions but PHP has introduce new set of functions like mysqli_xxx and PDO. we will go through those functions here after.Cheers...



1 comment :

  1. nice tutorial thank you very much. i am much appreciate that you going to do PDO tutorials too. thank you very much it helps a lot. keep it up sir.

    ReplyDelete