首页 > 代码库 > php使用mysql和mysqli连接查询数据

php使用mysql和mysqli连接查询数据

<?php 
$code = $_POST[‘code‘];
$status = "";
$success = "";
$scookies = "";

try {
    $dbname="root";
    $dbpass="root";
    $dbhost="127.0.0.1";
    $dbdatabase="mysql";
 
    //生成一个连接
    $db_connect=mysql_connect($dbhost,$dbname,$dbpass);

    //选择一个需要操作的数据库
    mysql_select_db($dbdatabase,$db_connect);

    // 获取查询结果
    $strsql="select * from `go_member_addmoney_record` where `code` = ‘$code‘ limit 1";
    $result=mysql_query($strsql);

    // 循环取出记录
    while ($row=mysql_fetch_array($result))
    {
        $status = $row[‘status‘];
        $scookies = $row[‘scookies‘];
    }

    // 释放资源
    mysql_free_result($result);
    // 关闭连接
    mysql_close($db_connect);  

    if($status=="已付款"){
        $success = "success";
    }
    else{
        $success = "faild";
    } 
    echo $success.";".$scookies;
}
catch (Exception $e){}
?>

mysqli:

<?php 
$code = $_POST[‘code‘];
$status = "";
$success = "";
$scookies = "";

try {
    $dbname="root";
    $dbpass="root";
    $dbhost="127.0.0.1";
    $dbdatabase="mysql";
 
    //生成一个连接
    $db_connect= new mysqli($dbhost,$dbname,$dbpass,$dbdatabase);

    // 获取查询结果
    $strsql="select * from `go_member_addmoney_record` where `code` = ‘$code‘ limit 1";
    $result=$db_connect->query($strsql);

    // 循环取出记录
    while ($row=mysqli_fetch_assoc($result))
    {
     $status = $row[‘status‘];
     $scookies = $row[‘scookies‘];
    }
    // 释放资源
    $result->close();
    // 关闭连接
    $db_connect->close();

    if($status=="已付款"){
        $success = "success";
    }
    else{
        $success = "faild";
    } 
    echo $success.";".$scookies;
}
catch (Exception $e){}

php使用mysql和mysqli连接查询数据