-
6. 查询 $sql=“SELECT id,username,password,email FROM user”; $res=mysqli_query($link,$sql); //echo mysqli_num_rows($res); if($res && mysqli_num_rows($res)>0){ while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){ //print_r($row); $rows[]=$row; } } print_r($rows); mysqli_free_result($res); //释放结果集 7. 关闭连接 mysqli_close($link);查看全部
-
4. 更新 $sql=“UPDATE user SET age=age+10 WHERE id=12;”; $sql.=“DELETE FROM user WHERE id=13”; $res=$mysqli_multi_query($link,$sql); var_dump($res); echo ‘<hr/>’; 5. 预处理语句 $sql=“INSERT user(username,password,age) VALUES(?,?,?)”; $stmt=$mysqli_prepare($link,$sql); $username=“abc”; $password=“123”; $age=“12”; mysqli_stmt_bind_param($stmt, ‘ssi’, $username,$password,$age); mysqli_stmt_execute($stmt);查看全部
-
//mysqli_func.php <?php //1.连接 $link = mysqli_connect(‘localhost’,’root’,’root’,’test’) or die(‘Connect Error:’.mysqli_connect_errno().”:”.mysqli_connect_error()); //2.编码 mysqli_set_charset($link,’UTF8’); //3.查询 $sql=“INSERT user(username,password,age) VALUES(‘a’,’a’,11);”; $res=$mysqli_query($link,$sql); if($res){ echo ‘AUTO_INCREAMENT:’.$mysqli_insert_id($link); echo ‘<hr/>’; echo ‘AFFECTED_ROWS:’.$mysqli_affected_rows($link); }else{ echo ‘ERROR:<br/>’; echo mysqli_errno($link).”:”.mysqli_error($link); } print_r($link); ?>查看全部
-
<?php header('content-type:text/html;charset=utf-8'); $mysqli = new mysqli('localhost','root','root','test'); if($mysqli->connect_errno){ die($mysqli->connect_error); } $mysqli->set_charset('utf8'); //关闭自动提交功能 $mysqli->autocommit(FALSE); $sql = "UPDATE account SET money = money-200 WHERE username = 'king'"; $res1 = $mysqli->query($sql); $res1_rows = $msyqli->affected_rows; $sql2 = "UPDATE account SET money = money+200 WHERE username = 'queen'"; $res2 = $mysqli->query($sql2); $res2_rows = $mysqli->affected_rows; if($res1 && $res1_rows>0 && $res2 && $res2_rows>0){ //提交 $msyqli->commit(); echo 'success'; $mysqli->autocommit(TRUE); } else{ //事务回滚 $mysqli->rollback(); echo 'fail'; } $mysqli->close(); ?>查看全部
-
1.面向对象。 2.面向过程。查看全部
-
trim过滤函数查看全部
-
预处理语句中的数据类型: s:字符串 i:整型 d:浮点型查看全部
-
mysqli扩展或者pdo操作数据库来代替mysql扩展 1.mysqli可以面向对象或者面向过程 2.支持预处理语句 3.支持事物 mysqli速度也更快一些。查看全部
-
<?php header('content-type:text/html;charset=utf-8'); $mysqli = new mysqli('localhost','root','root','test'); if($mysqli->connect_errno){ die($mysqli->connect_error); } $mysqli->set_charset('utf8'); //关闭自动提交功能 $mysqli->autocommit(FALSE); $sql = "UPDATE account SET money = money-200 WHERE username = 'king'"; $res1 = $mysqli->query($sql); $res1_rows = $msyqli->affected_rows; $sql2 = "UPDATE account SET money = money+200 WHERE username = 'queen'"; $res2 = $mysqli->query($sql2); $res2_rows = $mysqli->affected_rows; if($res1 && $res1_rows>0 && $res2 && $res2_rows>0){ //提交 $msyqli->commit(); echo 'success'; $mysqli->autocommit(TRUE); } else{ //事务回滚 $mysqli->rollback(); echo 'fail'; } $mysqli->close(); ?>查看全部
-
<?php header('content-type:text/html;charset=utf-8'); $mysqli = new mysqli('localhost','root','root','test'); if($mysqli->connect_errno){ die($mysqli->connect_error); } $mysqli->set_charset('utf8'); $sql = "select userid,username,userjob from user where userid >=?"; $mysqli_stmt = $mysqli->prepare($sql); $id = 20; $mysqli_stmt->bind_param('i',$id); if($mysqli_stmt->excute() ){ //bind_result()绑定结果集中的值到变量 $mysqli_stmt->bind_result($id, $name, $job); while($msyqli_stmt->fetch() ){ //通过fetch()遍历结果集 echo 'ID'.$id.'<br/>'; echo '昵称'.$name.'<br/>'; echo '身份'.$job.'<br/>'; echo '<hr/>'; } } $mysqli_stmt->free_result(); $mysqli_stmt->close(); $mysqli->close(); ?>查看全部
-
<?php header('Content-type:text/html;charset=utf-8'); $mysqli = @new mysqli('localhost','root','root','test'); if($mysqli->connect_errno){ echo 'Connect Error:'.$mysqli->connect->error; } $mysqli->set_charset('utf8'); $username = $_POST['username']; $password = md5( $_POST[''password] ); //$sql = "select * from user where username = '$username' and //password = '$password' "; //$mysqli_result = $mysqli->query($sql); //if($mysqli_result && $mysqli_result->num_rows > 0){ // echo '登陆成功!'; //} //else{ // echo '登录失败!'; //} //以上查询方式容易被SQL注入 $sql = "select * from user where username = ? and password = ?"; $mysqli_stmt = $mysqli->prepare($sql); $mysqli_stmt->bind_param('ss', $username, $password); if($mysqli_stmt->excute() ){ $mysqli_stmt->store_result(); if($mysqli_stmt->num_rows > 0){ echo '登陆成功'; } else{ echo '登录失败'; } } $mysqli_stmt->free_result();//释放结果集 $mysqli_stmt->close();//关闭预处理语句 $mysqli->close();查看全部
-
escape_string转义查看全部
-
$sql = "SELECT id,username,age FROM user"; $res = $mysqli->query($sql); //返回值在$mysqli_result中 var_dump($res) if($res && $res->num_rows_0){ echo $mysqli_result->num_rows; $rows = $mysqli_result->fetch_all();//获取结果集中的所有记录。 $rows = $mysqli_result->fetch_all(mysql_num);//数字索引 $rows = $mysqli_result->fetch_all(MYSQLI_ASSOC);//关联 $rows = $mysqli_result->fetch_all(MYSQLI_BOTH);//都有 $rows = $mysqli_result->fetch_row()//返回结果集中的第一个记录 $rows = $mysqli_result->fetch_assoc();//索引 $rows = $mysqli_result->fetch_array();//二者都有 $rows = $mysqli_result->fetch_object()//对象形式返回 $mysqli_result->data_seek(0);调正结果集中的指针到首部 print_r ($rows) }else{ echo '没有条数' } 循环显示 while($row = $mysqli_result->fetch_assoc()){ print_r($row); $rows[] = $row; } 释放结果集 $mysqli_result->free(); 关闭连接 $mysqli->close();查看全部
-
insert_id:得到自增长的值 affected_rows值为三种: 1.受影响记录条数 2.-1,代表sql语句有问题 3.0,代表没有受影响记录的条数查看全部
-
$sql = "UPDATE user SET age=age+10 WHERE NAME = 'LISI'"; $mysqli->query($sql);查看全部
举报
0/150
提交
取消