Home > php教程 > PHP源码 > body text

使用mysqli方式进行数据库查询

PHP中文网
Release: 2016-05-22 17:22:19
Original
962 people have browsed it

php代码

<?php

// 设置返回json格式数据
header(&#39;content-type:application/json;charset=utf8&#39;);

$mysql_server_name = "localhost"; //数据库服务器名称
$mysql_username = "root"; // 连接数据库用户名
$mysql_password = "Letmein@0227"; // 连接数据库密码
$mysql_database = "stalker"; // 数据库的名字
$mysql_port = "3306"; //端口号
$conn; //数据库连接标识;
$result; //执行query命令的结果资源标识


//Open a new connection to the MySQL server
$conn = new mysqli($mysql_server_name, $mysql_username, $mysql_password, $mysql_database, $mysql_port);
//Output any connection error
if($conn->connect_error) {
    die(&#39;Error:(&#39;.$conn->connect_errno .&#39;) &#39;.$conn->connect_error);
}


<?php

$sql; //sql执行语句
$row; //返回的条目数

//要执行的SQL语句
$sql="SELECT * FROM `personinfo`";

$results = array();
$i = 0;

//调用ConnectionMySQL.php文件进行数据库连接操作
require(&#39;ConnectionMySQL.php&#39;);

foreach($conn->query($sql) as $row) {
    $results[$i] = $row;
    $i++;
}
// 将结果转成json格式
echo json_encode(array(&#39;jsonObj&#39;=>$results));

// 关闭连接
$conn->close();
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!