如何在 Codeigniter 中使用 $_GET

发布于 2024-12-13 14:22:43 字数 1555 浏览 0 评论 0原文

这是我网站的网址,该网址用于从数据库调用帖子,其 id=1

http://*.com/?view=entry&id=1

我在具有此代码的模型

<?php
class inner_query extends CI_Model{
    // main shakli wow in slider 
    function get_current_entry(){
        $q= $this->db->query("SELECT * FROM hs_categories_cat INNER JOIN hs_news_nw ON id_cat=idcat_nw WHERE active_nw='1' AND id_nw={$id}");
        if($q->num_rows() == 1){
            foreach($q->result() as $row){
                $data[]=$row;//new array to store every results returned from database 
            }
            return $data;//return with array results
        }
    }
}

这是我的控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class entry extends CI_Controller {
    public function index()
    {
        $id=$_GET['id'];
        //load model of the current entry
        $this->load->model('inner_query');
        $data['get_current_entry'] = $this->inner_query->get_current_entry();

        $this->load->view('inner_page.php', $data);
    }

}

检查此页面后我收到此错误消息

A Database Error Occurred
Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

SELECT * FROM hs_categories_cat INNER JOIN hs_news_nw ON id_cat=idcat_nw WHERE active_nw='1' AND id_nw=

Filename: E:***\system\database\DB_driver.php

Line Number: 330

here is a url of my website, this url is used to call the post from database which its id=1

http://*.com/?view=entry&id=1

i try in the model with this code

<?php
class inner_query extends CI_Model{
    // main shakli wow in slider 
    function get_current_entry(){
        $q= $this->db->query("SELECT * FROM hs_categories_cat INNER JOIN hs_news_nw ON id_cat=idcat_nw WHERE active_nw='1' AND id_nw={$id}");
        if($q->num_rows() == 1){
            foreach($q->result() as $row){
                $data[]=$row;//new array to store every results returned from database 
            }
            return $data;//return with array results
        }
    }
}

and here is my controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class entry extends CI_Controller {
    public function index()
    {
        $id=$_GET['id'];
        //load model of the current entry
        $this->load->model('inner_query');
        $data['get_current_entry'] = $this->inner_query->get_current_entry();

        $this->load->view('inner_page.php', $data);
    }

}

once check this page i get this error message

A Database Error Occurred
Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

SELECT * FROM hs_categories_cat INNER JOIN hs_news_nw ON id_cat=idcat_nw WHERE active_nw='1' AND id_nw=

Filename: E:***\system\database\DB_driver.php

Line Number: 330

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

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

发布评论

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

评论(5

从此见与不见 2024-12-20 14:22:43

您没有将 $id 传递给 get_current_entry() 函数。

在控制器中,将 $id 传递给模型,如下所示:

$data['get_current_entry'] = $this->inner_query->get_current_entry($id);

修改模型的 get_current_entry() 函数以接受 $id 作为参数:

function get_current_entry($id){
    // other code here...
}

You're not passing $id to your get_current_entry() function.

In your controller, pass $id to your model like so:

$data['get_current_entry'] = $this->inner_query->get_current_entry($id);

Modify your model's get_current_entry() function to accept $id as a parameter:

function get_current_entry($id){
    // other code here...
}
呆萌少年 2024-12-20 14:22:43

您没有将 $id 传递给控制器​​中的 get_current_entry() 函数。它应该如何获得该值?

You are not passing the $id to get_current_entry() function in your controller. How is it supposed to get that value?

你げ笑在眉眼 2024-12-20 14:22:43

您尚未将 $id 传递到函数中。您可以在 index() 方法中定义它,但它不会通过对象属性或方法参数传递给 get_current_entry() 方法,因此 sql 查询将在字符串中插入一个 null 变量,从而使查询是:

...WHERE active_nw='1' AND id_nw=

导致错误。

You haven't passed $id down into the functions. You define it in your index() method, but it's not passed down via object attributes or method arguments to your get_current_entry() method, so the sql query will insert a null variable into the string, making the query be:

...WHERE active_nw='1' AND id_nw=

causing the error.

幻梦 2024-12-20 14:22:43

如果您想将 uri 与查询字符串一起使用,则必须在 application/config.php 中启用查询字符串以获取更多信息,请参阅:CodeIgniter URLs

你也可以选择

$config['allow_get_array'] = TRUE;
在最新版本的 CI 中默认为 TRUE。此选项允许您使用 $_GET[] 数组。

另一种方法(更好)是使用如下网址: http://example.com/view/entry/1 并获取 id

$this->uri->segment($n);
其中示例中 $n = 3。

If you want to use the uri with query string, you must enable query string in your application/config.php for more info see: CodeIgniter URLs

Also you have option

$config['allow_get_array'] = TRUE;
which is TRUE by default in the latest version of CI. This option permits you to use the $_GET[] array.

The other way (better) is to use url like : http://example.com/view/entry/1 and get id by

$this->uri->segment($n);
where $n = 3 in the example.

为你拒绝所有暧昧 2024-12-20 14:22:43

默认情况下,CI 取消设置 $_GET,因此您可以使用 $CI->uri->segment_array();
另外,这里有一个很好的示例,说明如何创建替换 $_GET 的辅助函数:

http://codeigniter.com /wiki/alternative_to_GET

By default, CI unsets $_GET, so you can use $CI->uri->segment_array();
Also, here is a nice example on how you can create a helper function that replaces $_GET:

http://codeigniter.com/wiki/alternative_to_GET

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