fetch a result of an alias in a query

Go To StackoverFlow.com

0

i have the following query:

$timecheck = $db->query("SELECT (B <= NOW()) AS var FROM table1 WHERE x='$x'");          
            while ($row = $db->fetch_object()){ 
                if ($row->var != 0){
                        $updatestatus = $db->query("UPDATE table2 SET abc='1' WHERE x='$x'");
                    }
            }  

and get the following errormessage:

Fatal error: Call to undefined method mysqli::fetch_object()  

that relates to this line:

while ($row = $db->fetch_object()){  

i also was trying to use:

while ($row = $db->fetch_object($timecheck)){  

without any success. So in the manual is nothing written about how to use an alias by fetch-method.

it would be great if there is someone who could tell me what am i doing wrong. thanks a lot.

2012-04-04 18:25
by bonny


2

Try this

Mysqli::query does not have fetch_object method it would return mysqli_result::fetch_assoc for more information please look at

http://www.php.net/manual/en/mysqli.query.php

http://php.net/manual/en/mysqli-result.fetch-assoc.php

http://www.php.net/manual/en/mysqli-result.fetch-object.php

Example :

$result = $db->query ( "SELECT (B <= NOW()) AS var FROM table1 WHERE x='$x'" );
while ( $row = $result->fetch_object () ) {
    if ($row->var != 0) {
        $updatestatus = $db->query ( "UPDATE table2 SET abc='1' WHERE x='$x'" );
    }
} 

Thanks

:)

2012-04-04 18:30
by Baba
yes. thats it. you saved my day :) would like to upvote but i have not enough reputations : - bonny 2012-04-04 18:36
All you need to do is just accept ... you can still do that even if you don't have reputatio - Baba 2012-04-04 18:38
ah sorry, i forget to say $db is mysql - bonny 2012-04-04 18:38
its ok .. i instantly knew it was mysqli ... one more thing accept button is below the reputation arro - Baba 2012-04-04 18:40
i know, i just had to wait 3min ... curious about that is, that it works with mysqli - bonny 2012-04-04 18:41
Don't worry .. as use PHP .. you would be able to run and debug scripts without running i - Baba 2012-04-04 18:42
i dont understand why this works when: while ($row = $timecheck->fetchobject()){ means that $timecheck is = mysqli(or $db)query("...") - bonny 2012-04-04 18:44
Ads