Welcome Guest, Not a member yet? Register   Sign In
simple_query() return...?
#1

[eluser]rvent[/eluser]
Hello,

I have this controller that takes the data from the user and inserts it into the DB. But i want to make sure that there is not other record like it so i have the following

Controller:
Code:
function testDB()
    {
        $this->load->model('Boards/SmtJob');
        
        $newPart = "TES-TEST2";
        $res = $this->SmtJob->isPart($newPart);
        echo "$newPart";
        echo "$res";
    }

Model:
Code:
function isPart($part)
    {
        echo "part is $part";
        $bool = $this->db->simple_query("SELECT PartNumber FROM PartNumber WHERE PartNumber = '$part'");
        return $bool;
    }

the $bool is being returned to the controller, but when i echo it to see what it is i get: "Resource id #46"

Isnt it supposed to return TRUE or FALSE...?

Thanks
#2

[eluser]TheFuzzy0ne[/eluser]
No. $this->db->simple_query() returns the database resource (results) for the query you just executed.

You'd probably be better of counting if any rows are returned from the query.
#3

[eluser]rvent[/eluser]
so:

Code:
$this->db->query("INSERT ......");

So the above returns TRUE if insert is successful and FALSE if it fails... right..?

Thanks
#4

[eluser]James Gifford[/eluser]
Yes that's right. Write-type queries like inserts and updates will return true/false while read-type queries will return the results.
#5

[eluser]rvent[/eluser]
Well i have this new test function in my controller

Code:
$chkPart = $this->SmtJob->isPart(); //queries DB to check is part is unique
        if ($chkPart == '100022810')
        {
            echo 'its true';
        }
        else
        {
            echo 'its false';
        }

and this on my model
Code:
$part = '100022810';
        $sqlChk = "SELECT PartNumber FROM PartNumber
                    WHERE PartNumber = ? LIMIT ?";
        
        $getPart = $this->db->query($sqlChk, array($part, 1));
        
        echo "-- model says: -- $getPart --]";
            
        return $getPart;

and i cant seem to get the result from the query. I get this on the page reload

Code:
-- Part -- TEST1 --]
A PHP Error was encountered

Severity: 4096

Message: Object of class CI_DB_mysql_result could not be converted to string

Filename: Boards/smtJob.php

Line Number: 52
-- model says: -- --]its false

Line 52 is the echo line in the model...

What am i missunderstanding...? shouldnt i be getting the value...? i am possitive that the value exist in the DB.. Thats where i got it from...

Any ideas..?

Thanks
#6

[eluser]rvent[/eluser]
NEVER MIND

I missed the line where it tells me that i need to use result()


Thanks Smile




Theme © iAndrew 2016 - Forum software by © MyBB