Blogger Information
Blog 35
fans 0
comment 1
visits 42648
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1. 数据库连接的基本步骤;mysqli_connect() 2.数据库查询的基本步骤;mysqli_query()
魏先生的博客
Original
6223 people have browsed it
<meta charset="UTF-8">
<?php
/医院
 mysqli的数据库连接DB
 */
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','root');
define('DB_NAME','php');
define('DB_CHAR','utf8');

$db = @mysqli_connect(DB_HOST,DB_USER,DB_PASS);
if(mysqli_connect_errno($db)){
    exit('对不起,连接错误哦'.mysqli_connect_error($db));
}else{
    //echo '连接成功';
}
//选择数据库名
mysqli_select_db($db,DB_NAME);
//选择字符集
mysqli_set_charset($db,DB_CHAR);

$sql = "INSERT staff SET name='名人',age=20,salary=1000";
if(mysqli_query($db,$sql)){

if(mysqli_affected_rows($db) > 0){
    echo '新增加的id为'.mysqli_insert_id($db);
}else{
    echo "没有该数据";
}
}else { //不该显示 会暴露信息
    exit(mysqli_errno($db).':'.mysqli_error($db));
}

//4.关闭连接
mysqli_close($db);

BR1FQ{70[QF`Z~HZK0GJAK3.png

数据库连接:先设定好DB的常量,然后放到$db = mysqli_connect();在检测一下就可以检测是否连接成功

数据库增加字段:$sql = 'INSERT 表 SET 字段........';放到变量里,然后用mysqli_query()执行出来,就可以查询到数据库的


查询select:

实例

$sql = "SELECT id,name,age FROM staff";
$result = mysqli_query($db,$sql);
if(mysqli_affected_rows($db)>0){
    echo '查询到'.mysqli_affected_rows($db).'数据<br>';
}else{
    echo '没有查询到数据';
}

while($row = mysqli_fetch_array($result)){
    var_export($row);
    echo '<hr>';
}
//4.关闭连接
mysqli_close($db);

新的打印函数 var_export();感觉很高大上.

查询先返回一个结果集里,然后在按要求遍历出来就好.


图片:

WKI]7T9C8}5)_`NO~N%[Q3L.png

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post