Home  >  Article  >  php教程  >  使用mysqli方式进行数据库查询

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

PHP中文网
PHP中文网Original
2016-05-22 17:22:19967browse

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();
Statement:
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