一 代码
<?php include_once ('../adodb5/adodb.inc.php'); $conn = ADONewConnection('mysql'); $conn -> PConnect('localhost','root','root','db_database14'); $conn -> execute('set names gb2312'); $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>使用getInsertSQL()函数向表tb_object中插入一条新数据</title> <style type="text/css"> <!-- body,td,th { font-size: 12px; } body { margin-left: 10px; margin-top: 10px; margin-right: 10px; margin-bottom: 10px; } --> </style></head> <body> <?php include_once ('../adodb5/adodb.inc.php'); //载入(include)adodb.inc.php文件 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; //设置公共变量值,结果集按照字段名称为索引进行存取 $conn = ADONewConnection('mysql'); //建立连接 $conn -> PConnect('localhost','root','root','db_database14'); //连接数据库 $conn -> execute('set names gb2312'); //设置编码格式 $sqlstr = 'select * from tb_object where id = 0'; //生成sql语句 $rst = $conn -> execute($sqlstr) or die('Error: '.$conn -> errorMsg()) ; //执行SQL语句 $fields = array(); //创建一个数组 $fields['bigclass'] = '办公用品'; //向数组中添加数据 $fields['d_time'] = date("Y-m-d H:i:s"); $insert = $conn -> getInsertSQL($rst,$fields) or die('update error:'.$conn -> errorMsg());//添加新数据 $conn -> execute($insert); //执行添加 echo '添加的数据为:'; $showsql = "select * from tb_object where d_time = '".$fields['d_time']."'"; //创建sql语句 $rst = $conn -> execute($showsql); //查找刚刚创建的记录 foreach($rst -> fields as $nm => $vl) //使用foreach输出新记录 echo $nm.'=>'.$vl.' '; $rst -> close(); //关闭连接 $conn -> close(); ?> </body> </html>
二 运行结果
添加成功