php 项目放服务器显示mysql_connect 已经废弃 ?

发布于 2022-09-07 22:34:50 字数 6306 浏览 40 评论 0

小弟前端菜鸟一枚在做一个小项目时

<?php
header("Content-type: text/html;charset=utf-8");//防止乱码
//1、获取链接
$conn=@mysql_connect("localhost","root","0123456zp");
if(!$conn){
    //mysql_error()是返回mysql的上一次操作
    die("连接失败".mysql_error());
}else{
    //echo '连接成功!<br/>';
}
//2、选择对应的数据库
@mysql_select_db("caiwu");
//3、设置操作编码(可有可无,建议有)
mysql_query("set names utf8");//保证是按照utf8码操作的【utf8无-的,有-的是网页版的!!!!!!!】

$draw = $_GET['draw']; //第几次请求
//排序
$order_column = $_GET['order']['0']['column']; // 哪一列排序
$order_dir= $_GET['order']['0']['dir']; // ase desc 升序或者降序
//拼接排序sql
$orderSql = "";
if(isset($order_column)){
    $i = intval($order_column);
    switch($i){
        case 0;$orderSql = " order by id ".$order_dir;break;
        case 1;$orderSql = " order by kh ".$order_dir;break;
        case 2;$orderSql = " order by kj ".$order_dir;break;
        case 3;$orderSql = " order by cpx ".$order_dir;break;
        case 4;$orderSql = " order by zhd ".$order_dir;break;
        case 5;$orderSql = " order by afzhd ".$order_dir;break;
        case 6;$orderSql = " order by afxjd ".$order_dir;break;
        case 7;$orderSql = " order by zxrq ".$order_dir;break;
        case 8;$orderSql = " order by xs ".$order_dir;break;
        case 9;$orderSql = " order by crrq ".$order_dir;break;
        case 10;$orderSql = " order by skr ".$order_dir;break;
        case 11;$orderSql = " order by skzh ".$order_dir;break;
        default;$orderSql = '';
    }
}

//分页
$start = $_GET['start'];//从多少开始
$length = $_GET['length'];//数据长度
$limitSql = '';
$limitFlag = isset($_GET['start']) && $length != -1 ;
if ($limitFlag ) {
    $limitSql = " LIMIT ".intval($start).", ".intval($length);
}

