当前位置:首页>>问题

php7.0以上版本mysql数据库连接方法demo代码

Fatal error: Uncaught Error: Call to undefined function mysql_connect()如果出现上面的问题,可能是由于php版本问题导致的,mysql_connect()在php5.x版本可以实用,在php7.x版本中需要使用以下的代码:<?phpheader("Content-type: text/html; charset=utf-8");$con = mysqli_connect("127.0.0.1"

admin

Fatal error: Uncaught Error: Call to undefined function mysql_connect()

如果出现上面的问题,可能是由于php版本问题导致的,mysql_connect()在php5.x版本可以实用,在php7.x版本中需要使用以下的代码:

<?php
header("Content-type: text/html; charset=utf-8");
$con = mysqli_connect("127.0.0.1","用户名","密码");

 

if(!$con)
{
    die('Could not connect: ' . mysqli_connect_error());
}else{
echo '数据库连接成功!';
}

 

mysqli_select_db($con,"数据库");

 

$sitename = '';
$siteurl = '';

 

function replaceSpecialChar($strParam){
    $regex = "//|~|!|@|#|$|%|^|&|*|[|]|_|+|{|}|:|<|>|?|[|]|/|;|'|`|-|=|||/";
    return preg_replace($regex,"",$strParam);
}
?>


第二种,更简单的写法

$connection = mysqli_connect('localhost', '用户名', '密码', '数据库名');


$xml_data = file_get_contents('php://input');    //微信jsapi支付接收回调的xml数据

file_put_contents('支付返回数据.txt',$xml_data);

// 将 XML 字符串转换为 SimpleXML 对象

$xml = simplexml_load_string($xml_data);

if ($xml === false) {

    die('XML 解析失败');

}

// 提取字段值(微信支付回调的典型字段)

$return_code = (string)$xml->return_code;      // 返回状态码(SUCCESS/FAIL)

//$return_msg = (string)$xml->return_msg;        // 返回信息

//$result_code = (string)$xml->result_code;      // 业务结果(SUCCESS/FAIL)

$out_trade_no = (string)$xml->out_trade_no;    // 商户订单号

//echo $out_trade_no.'<br />';

//$transaction_id = (string)$xml->transaction_id; // 微信支付订单号

$total_fee = (int)$xml->total_fee;             // 订单金额(单位:分)



$sql = "select * from xxx where xxx='".$out_trade_no."'";

$result = mysqli_query($connection, $sql);

$rowcount=mysqli_num_rows($result);    //判断有无记录

if($rowcount > 0){

    mysqli_query($connection, "update xxx set ispay = 1 where xxx='".$out_trade_no."'");

}

$row = mysqli_fetch_array($result,MYSQLI_ASSOC);

mysqli_query($connection, "update xxx set xxx = xxx+".$row['xxx']." where id='".$row['tid']."'");

//微信的jsapi支付,接收回调后需要向对方服务器返回SUCCESS

echo '<xml>

        <return_code><![CDATA[SUCCESS]]></return_code>

        <return_msg><![CDATA[OK]]></return_msg>

      </xml>';


第三种连接方式:

try{

  $db = new PDO("mysql:host=localhost;dbname=数据库名","用户名","密码");

  //print_r($db);

}catch(PDOException $e){

  die("数据库连接失败".$e->getMessage());

}



返回顶部