php连接MySQL数据库

# php5.5版本

<?php
$conn = mysql_connect('ip地址','用户名','密码');

if($conn) {
    mysql_select_db("codo_cmdb",$conn);
    $sql="select * from asset_server";
    $result=mysql_query($sql);
   while($row = mysql_fetch_array($result)){

    echo $row['private_ip'];

    echo "<br>";
    };
    echo 'mysql connect success';
}else {
    die("无法连接数据库".mysql_error());
}
    mysql_close($con);

?>

# PHP7版本

<?php
    $con = @mysqli_connect("192.168.10.131","root","123456");
    if($con){
        echo "数据库连接成功!</br>";
    }else{
        echo "数据库连接失败!</br>";
    }
    mysqli_select_db($con,"test");
    $sql="select count(1) from pay_orders";


    if(mysqli_query($con,$sql)){
        echo "查询成功";
    }else{
        echo "查询失败";
    }
?>

# PHP7版本

<?php
$servername = "192.168.10.133";
$username = "root";
$password = "123456";
$dbname = "test";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

$sql = "select orderid,role_name,money from pay_orders";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "orderid: " . $row["orderid"]. " - role_name: " . $row["role_name"]. " - money: " . $row["money"]. "<br>";
    }
} else {
    echo "0 结果";
}
$conn->close();
?>

  

posted @ 2022-09-19 21:53  lucky_tomato  阅读(57)  评论(0编辑  收藏  举报