php mysql编程的基本步骤:

  1. 连接mysql服务器 -- mysql_connect()函数
  2. 选择数据库 -- mysql_select_db()函数
  3. 执行sql语句 -- mysql_query()函数
  4. 如果有数据集返回,还需要处理数据集 -- mysql_fetch_array()函数

知道了PHP操作数据库的步骤,那么要实现php向mysql表中添加一条数据非常简单,请看源码:

<?php
$user = \"root\";
$pass = \"\";
$db = \"mydata \";
$  = @mysql_connect( \"localhost\", $user, $pass );
if ( ! $  ) {
  die( \"Couldn\'t connect to MySQL: \".mysql_error() );
}
/* http://www.manongjc.com/article/1238.html */
print \"<h2>Successfully connected to server</h2>\\n\\n\";
@mysql_select_db( $db ) or die ( \"Couldn\'t open $db: \".mysql_error() );
print \"Successfully selected data  \\\"$db\\\"<br />\\n\";

$query = \"INSERT INTO domains( domain, sex, mail )values( \'example.com\', \'F\', \'a@example.com\' )\";
print \"running query: <br />\\n$query<br />\\n\";
mysql_query( $query, $  ) or die ( \"INSERT error: \".mysql_error() );
mysql_close( $  );
?>
收藏 打印