[英]How to use $_GET in Codeigniter
here is a url of my website, this url is used to call the post from database which its id=1 这是我网站的网址,该网址用于从ID为1的数据库中调用帖子
http:// * .com/?view=entry&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
You're not passing $id
to your get_current_entry()
function. 您没有将
$id
传递给get_current_entry()
函数。
In your controller, pass $id
to your model like so: 在您的控制器中,将
$id
传递给模型,如下所示:
$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: 修改模型的
get_current_entry()
函数以接受$id
作为参数:
function get_current_entry($id){
// other code here...
}
You are not passing the $id
to get_current_entry()
function in your controller. 您没有将
$id
传递给控制器中的get_current_entry()
函数。 How is it supposed to get that value? 如何获得该价值?
You haven't passed $id down into the functions. 您尚未将$ id传递给函数。 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: 您可以在
index()
方法中定义它,但是不会通过对象属性或方法参数将其传递给get_current_entry()方法,因此sql查询将在字符串中插入一个null变量,从而使查询成为:
...WHERE active_nw='1' AND id_nw=
causing the error. 导致错误。
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 如果要对查询字符串使用uri,则必须在application / config.php中启用查询字符串以获取更多信息,请参阅: CodeIgniter URL
Also you have option 你也有选择
$config['allow_get_array'] = TRUE;
$ config ['allow_get_array'] = TRUE; which is TRUE by default in the latest version of CI.
在最新版本的CI中,默认情况下为TRUE。 This option permits you to use the $_GET[] array.
此选项允许您使用$ _GET []数组。
The other way (better) is to use url like : http://example.com/view/entry/1 and get id by 另一种方法(更好)是使用类似http://example.com/view/entry/1的网址并通过以下方式获取ID
$this->uri->segment($n);
$ this-> uri-> segment($ n); where $n = 3 in the example.
在示例中,$ n = 3。
By default, CI unsets $_GET, so you can use $CI->uri->segment_array();
默认情况下,CI会取消设置$ _GET,因此可以使用
$CI->uri->segment_array();
Also, here is a nice example on how you can create a helper function that replaces $_GET: 另外,这是一个很好的示例,说明如何创建一个代替$ _GET的辅助函数:
http://codeigniter.com/wiki/alternative_to_GET http://codeigniter.com/wiki/alternative_to_GET
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.