6

I was trying to create an xml response using CodeIgniter. The following error gets thrown when I run the code.

This page contains the following errors:

error on line 1 at column 48: Extra content at the end of the document

Also:

Call to undefined method ci_db_mysql_driver::result()

<?php  
class Api extends CI_Controller{  
    
    function index()  
    {
        $this->load->helper('url', 'xml', 'security');
        echo '<em>oops! no parameters selected.</em>';
        
    }
    
    function authorize($email = 'blank', $password = 'blank')
    {
        header ("content-type: text/xml");
        echo '<?xml version="1.0" encoding="ISO-8859-1"?>';
        echo '<node>';
        
        if ($email == 'blank' AND $password == 'blank')
        {
                echo '<response>failed</response>';
        } 
        else 
        {
            $this->db->where('email_id', $email);
            $this->db->limit(1);
            $query = $this->db->from('lp_user_master');
            $this->get();
            $count = $this->db->count_all_results();
            
            if ($count > 0)
            {
                foreach ($query->result() as $row){
                    echo '<ip>'.$row->title.'</ip>';
                }
            }
        }
        echo '</node>';
    }
}
?>
1
  • Can you show the end result that is being parsed? Commented Nov 22, 2011 at 9:40

2 Answers 2

14

Your code here is wrong:

$this->db->where('email_id', $email);
$this->db->limit(1);
$query = $this->db->from('lp_user_master');
$this->get();

Should be, instead:

$this->db->where('email_id', $email);
$this->db->from('lp_user_master'); 
$this->db->limit(1);
$query = $this->db->get();

Now you can call $query->result(), because the result resource is there after you actually get the table results

Sign up to request clarification or add additional context in comments.

Comments

0

The controller is not the layer where database interactions should take place; that's a job for the model layer.

You seem to be trying to select a single value from the first row of the query result; this is most elegantly achieved with row(). Assuming title is a non-nullable column, you don't need to count the rows; if the value returned from row() is null then you know there is no qualifying row.

get() executes the query built upto that point, then it reset the entire query builder cache. When you call count_all_results(), you have no FROM clause, only the SELECT clause that the method provides.

Controller

public function authorize(string $email = 'blank', string $password = 'blank')
{
    header ("content-type: text/xml");
    echo '<?xml version="1.0" encoding="ISO-8859-1"?>';
    echo '<node>';
        if ($email === 'blank' && $password === 'blank') {
            echo '<response>failed</response>';
        } else {
            $this->load->model('User_model', 'UserModel');
            $title = $this->UserModel->getUserTitle($email);
            printf('<ip>%s</ip>', $title ?? 'failed');
        }
    echo '</node>';
}

Model

public function getUserTitle(string $email): ?string
{
     return $this->db
         ->get_where('lp_user_master', ['email_id' => $email], 1)
         ->row('title');
}

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.