php 操作(新浪云)mysql数据库

连接数据库:$conn = new mysqli(SAE_MYSQL_HOST_M, SAE_MYSQL_USER, SAE_MYSQL_PASS , SAE_MYSQL_DB);



//读取数据
$sql = "SELECT mz, age, gender FROM first";
$result = $conn->query($sql);


if ($result->num_rows > 0) {
    // 输出每行数据
    while($row = $result->fetch_assoc()) {
        echo "
Mz: ". $row["mz"]. " - Age: ". $row["age"]. " " . $row["gender"];
    }
} else {
    echo "0 个结果";
}
              15:28:48
//插入数据


$aname=$_GET["name"];


//echo $name;
$age=$_GET["age"];
//echo $age;
$gender=$_GET["gender"];
//echo $gender;
$q=$_GET["q"];
// 检测连接
if ($conn->connect_error) {
    echo("连接失败: " . $conn->connect_error);





if($q === "ins"){




$sql = "INSERT INTO first (mz, age, gender)
VALUES ('".$aname."',$age,'".$gender."')";


if ($conn->query($sql) === TRUE) {
    echo "新记录插入成功";
  } else {
    echo "Error: " . $sql . "
" . $conn->error;
 }




    
}
              15:36:58
//查询


$result = mysqli_query($conn,"SELECT * FROM first
WHERE mz='lisi'");


while($row = mysqli_fetch_array($result))
{
echo $row['mz'] . " " . $row['age'];
echo "
";
}
              15:48:40
//修改
mysqli_query($conn,"UPDATE first SET age=36
WHERE mz='zhangsan' AND gender='nan'");
              15:58:28
//删除
mysqli_query($conn,"DELETE FROM first WHERE mz='zhangsan'");

你可能感兴趣的:(php)