php5.3以上版本不建议使用mysql_connect ,mysql_query等函数,转而使用mysqli系列(improved)。这是连接数据库及查询数据的两个例子。
- <?php
- require 'conn.php'; //$link=mysqli_connect($host,$user,$pass,$database);
- $query="select * from user_info";
- if($result=mysqli_query($link,$query)){
- echo "query was successfully executed.".'<br>';
- while($row=mysqli_fetch_assoc($result)){
- echo $row["name"]." ".$row["surname"].'<br>';
- }
- mysqli_free_result($result);
- }
- else{
- echo "the query is not executed.".'<br>';
- }
- mysqli_close($link);
- ?>
- <?php
- /**
- * Created by PhpStorm.
- * User: KPF
- * Date: 2016/6/28
- * Time: 17:21
- */
- require_once('json.php');
- /*class Db{
- static private $_instance;
- public function __construct()
- {
- }
- }*/
- $connect=mysqli_connect('127.0.0.1','root','606','tcb');
- /* check connection */
- if (mysqli_connect_errno()) {
- printf("Connect failed: %s\n", mysqli_connect_error());
- exit();
- }
- $sql="select * from user_info";
- $result=mysqli_query($connect,$sql);
- if (!$result) {
- printf("Error: %s\n", mysqli_error($connect));
- exit();
- }
- $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
- printf("%s %s %s\n", $row['user_name'], $row['user_pass'],$row['user_faceurl']);
- /*$row=array();
- while($row=mysqli_fetch_assoc($result)){
- $row[]=$row;
- }
- if($row){
- return Response::show(200,'首页数据返回成功',$row);
- }*/
- mysqli_close($connect);
- ?>