【问题标题】:codeignte route not workng afer upgrading codeignite 3.1.11 and php 7.1升级 codeigniter 3.1.11 和 php 7.1 后,codeigniter 路由不起作用
【发布时间】:2021-10-03 06:02:45
【问题描述】:

我有一个 codeignte 项目,我是旧版本,我在 codeigite 3.1.11 和 php 7.1 中升级了我的项目,一切正常,但我的自定义路线不起作用。但我的旧项目这条路线工作正常。请检查我的代码并给我一些解决方案。我正在尝试所以可能会改变我的 htacess 和 rout。我使用配置 uri_protocol 是 REQUEST_URI
这是我的 roue.php

    $url    =   $_SERVER['REQUEST_URI'];
$url    =   explode('/',$url);

$url=end($url);

$secondlasturl  = explode('/',$_SERVER['REQUEST_URI']);

$controller     =   $secondlasturl[1];
$second_url =   $secondlasturl[2];
if(isset($secondlasturl[3])){
    $third_url  =   $secondlasturl[3];
}else{
    $third_url='';
}


//$min=explode('-',$second_url1);

$con    = mysqli_connect('localhost','root','','mydb');

if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    exit();
}

if($url!=''){

    $sql    =   'select CompanyID, shorturl, BusinessType from lxn_companies where shorturl ="'.$url.'"';

    $res=mysqli_query($con, $sql);


    if(mysqli_fetch_assoc($res))
    {

        $row    =  mysqli_fetch_row($res);
        //print_r($row);
        $type   =   $row[2];
        if($type=='Exporters')
        {
            $userlink   =   'ExporterDetail/'.$row[0];

        }
        else
        {
            $userlink   =   'ImporterDetail/'.$row[0];
        }

        $route['(:any)'] = "user/".$userlink."";
    }
    else
    {


        $sql    =   'select CompanyID, sef_url, BusinessType from lxn_companies where sef_url ="'.$url.'"';
        $res=mysqli_query($con, $sql);

        if(mysqli_num_rows($res)>0)
        {
            $row    =  mysqli_fetch_row($res);
            //print_r($row);
            $type   =   $row[2];
            if($type=='Exporters')
            {
                $userlink   =   'ExporterDetail/'.$row[0];

            }
            else
            {
                $userlink   =   'ImporterDetail/'.$row[0];
            }

            $route['(.*)'] = "user/".$userlink."";
        }

    }
}

我正在为我的项目使用这个 htacess 代码。

   <IfModule mod_rewrite.c>
        Options +FollowSymLinks
        RewriteEngine on
    
        RewriteRule ^([a-z0-9_-]+)\.html$ index.php/page/$1 [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond $1 !^(index\.php|asset|robots\.txt)
        RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
    </IfModule>
    
    # php -- BEGIN cPanel-generated handler, do not edit
    # Set the “ea-php71” package as the default “PHP” programming language.
    <IfModule mime_module>
      AddHandler application/x-httpd-ea-php71 .php .php7 .phtml
    </IfModule>

【问题讨论】:

  • 您的“工作版本”是否也在 PHP 7.1 上运行? mysql_xxxx 自 PHP 7 以来已被删除,这可能是它不起作用的原因。您还应该检查服务器上的错误日志。
  • @TimBrownlaw 你能解释一下查询是有效的吗?我检查了查询结果
  • @TimBrownlaw 是的,我升级了 mysql 查询但不工作
  • @MinhazurRahaman - 您需要检查您的 IF 语句。在最后一个中,您使用 mysqlI_num_rows 正确执行此操作。在第一个中,您实际上阅读了该行。所以你试图做同样的事情,两种不同的方式。为什么不使用内置的 CI 函数,如 $this->db->query($sql),你一开始就不会遇到这个问题。

标签: php .htaccess codeigniter codeigniter-3 php-7


【解决方案1】:

根据您现有的代码,我想出了这个供您尝试。

是的,它使用的是 mysqli 而不是 PDO(还)所以其他人,请不要为此火化我。

设置:(如果需要,您需要在以后的问题中提供此类信息。)

    DROP TABLE IF EXISTS lxn_companies;
    CREATE TABLE lxn_companies (
      CompanyID INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
      BusinessType VARCHAR(48),
      shorturl VARCHAR(128),
      sef_url VARCHAR(128)
    );

INSERT INTO lxn_companies
(CompanyID, BusinessType, shorturl, sef_url) values
(1,'Exporters','CompanyID1_short_url',''),
(2,'Importers','CompanyID2_short_url',''),
(3,'Exporters','','CompanyID3_sef_url'),
(4,'Importers','','CompanyID4_sef_url');

“roue.php”文件

在您看到代码重复的地方(就像您的代码一样),它请求重构。

我现在没有时间在这里解释所有内容,但请仔细阅读。

要点 - 在读取数据库行列(字段)时使用 mysqli_fetch_assoc(关联),因此使用名称(更易读)而不是数字索引(难以理解)。

$url_array = explode('/', $_SERVER['REQUEST_URI']);
$url = end($url_array);
// The Following 3 variables are not used in this code.
//$controller = isset($url_array[1]) ? $url_array[1] : '';
//$second_url = isset($url_array[2]) ? $url_array[2] : '';
//$third_url = isset($url_array[3]) ? $url_array[3] : '';

if ($url != '') {
    $con = mysqli_connect('localhost', 'root', '', 'mydb');
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        exit();
    }
    // Look for entries with either shorturl or sef_url.
    // A Row cannot have both shorturl and sef_url
    $sql = "SELECT 
                CompanyID,
                BusinessType
             FROM lxn_companies 
             WHERE shorturl = '{$url}'
             OR sef_url = '{$url}'
            ";
    $res = mysqli_query($con, $sql);
    if ($res && mysqli_num_rows($res)) {
        $row = mysqli_fetch_assoc($res);
        if ($row['BusinessType'] == 'Exporters') {
            $userlink = 'ExporterDetail/';
        } else {
            $userlink = 'ImporterDetail/';
        }
        $route['(.*)'] = "user/" . $userlink . $row['CompanyID'];
    }
}

// Debug Stuff
echo '<pre>';
echo 'LINE: ' . __LINE__ . ' Module ' . __CLASS__ . '<br>';
var_dump($route);
echo '</pre>';
exit('Stop here to inspect the final routes');

我在 application/config/routes.php 中“包含”了这个文件。

需要解决的问题: 用 PDO 替换 mysqli

【讨论】:

    猜你喜欢
    • 2016-04-22
    • 2020-10-28
    • 1970-01-01
    • 2011-11-07
    • 2018-06-27
    • 1970-01-01
    相关资源
    最近更新 更多