0

I have this function on my model, which receives a parameter so I can pre-load some information on the view page, but somehow is coming back empty:

function addTicket($idt)
    {
        //Db Connection
        $DB2 = $this-> load-> database('DB2', TRUE);
        if (!empty($idt)){

            $query = $DB2->query ("
                    Select TROUBLE_ID, ASSIGNED_DATE, CREATOR, PROBLEM_DESCRIPTION, RESOLUTION, RESOLVED_DATE 
                    FROM TABLE1
                    WHERE TROUBLE_ID = ".$idt." 
                    "); 

        if($query){

            return $query->result_array();
        }
        else 
            {

        echo 'No Queries to display';
            }
        }
        else {echo 'No results to display';}
    }

I have an Oracle DB with a ton of entries, but the query keeps coming back empty, Just in case I did an echo 'id:'.$idt.; to check if the ID is being passed. And yes it is. Also Im getting this message: Fatal error: Call to a member function result_array() on a non-object

On my view page i have this code:

foreach($results as $row){ }

And im getting this message now: A PHP Error was encountered Severity: Warning Message: Invalid argument supplied for foreach() Any idea why this is not working?

3 Answers 3

2

Is this CodeIgniter? If so a better way to check for results is if($query->num_rows() > 0) { } rather than just if($query) { }

Also if its CodeIgniter make sure you have db_debug set to TRUE in the config/database.php otherwise you won't get to see the database errors.

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

7 Comments

Yes, it is codeigniter 1.7.2, After I changed the db_debug to TRUE, i got this: ` A Database Error Occurred Error Number: Select TROUBLE_ID, ASSIGNED_DATE, CREATOR, PROBLEM_DESCRIPTION, RESOLUTION, RESOLVED_DATE FROM TABLE1 WHERE TROUBLE_ID = ABC000001067442`
I think stack stripped out some of that can you provide a screenshot? the error doesn't make any sense...
Oh nevermind. because your trouble_id is a string you need to put single quotes around it.
My trouble_id is a varchar2 and im receiving a string ABC00001, that makes sense, I just changed it and it worked, thanks!
Nevermind, now is not showing any errors, but is not loading any data
|
0

For some reason the query is failing

$this->db->_error_message();

should tell you why

$this->output->enable_profiler(TRUE); 

Should give you much more information about the query

Update:

    $DB2 = $this-> load-> database('DB2', TRUE);
    if (!empty($idt)){

        $query = $DB2->query ("

change ^^ to vv

    $DB2 = $this-> load-> database('DB2', TRUE);
    if (!empty($idt)){

        $query = $this->DB2->query ("

1 Comment

wouldnt it be $this->DB2->query() ?
0

Finally I was able to fix this problem when in my query condition i used single quotes before double quotes, like this:

Select TROUBLE_ID, ASSIGNED_DATE, CREATOR, PROBLEM_DESCRIPTION, RESOLUTION, RESOLVED_DATE 
FROM TABLE1
WHERE TROUBLE_ID = '" . $idt . "'

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.