Blogger Information
Blog 36
fans 0
comment 1
visits 27877
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
mysqli_connect()与mysqli_query()
其琛的博客
Original
796 people have browsed it

mysqli_connect()代码

<?php
//define() 函数定义一个常量。
//
//常量类似变量,不同之处在于:
//
//在设定以后,常量的值无法更改
//常量名不需要开头的美元符号 ($)
//作用域不影响对常量的访问
//常量值只能是字符串或数字
define('db_host','localhost');
define('db_user','root');
define('db_pas','root');
define('db_name','php');
define('db_char','utf8');
//设置编码字符集
//数据库连接
//函数mysql_connect(server,user,pwd,newlink,clientflag)
$db = mysqli_connect(db_host,db_user,db_pas);
//如果成功,则返回一个 MySQL 连接标识,失败则返回 FALSE。
//测试方法1
//if ($db != false){
//    echo '<h1>连接成功</h1>';
//}else {
//    echo '<h1>连接失败</h1>';
//}
//测试方法2
//mysqli_connect_errno() 函数返回上一次连接错误的错误代码
//mysqli_connect_error() 函数返回上一次连接错误的错误描述。
if (mysqli_connect_errno($db)){
    exit('连接失败2'.mysqli_connect_error($db));
}else{
    echo '<h1>连接成功2</h1>';
}


//选择要操作的数据库(可省略步骤)
mysqli_select_db($db,db_name);

//设置默认字符集
mysqli_set_charset($db,db_char);

mysqli_query()代码

<?php
//连接数据库
require 'demo1.php';

//创建数据库语句
$sql = "INSERT class SET name='孔夫子',sex=0,age=23,grade=90";
//实用数据库语句
//mysqli_query() 函数执行某个针对数据库的查询。
$play = mysqli_query($db,$sql);
//测试是否执行成功
//mysqli_affected_rows() 函数返回前一次 MySQL 操作(SELECT、INSERT、UPDATE、REPLACE、DELETE)所影响的记录行数。
//mysqli_insert_id() 函数返回最后一个查询中自动生成的 ID(通过 AUTO_INCREMENT 生成)
if (mysqli_affected_rows($db) >0){
    echo '成功执行了'.mysqli_affected_rows($db).'条记录,新纪录id是'.mysqli_insert_id($db);

}else{
    echo '执行失败';

}
//关闭数据库连接
mysqli_close($db);


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!