1

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

5 Answers 5

3

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...
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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.

Comments

1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.