fork download
  1. <?php
  2.  
  3. header('content-Type:text/html;charset=utf-8');//设置页面编码,如果文件是gbk编码,则charset也应用gbk
  4.  
  5.  
  6.  
  7. function connect() {
  8. $link = mysqli_connect("localhost", "root", "123456", "employees");//面向过程
  9. $link = new mysqli("localhost", "root", "123456", "employees");//面向对象
  10.  
  11. $link->query("SET NAMES utf8");//执行语句
  12.  
  13. mysqli_set_charset($link, 'utf8');//面向过程
  14. $link->set_charset('utf8');//面向对象
  15.  
  16. return $link;
  17. }
  18.  
  19. $link = connect();
  20.  
  21. //print_r($link);
  22. $sql = 'select * from employees limit 10';
  23. $result = mysqli_query($link, $sql);//面向过程
  24. $result = $link->query($sql);//面向对象
  25. //print_r($result);
  26.  
  27.  
  28.  
  29. //fetch_all、fetch_array、fetch_assoc与fetch_row
  30. $result->data_seek(0);
  31. var_dump('fetch_all()方法');
  32. $data = $result->fetch_all();
  33. print_r($data);
  34.  
  35. //fetch_array获取一条数据
  36. $result->data_seek(0);
  37. var_dump('fetch_array()方法');
  38. $data = $result->fetch_array(MYSQLI_NUM);
  39. print_r($data);
  40.  
  41. //fetch_assoc获取多条数据 有字段名
  42. $result->data_seek(0);//0 为重置指针到起始
  43. var_dump('fetch_assoc()方法');
  44. $data = [];
  45. $data = $result->fetch_assoc();
  46. print_r($data);
  47.  
  48. //fetch_row获取一条数据
  49. var_dump('fetch_row()方法');
  50. $result->data_seek(0);
  51. $data = $result->fetch_row();
  52. print_r($data);
  53.  
  54. //获取字段信息 返回是stdClass对象
  55. var_dump('fetch_fields方法');
  56. $fields = $result->fetch_fields();
  57. print_r($fields);
  58.  
  59. var_dump('fetch_field方法');
  60. $result->field_seek(1);
  61. $fields = $result->fetch_field();
  62. print_r($fields);
  63.  
  64. $result->data_seek(0);
  65. //fetch_object 获取stdClass没有table等字段
  66. var_dump('fetch_object方法');
  67. $data = $result->fetch_object();
  68. print_r($data);
  69.  
  70. //fetch_field_direct
  71. var_dump('fetch_field_direct()方法');
  72. $data = $result->fetch_field_direct(1);
  73. print_r($data);
  74.  
  75. //$sql = "insert into employees(`birth_date`,`first_name`,`last_name`,`gender`,`hire_date`) values ('1988-10-9', 'pw', 'strick', 'M', '1988-10-9')";
  76. //$result = $link->query($sql);
  77.  
  78. //var_dump('affected_rows受影响行数');
  79. //var_dump($link->affected_rows);//受影响行数
  80.  
  81. //var_dump('insert_id主键');
  82. //var_dump($link->insert_id);//主键
  83. //print_r(mysqli_error($link));
  84.  
  85. //预处理
  86. //添加
  87. /* $sql = "insert into employees(`birth_date`,`first_name`,`last_name`,`gender`,`hire_date`) values (?,?,?,?,?)";
  88. $stmt = $link->prepare($sql);//获得预处理对象
  89. $birth_date = '1989-10-9';//必须得用变量赋值
  90. $first_name = 'pw2';
  91. $last_name = 'strick2';
  92. $gender = 'M';
  93. $hire_date = '1988-10-9';
  94. $stmt->bind_param('sssss', $birth_date, $first_name, $last_name, $gender, $hire_date);//绑定时使用变量绑定
  95. $stmt->execute();//执行预处理
  96.  
  97. var_dump('insert_id主键');
  98. var_dump($stmt->insert_id);
  99. var_dump('affected_rows受影响行数');
  100. var_dump($stmt->affected_rows); */
  101.  
  102. //查询
  103. $sql = 'select * from employees limit ?';
  104. $stmt = $link->prepare($sql);
  105. $count = 10;
  106. $stmt->bind_param("i", $count);
  107. $stmt->execute();
  108. $result = $stmt->get_result();//结果集取后的操作就和之前一样了
  109. print_r($result);
Runtime error #stdin #stdout #stderr 0.02s 52432KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
PHP Fatal error:  Call to undefined function mysqli_connect() in /home/Xp7RkG/prog.php on line 8