
时间:2015-06-28 00:00 来源:IT猫扑网|http://www.itmop.com/ 作者:网管联盟 我要评论(0)
连接
<?php
//下面使用了函数库mysqli,所以需要将php.ini文件中的extension=php.mysqli 打开
@ $db = new mysqli('localhost', 'root', 'tograce', 'books');
//以上代码实例化了mysqli类并且创建了到主机'localhost'的连接,该连接使用的用户名为和密码分别是:root,tograce,同时连接上books数据库
if (mysqli_connect_errno())
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
else{echo &gong xi,Connected successfully&;}
$query = &select * from books&;
$result = $db->query($query);
$num_results = $result->num_rows;
echo '<p>Number of books found: '.$num_results.'</p>';
$result->free();
$db->close();
?>
插入数据
摘自<PHP和MySQL Web开发>第11章
<html>
<head>
<title>Book-O-Rama Book Entry Results</title>
</head>
<body>
<h1>Book-O-Rama Book Entry Results</h1>
<?php
// create short variable names
$isbn=$_POST['isbn'];
$author=$_POST['author'];
$title=$_POST['title'];
$price=$_POST['price'];
if (!$isbn || !$author || !$title || !$price)
{
echo 'You have not entered all the required details.<br />'
.'Please go back and try again.';
exit;
}
if (!get_magic_quotes_gpc())
{
$isbn = addslashes($isbn);
$author = addslashes($author);
$title = addslashes($title);
$price = doubleval($price);
}
@ $db = new mysqli('localhost', 'root', 'tograce', 'books');
if (mysqli_connect_errno())
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
$query = &insert into books values
('&.$isbn.&', '&.$author.&', '&.$title.&', '&.$price.&')&;
$result = $db->query($query);
if ($result)
echo $db->affected_rows.' book inserted into database.';
$db->close();
?>
</body>
</html>
关键词标签:PHP,MYSQL数据库
相关阅读 2021年最好用的10款php开发工具推荐 plsql developer怎么连接数据库-plsql developer连接数据库方法 php利用淘宝IP库获取用户ip地理位置 在 PHP 中使用命令行工具 php出现Cannot modify header information问题的解决方法大全 详解ucenter原理及第三方应用程序整合思路、方法
热门文章
2021年最好用的10款php开发工具推荐
plsql developer怎么连接数据库-plsql developer连接数据库方法
在 PHP 中使用命令行工具
php应用程序安全防范技术研究
关于php curl获取301或302转向的网址问题
人气排行 详解ucenter原理及第三方应用程序整合思路、方法 plsql developer怎么连接数据库-plsql developer连接数据库方法 PHP中防止SQL注入攻击 PHP会话Session的具体使用方法解析 PHP运行出现Notice : Use of undefined constant 的解决办法 PHP如何清空mySQL数据库 CakePHP程序员必须知道的21条技巧 PHP采集图片实例(PHP采集) 完美解决failed to open stream: HTTP request failed! <? phpinfo() ?>无法执行的解决办法 2021年最好用的10款php开发工具推荐 用header或meta实现PHP页面编码的区别
查看所有0条评论>>