MENU

MySQLi之数据库连接、插入、查询

MySQLi--面向对象之连接数据库

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";
$mysqli = new mysqli( $servername, $user, $password, $dbname);
if($mysqli->connect_errno){
          die('Connect error:'.$mysqli->connect_error);
}else{
echo "连接成功";
}
$mysqli->close();  

MySQLi--面向过程之连接数据库

<?php    
$servername = "localhost";     
$username = "root";   
$password = "root";   
$dbname = "test";    
$mysqli = mysqli_connect( $servername, $username, $password, $dbname);       
if(!$mysqli){      
    die('Connect error:'.mysqli_connect_error());      
}else{     
echo "连接成功";  
}      
mysqli_close($mysqli);

MySQLi--面向对象之数据库查询

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = "root"; 
$dbname = "test"; 
$mysqli = new mysqli( $servername, $user, $password, $dbname); 
if($mysqli->connect_errno){     
    die('Connect error:'.$mysqli->connect_error);     
}     
echo "连接成功";   
$sql = "INSERT INTO user (Name, Pw) VALUES ($name, $pw)";    
if(mysqli->query($sql)){    
    echo "插入成功";     
}else{    
    echo "插入失败".$mysqli->error;   
}  
</pre>
[title]MySQLi--面向过程之数据库查询[/title]
<pre> 
<?php    
$servername = "localhost";     
$username = "root";   
$password = "root";   
$dbname = "test";    
$mysqli = mysqli_connect( $servername, $username, $password, $dbname);       
if(!$mysqli){      
    die('Connect error:'.mysqli_connect_error());      
}else{     
echo "连接成功";  
}    
$sql = "INSERT INTO user (Name, Pw) VALUES ($name, $pw)";    
if (mysqli_query($mysqli, $sql)){    
    echo "插入成功";    
}else {    
    echo “插入失败”.mysqli_error($mysqli);     
}   

MySQLi--预处理之插入数据库

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = "root"; 
$dbname = "test"; 
$mysqli = new mysqli( $servername, $user, $password, $dbname); 
if($mysqli->connect_errno){     
    die('Connect error:'.$mysqli->connect_error);     
}else{     
echo "连接成功";    
}
$mysqli_stmt = $mysqli->prepare("INSERT INTO user (Name, Pw) VALUES(?, ?)");    
$mysqli->bind_param("ss",$name, $pw);     
$Name = "icharle" ;          
$pw = “icharle” ;        
$mysqli_stmt->execute();       
echo "插入成功";           
$mysqli_stmt->close();          
$mysqli->close();        

MySQLi--预处理之查询数据库

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = "root"; 
$dbname = "test"; 
$mysqli = new mysqli( $servername, $user, $password, $dbname); 
if($mysqli->connect_errno){     
    die('Connect error:'.$mysqli->connect_error);     
}else{     
echo "连接成功";    
}        
$mysqli_stmt = $mysqli->prepare("SELECT * FROM user WHERE Name=? AND Pw=?");       
$mysqli_stmt->bind_param("ss",$Name,$Pw);       
$Name = "icharle";     
$Pw = "icharle";      
$mysqli_stmt->execute();    
$mysqli_stmt->store_result();
$mysqli_stmt->bind_result($Name);
if($mysqli_stmt->fetch()){         
    echo "账号密码正确";       
}      
else{        
    echo "账号密码错误";     
}      
$mysqli_stmt->close();  
$mysqli->close();     
标签: mysql
返回文章列表 文章二维码 打赏
本页链接的二维码
打赏二维码