如何在 Codeigniter 中使用 $_GET
这是我网站的网址,该网址用于从数据库调用帖子,其 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您没有将
$id
传递给get_current_entry()
函数。在控制器中,将
$id
传递给模型,如下所示:修改模型的
get_current_entry()
函数以接受$id
作为参数:You're not passing
$id
to yourget_current_entry()
function.In your controller, pass
$id
to your model like so:Modify your model's
get_current_entry()
function to accept$id
as a parameter:您没有将
$id
传递给控制器中的get_current_entry()
函数。它应该如何获得该值?You are not passing the
$id
toget_current_entry()
function in your controller. How is it supposed to get that value?您尚未将 $id 传递到函数中。您可以在
index()
方法中定义它,但它不会通过对象属性或方法参数传递给 get_current_entry() 方法,因此 sql 查询将在字符串中插入一个 null 变量,从而使查询是:导致错误。
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:causing the error.
如果您想将 uri 与查询字符串一起使用,则必须在 application/config.php 中启用查询字符串以获取更多信息,请参阅:CodeIgniter URLs
你也可以选择
另一种方法(更好)是使用如下网址: http://example.com/view/entry/1 并获取 id
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
The other way (better) is to use url like : http://example.com/view/entry/1 and get id by
默认情况下,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