PHP mysqli扩展库操作(1)

 
更多
<?php
header ( 'http-equiv="Content-Type" content="text/html; charset=utf-8"' );
$DB_host = "localhost"; //数据库主机
$DB_user = "root"; //数据库用户
$DB_psw = "sqlroot"; //数据库密码
$DB_datebase = "massageboard"; //数据库名
$DB_charset = "utf8"; //数据库字符集


$db = new mysqli ( $DB_host, $DB_user, $DB_psw ); //实例化对象


//检查连接
if (mysqli_connect_errno ()) {
	printf ( "Connect failed: %s\n", mysqli_connect_error () );
	exit ();
}

$db->select_db ( $DB_datebase ); //选择操作数据库


$db->set_charset ( $DB_charset ); //设置数据库字符集


//$sql = "INSERT INTO `massage` (`userid`,`title`, `content`, `date`, `ip`) VALUES ('2','小明加油', '小明加油',UNIX_TIMESTAMP(), '127.0.0.1')"; //UNIX_TIMESTAMP()是mysql中的一个时间函数,返回当前时间戳
//if ($db->query ( $sql )) {
//	echo '成功写入数据!<br/>';
//	echo $db->affected_rows . '条记录受影响<br/>'; //affected_rows返回非select语句操作受影响的行数
//	echo '自动增长ID:' . $db->insert_id . '<br/>'; //insert_id返回最后自动增长id
//} else {
//	echo '写入数据失败!<br/>';
//	echo '<font color="red">Error(' . $db->errno . ')' . $db->error . '</font><br/>'; //显示错误信息
//	exit ();
//}


//$sql = "delete from massage where date != UNIX_TIMESTAMP()";
//$result = $db->query ( $sql );
//if ($result) {
//	echo $db->affected_rows . '条记录受影响<br/>'; //affected_rows返回非select语句操作受影响的行数
//}


//执行一个查询
$sql = 'select * from massage';
$result = $db->query ( $sql );

echo $result->num_rows . ' 行结果  ' . $result->field_count . ' 列内容<br/>';

//$result->data_seek('5');//从结果集中第5条开始取结果


echo '<table border="1" cellspacing="0" cellpadding="0" align="center" width="90%">';

//循环输出字段名
//$result->field_seek(2);//从字段集中第2条开始取结果
while ( true == ($field = $result->fetch_field ()) ) {
	echo '<th>' . $result->current_field . '_' . $field->name . '(' . $field->length . ')</th>';
}

//循环输出查询结果
while ( true == ($row = $result->fetch_assoc ()) ) {
	echo '<tr>';
	foreach ( $row as $col ) {
		echo '<td align="center">' . $col . '</td>';
	}
	echo '</tr>';
}

echo '</table>';
$result->free ();//释放结果集
$db->close (); //关闭连接
?>
打赏

本文固定链接: https://www.cxy163.net/archives/268 | 绝缘体

该日志由 绝缘体.. 于 2012年02月26日 发表在 MySQL, PHP 分类下,
原创文章转载请注明: PHP mysqli扩展库操作(1) | 绝缘体

报歉!评论已关闭.