Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm getting an error in PHP on a valid query, which executes fine when run directly from PHPMyAdmin.

Has anyone had a similar problem before and could point me in the right direction?

The error is below along with the function used the execute the query.

Array 
( 
    [Error] => Invalid Query : SELECT * FROM users ORDER BY userDeleted ASC, userFullname ASC 
) 
Array 
( 
    [Error] => Empty MySQL resource. 
) 

public function query($q){ 
        if(empty($q)) $this->dbError('Empty MySQL Query.'); 
        if($this->linkID == 0) $this->connect(); 
        $temp = @mysql_query($q, $this->linkID); 
        if(!$temp) $this->dbError('Invalid Query : '.mysql_error().'<br />'.$q); 
        return $temp; 

} 

public function getUsers(){ 
    $q = "SELECT * FROM users ORDER BY userDeleted ASC, userFullname ASC"; 
    $result = $this->query($q);  

Update: The database is connected to via:

private function connect(){
        if(!$this->linkID){
            $this->linkID = @mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD);
            if(!$this->linkID) $this->dbError('Could not connect: ' . mysql_error());
            $this->select_db();
        }
        return $this->linkID;   
    }

    function select_db(){
        if($this->linkID){
            if(!@mysql_select_db(DB_NAME, $this->linkID)) $this->dbError('Can not use Database : ' . mysql_error());
        }
    }
share|improve this question
    
Is linkID valid? –  DonCallisto Jan 29 '12 at 15:19
    
Are you sure your code is calling getUsers? Assuming this is in a class, I can't see why this would error out. –  Sam152 Jan 29 '12 at 15:19
    
I take it you've selected the database which your table resides in, using mysql_select_db()? –  cb1 Jan 29 '12 at 15:20
    
Where are you selecting db and its connections . Remove that @ from @mysql_query . You will get what the error is –  Sabari Jan 29 '12 at 15:21
    
Thanks, but the database is selected I think. The strange thing is, this system is working on other sites. And its only some queries with problems. I can log in, but trying to SELECT on other queries causes problems. –  TMPilot Jan 29 '12 at 15:27
show 2 more comments

4 Answers

You didn't select a database. That means that php (or phpmyadmin) has no idea which database your talking about!

If that isn't the case, then it might be something to do with your table setup. Putting @ on methods hides all errors and warnings, which is good for production, for debugging... Not really.

share|improve this answer
add comment

You either didn't select a DB, or didn't establish a connection to the MySQL server (either that or it wasn't available, maybe the server was down or incorrect settings/password?)

share|improve this answer
add comment

Remove all these @'s and run your code again.
Make sure you can see errors occurred.

share|improve this answer
add comment

Take out the @ symbols and see if that shows any errors.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.