[root@kvm8 html]# mysql -uroot -p123qwe \ -e "use dyw; select * from dyw_tbl;" +--------+-----------+------------+-----------------+ | dyw_id | dyw_title | dyw_author | submission_date | +--------+-----------+------------+-----------------+ | 1 | PHP | david | 2014-12-24 | | 3 | MySQL | dywang | 2014-12-12 | | 4 | Linux | linda | 2014-12-24 | +--------+-----------+------------+-----------------+
dyw_id=1 的 dyw_title 為 python。
[root@kvm8 html]# mysql -uroot -p123qwe \ -e "use dyw; update dyw_tbl set dyw_title='python' where dyw_id=1;"
dyw_id=1 的 dyw_title 已更新為 python。
[root@kvm8 html]# mysql -uroot -p123qwe \ -e "use dyw; select * from dyw_tbl;" +--------+-----------+------------+-----------------+ | dyw_id | dyw_title | dyw_author | submission_date | +--------+-----------+------------+-----------------+ | 1 | python | david | 2014-12-24 | | 3 | MySQL | dywang | 2014-12-12 | | 4 | Linux | linda | 2014-12-24 | +--------+-----------+------------+-----------------+
[root@kvm8 html]# mysql -uroot -p123qwe \ -e "use dyw; update dyw_tbl set dyw_title='c++',dyw_author='peter' \ where dyw_id=1;"
dyw_id=1 的 dyw_title 及 dyw_author 2 個欄位都已更新。
[root@kvm8 html]# mysql -uroot -p123qwe \ -e "use dyw; select * from dyw_tbl;" +--------+-----------+------------+-----------------+ | dyw_id | dyw_title | dyw_author | submission_date | +--------+-----------+------------+-----------------+ | 1 | c++ | peter | 2014-12-24 | | 3 | MySQL | dywang | 2014-12-12 | | 4 | Linux | linda | 2014-12-24 | +--------+-----------+------------+-----------------+
$sql,改為 UPDATE 命令即可。
[root@kvm8 html]# vim updaterecord.php
[root@kvm8 html]# cat updaterecord.php
<?php
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = '123qwe';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'UPDATE dywphp_tbl
SET dywphp_title="python"
WHERE dywphp_id=2';
mysql_select_db('dywphp');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "Dywphp ID :{$row['dywphp_id']} <br> ".
"Title: {$row['dywphp_title']} <br> ".
"Author: {$row['dywphp_author']} <br> ".
"Submission Date : {$row['submission_date']} <br> ".
"--------------------------------<br>";
}
mysql_free_result($retval);
echo "Fetched data successfully\n";
mysql_close($conn);
?>
http://kvm8.deyu.wang/updaterecord.php,網頁上回傳 'Fetched data sucessfully'。
http://kvm8.deyu.wang/selectrecord.php,網頁上列出紀錄 dywphp_id=2 的 title 已變更為 python。