-
/检测mysqli是否开启,其他扩展也类似 //phpinfo(); var_dump(extension_loaded("mysqli")); echo "<hr/>"; var_dump(function_exists("mysqli_connect")); echo "<hr/>"; print_r( get_loaded_extensions()); echo "<hr/>";查看全部
-
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); ?>查看全部
-
//输出到html,显示评论内容 public function output(){ if($this->data[‘url’]){ $link_start=“<a href=‘”.$this->data[‘url’].”’ target=‘_blank’>”; $link_end=“</a>”; } $dateStr=date(“Y年m月d日 H:i:s”,$this->data[‘pubTime’]); $res=<<<EOF <div class=‘comment’> <div class=‘face’> {$link_start} <img width=’50’ height=’50’ src=“img/{$this->data[‘face’]}.jpg”> {$link_end} </div> <div class=‘username’></div> <div class=‘date’>{$dateStr}</div> <p>{$this->data[‘content’]}</p> </div> }查看全部
-
//comment.class.php <?php class Comment{ private $data=array(); function __construct($data){ $this->data=$data; } public static function validate(&$arr){ if(!($data[‘email’]=filter_input(INPUT_POST,’email’,FILTER_VALIDATE_EMAIL))){ $errors[‘email’]=‘请输入合法邮箱’; } if(!($data[‘url’]=filter_input(INPUT_POST,’url’,FILTER_VALIDATE_URL))){ $url=‘’; } if(!($data[‘content’]=filter_input(INPUT_POST,’content’,FILTER_CALLBACK,array(‘options’=>’Comment::validate_str’)))){ $errors[‘content’]=‘请输入核实内容’; } //username,face,FILTER_VALIDATE_INT, $options = array('option'=>array('min_range'=>1,'max_range'=>5)); $arr = $data; $arr[‘email’] = strtolower(trim($arr[‘email’])); } //过滤用户输入的特殊字符 public static function validate_str($str){ if(mb_strlen($str,’UTF8’)<1){ return false; } $str=nl2br(htmlspecialchars($str,ENT_QUOTES)); //nl2br,前后加入回车换行 return $str; } } ?>查看全部
-
16. MySQLi评论系统 //doAction.php <?php //header require_once ‘connect.php’; require_once ‘comment.class.php’; $arr = array(); $res = Comment::validate($arr); if($res){ $sql=“INSERT comments(username,email,url,face,content,pubTime) VALUES(?,?,?,?,?,?)”; $mysqli_stmt =$mysqli->prepare($sql); $arr[‘pubTime’]=time(); $mysqli_stmt->bind_param(‘sssssi’,$arr[‘username’],$arr[‘email’],$arr[‘url’],$arr[‘face’],$arr[‘content’],$arr[‘pubTime’]); $mysqli_stmt->execute(); $comment=new Comment($arr); echo json_encode(array(’status’=>1,’html’=>$comment->output())); }else{ echo ‘{“status”:0,”errors”:’.json_encode($arr).’}’; } ?> //connect.php <?php $mysqli=new mysqli(‘localhost’,’root’,’root’,’test’); if($mysqli->errno){ die(‘Connect Error:’.$mysqli->error); }else{ $mysqli->set_charset(‘UTF8’); } ?>查看全部
-
mysqli的优势查看全部
-
bind_result()中绑定的值的数量多少等要和 SQL语句中"SELECT u_id,u_username,u_age FROM user WHERE u_id>=?;"返回的列数对应; ----------------------------------------------------------------------------------------- "SELECT u_id FROM user WHERE u_id>=?;" >>>> bind_result($id); ----------------------------------------------------------------------------------------------- "SELECT * FROM user WHERE u_id>=?;" //所有列 >>>> bind_result($id, $name, $password, $age); //事实上$password不用取,只是例子演示需要。 --------------------------------------------------------------------------------------------------查看全部
-
ssi 表示 第1个插入数据库列的参数为字符,第2个为字符,第3个为integer整数数值 Type specification chars Character Description i corresponding variable has type integer d corresponding variable has type double s corresponding variable has type string b corresponding variable is a blob and will be sent in packets 如果是这样的 $stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)"); $stmt->bind_param('sssd', $code, $language, $official, $percent); 表达的是: 第1个到第3个都是字符,第4个是double小数查看全部
-
检测mysqli扩展是否开启查看全部
-
检测mysqli扩展是否开启查看全部
-
$sql = "INSERT...;" $SQL.= "UPDATE...;" $sql .="DELETE...;" $mysqli->multi_query($sql); 必须第一条成功,后面两条才能执行 $sql = "SELECT ID,username,age FROM user;"; $sql .= "SELECT * FROM mysql.user;" //use_resuly()/store_result()第一个结果集 //通过more_result检查是否有更多的结果集 //next_result()将结果集指针向下移动一位。 $mysqli->multi_query($sql) if($mysqli->multi_query($sql)){ do{ if($mysqli_result=$mysqli->storage_result()){ $rows[]=$muysqli_result->fetch_all(MYSQLI_ASSOC); } WHILE($mysqli->more_results()&&mysqli->next_results()); } }查看全部
-
$sql = "INSERT...;" $SQL.= "UPDATE...;" $sql .="DELETE...;" $mysqli->multi_query($sql); 必须第一条成功,后面两条才能执行 $sql = "SELECT ID,username,age FROM user;"; $sql .= "SELECT * FROM mysql.user;" //use_resuly()/store_result()第一个结果集 //通过more_result检查是否有更多的结果集 //next_result()将结果集指针向下移动一位。 $mysqli->multi_query($sql) if($mysqli->multi_query($sql)){ do{ if($mysqli_result=$mysqli->storage_result()){ $rows[]=$muysqli_result->fetch_all(MYSQLI_ASSOC); } WHILE($mysqli->more_results()&&mysqli->next_results()); } }查看全部
-
$mysqli = new mysqli('localhost','root','test') if($mysqli->connect_errno){ die("connect error"$mysqli->connect_error); } $sql = "select ID,username,age from user" $mysqli_result=$mysqli->auery($sql); if($mysqli_result && $mysqli_tesult->num_rows>0){ while($row = $mysqli_result->fetch_assoc()){ $row[]=$row; } }查看全部
举报
0/150
提交
取消