//定义查询数据总记录数sql
$sumSql = "SELECT count(id) as sum FROM financialdata";
//条件过滤后记录数 必要
$recordsFiltered = 0;
//表的总记录数 必要
$recordsTotal = 0;
$recordsTotalResult = mysql_query($sumSql,$conn);
while($row=mysql_fetch_assoc($recordsTotalResult)){
    $recordsTotal = $row['sum'];
}
//搜索//定义过滤条件查询过滤后的记录数sql
$search = $_GET['search']['value'];//获取前台传过来的过滤条件
// 每列搜索
$j = 1;
$search_arr =array();
$colSearchSql='';
for($j=0;$j<6;$j++){
    $colSearchable = $_GET['columns'][$j]['searchable'];
    $colSearchVal = $_GET['columns'][$j]['search']['value'];
    if(strlen($colSearchVal)>0 && $colSearchable == 'true' ){
        switch ($j) {
            case 0: $colSearchVal = "id LIKE '%".$colSearchVal."%'";
                array_push($search_arr,$colSearchVal);
                break;
            case 1: $colSearchVal = "kh LIKE '%".$colSearchVal."%'";
                array_push($search_arr,$colSearchVal);
                break;
            case 2: $colSearchVal = "kj LIKE '%".$colSearchVal."%'";
                array_push($search_arr,$colSearchVal);
                break;
            case 3: $colSearchVal = "cpx LIKE '%".$colSearchVal."%'";
                array_push($search_arr,$colSearchVal);
                break;
            case 4: $colSearchVal = "zhd LIKE '%".$colSearchVal."%'";
                array_push($search_arr,$colSearchVal);
                break;
            case 5: $colSearchVal = "afzhd LIKE '%".$colSearchVal."%'";
                array_push($search_arr,$colSearchVal);
                break;
            case 6: $colSearchVal = "afxjd LIKE '%".$colSearchVal."%'";
                array_push($search_arr,$colSearchVal);
                break;
            case 7: $colSearchVal = "zxrq LIKE '%".$colSearchVal."%'";
                array_push($search_arr,$colSearchVal);
                break;
            case 8: $colSearchVal = "xs LIKE '%".$colSearchVal."%'";
                array_push($search_arr,$colSearchVal);
                break;
            case 9: $colSearchVal = "crrq LIKE '%".$colSearchVal."%'";
                array_push($search_arr,$colSearchVal);
                break;
            case 10: $colSearchVal = "skr LIKE '%".$colSearchVal."%'";
                array_push($search_arr,$colSearchVal);
                break;
            case 11: $colSearchVal = "skzh LIKE '%".$colSearchVal."%'";
                array_push($search_arr,$colSearchVal);
                break;
            default:
                # code...
                break;
        }
    }
}
if(empty($search_arr) == false){
    $colSearchSql = " WHERE ".implode(" AND ",$search_arr);
}
// 综合搜索sql
// $zSearchSql =" where teacher_id || teacher_name || teacher_password || teacher_email || teacher_date || teacher_note LIKE '%".$search."%'";
$zSearchSql = " id LIKE '%".$search."%' OR kh LIKE '%".$search."%' OR kj LIKE '%".$search."%' OR cpx LIKE '%".$search."%' OR zhd LIKE '%".$search."%' OR afzhd LIKE '%".$search."%'OR afxjd LIKE '%".$search."%'OR zxrq LIKE '%".$search."%'OR xs LIKE '%".$search."%'OR crrq LIKE '%".$search."%'OR skr LIKE '%".$search."%'OR skzh LIKE '%".$search."%'";
// 拼接搜索SQL
$sumSearchSql = '';
if(strlen($colSearchSql)>0 && strlen($search)>0){
    $sumSearchSql = $colSearchSql." AND (".$zSearchSql.")";
}else if(strlen($colSearchSql)>0 && strlen($search)==0){
    $sumSearchSql = $colSearchSql;
}else if(strlen($colSearchSql)==0 && strlen($search)>0){
    $sumSearchSql = " WHERE ".$zSearchSql;
}else{
    $sumSearchSql = '';
}
if(strlen($sumSearchSql)>0){
    $recordsFilteredResult = mysql_query($sumSql.$sumSearchSql);
    while ($row = mysql_fetch_assoc($recordsFilteredResult)) {
        $recordsFiltered =  $row['sum'];
    }
}else{
    $recordsFiltered = $recordsTotal;
}
// query data
$sql='';
$totalResultSql = "SELECT * FROM financialdata";
$infos = array();
// 拼接最终SQL
$sql=$totalResultSql.$sumSearchSql.$orderSql.$limitSql;
$dataResult = mysql_query($sql,$conn);
while ($row = mysql_fetch_assoc($dataResult)) {
    // $obj = array($row['teacher_id'], $row['teacher_name'], $row['teacher_password'], $row['teacher_email'], $row['teacher_date'], $row['teacher_note']);
    // array_push($infos,$obj);
    array_push($infos,$row);
}
// return data
echo json_encode(array(
    "draw" => $draw,
    "recordsTotal" =>$recordsTotal,  // necessary
    "recordsFiltered" =>$recordsFiltered, // necessary
    "data" =>$infos // necessary
),JSON_UNESCAPED_UNICODE);

本地服务器环境可以正常访问 但是一放到网络服务器就报错 mysql_connect
,从网上查了下说是mysql_connect 这个已经废弃,但是我自己改了还是报错 求前辈帮忙指点下 或者给改下 谢谢 以上为php代码

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风吹过旳痕迹 2022-09-14 22:34:50

服务器上安装一下mysqli扩展,然后把所有的 mysql_* 函数都替换成 mysqli_ 吧。

http://php.net/manual/zh/mysqli.installation.php

椒妓 2022-09-14 22:34:50

线上PHP版本过高

PHP5.5开始就会提示为待删除的方法,7.0开始就已经完全移除了。请跟上PHP的发布进度

情未る 2022-09-14 22:34:50

不是线上php版本过高,是你本地php版本太低
不要用mysql_connect或者mysqli,用pdo,或者直接去用tp3框架吧

感情旳空白 2022-09-14 22:34:50
  1. 如果需要用mysql_connect函数,建议PHP版本5.3
  2. 建议使用PDO来操作数据库,全版本支持
把回忆走一遍 2022-09-14 22:34:50

多看文档

clipboard.png

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文