I am a php noob. I am following along with a tutorial. I have the same exact database tables, fields, etc and I have the same exact code as the tutorial but yet I am getting an error and his works....I searched for an answer on a few forums and the same question is asked but never really answered. I the info I am selecting from the db is there so that is not it....thanks in advanced

THE ERROR

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\jobolicious\db_fns.php on line 34

THE CODE

function db_connect(){
$connection = mysql_connect('localhost','user','pass','db')
or die('Not Connected!');
}

function find_jobs(){
db_connect();

$select ="SELECT 
			id, 
			location, 
			title, 
			company, 
			description, 
			url";

$from = "FROM
			jobs";	

$where = "WHERE
             id > 0";	

$query = $select . $from . $where;	

$result = mysql_query($query);

while ($row = mysql_fetch_array($result)){
	print_r($row);
}

}

find_jobs();

    It usually means that MySQL could not process the query, and as a result, mysql_query() returned false. So you want to check for that and handle it appropriately before trying to use the result in other functions.

    $result = mysql_query($sql);
    if($result == false) {
       // handle error here, doing whatever debugging you want to do
    }
    else {
       // looks ok so far, so now do your fetching and stuff
    }
    

    I think you'll find that your query is missing some spaces between some words. There's really no reason I see here to define the query string as 3 separate variables that are then concatenated together, but if you have a reason, then you need to ensure there are spaces (or newlines) between those string variables when you concatenate them.

      NogDog;10987715 wrote:

      It usually means that MySQL could not process the query, and as a result, mysql_query() returned false. So you want to check for that and handle it appropriately before trying to use the result in other functions.

      $result = mysql_query($sql);
      if($result == false) {
         // handle error here, doing whatever debugging you want to do
      }
      else {
         // looks ok so far, so now do your fetching and stuff
      }
      

      I think you'll find that your query is missing some spaces between some words. There's really no reason I see here to define the query string as 3 separate variables that are then concatenated together, but if you have a reason, then you need to ensure there are spaces (or newlines) between those string variables when you concatenate them.

      Thanks Ill try that. I totally agree about the variables are pointless but thats how the tutorial had it

        ok, i rewrote without variables and tested $result

        	$query = "SELECT 
           				id, 
           				location, 
           				title, 
           				company, 
           				description, 
           				url	
        				FROM jobs
        				WHERE id > 0";
        
        $result = mysql_query($query);
        
        if($result == false) {
           echo "there is a problem";
        }
        else {
           echo "it works";
        } 
        
        
        }
        
        find_jobs();

        and it said "ther is a problem"

          If [man]mysql_query/man returns boolean FALSE (indicating an error has occurred), then you need to use the MySQL error message (see [man]mysql_error/man) to determine what went wrong.

          Also, when posting PHP code, please use the board's [noparse]

          ..

          [/noparse] bbcode tags as they make your code much easier to read and analyze.

            bradgrafelman;10987739 wrote:

            If [man]mysql_query/man returns boolean FALSE (indicating an error has occurred), then you need to use the MySQL error message (see [man]mysql_error/man) to determine what went wrong.

            Also, when posting PHP code, please use the board's [noparse]

            ..

            [/noparse] bbcode tags as they make your code much easier to read and analyze.

            ok, so I now have

            	$query = "SELECT 
               				id, 
               				location, 
               				title, 
               				company, 
               				description, 
               				url	
            				FROM jobs
            				WHERE id > 0";
            
            $result = mysql_query($query);
            
            if($result == false) {
               echo mysql_error();
            }
            else {
               echo "it works";
            } 
            
            
            }
            
            find_jobs();
            
            

            And I get
            "No database connected"

              Are you sure it wasn't "No database selected" ?

              Either way, where do you select which database to use before executing that query?

                Here is the whole page

                function db_connect(){
                	$connection = mysql_connect('localhost','user','pass','db') 
                	or die('Not Connected!');
                }
                
                function find_jobs(){
                	db_connect();
                
                
                
                    $query = "SELECT 
                			id, 
                			location, 
                			title, 
                			company, 
                			description, 
                			url	
                			FROM jobs
                			WHERE id > 0";
                $result = mysql_query($query) or die ("ERROR!".mysql_error());
                if (mysql_num_rows($result) > 0) {
                while ($row = mysql_fetch_assoc($result)) {
                echo '<pre>'; print_r($row); echo '</pre>';
                }
                } else {
                echo "Didn't find anything with the query. Check the query to make sure it's correct and try again.";
                }
                
                
                
                
                }
                
                find_jobs();
                
                

                  You never tell MySQL which database to use. See [man]mysql_select_db/man.

                    as you can see i modified the code to be more simple but still get the database error

                      Also note that [man]mysql_connect/man expects the fourth parameter to be a boolean, whereas you've given it a string. Perhaps you're confusing it with [man]mysqli_connect/man?

                        Write a Reply...