-5

Trying to query some information onto a DB but can not seem to find the error. I've been having some trouble with PHPs MySQL functions but I would say about 90% of the fixes have been simple syntax errors.

        $TableName = "opportunities";
        $Opportunities = array();
        $SQLString = "SELECT opportunity_ID, company, city, " . 
         "start_date, end_date, position, description" . " FROM $TableName;";       
        $QueryResult = mysql_query($SQLString);

        if (mysql_num_rows($QueryResult) > 0) 
        {
            while (($Row = mysql_fetch_assoc($QueryResult)) !== FALSE) 
            {
                $Opportunities[] = $Row;
                mysql_free_result($QueryResult);
            }
        }
guyg
  • 1
  • 4
  • http://stackoverflow.com/questions/1858304/mysql-fetch-assoc-supplied-argument-is-not-a-valid-mysql-result-resource-in-p?rq=1 – Marty Apr 08 '14 at 03:23
  • http://stackoverflow.com/questions/3268691/mysql-num-fields-supplied-argument-is-not-a-valid-mysql-result-resource?rq=1 – Marty Apr 08 '14 at 03:23
  • http://stackoverflow.com/questions/3568478/mysql-mysql-fetch-assoc-supplied-argument-is-not-a-valid-mysql-result-resour?rq=1 – Marty Apr 08 '14 at 03:24
  • 2
    [And the other 800+ duplicates](http://stackoverflow.com/search?q=not+a+valid+mysql+result+resource) – Marty Apr 08 '14 at 03:27
  • Thank you. I am just starting out using stackoverflow so I still have some learning to do as far as researching other questions and asking good ones that haven't been answered yet. – guyg Apr 24 '14 at 06:06

1 Answers1

0

Make sure to add checks when making your connection as well as after the query

    $server = '127.0.0.1';
    $username = 'root';
    $password = '';
    $database = 'test';


mysql_connect($server, $username, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());

$TableName = "opportunities";
$Opportunities = array();
$SQLString = "SELECT opportunity_ID, company, city, " . 
 "start_date, end_date, position, description" . " FROM $TableName;";       
$QueryResult = mysql_query($SQLString);

if(mysql_error()) {
    die(mysql_error();
}

if($QueryResult) {

        while($Row = mysql_fetch_assoc($QueryResult)) 
        {
            $Opportunities[] = $Row;
        }
            mysql_free_result($QueryResult);
}

mysql_close();

Also, you were freeing the result $QueryResult inside the loop, so the next iteration would have no resource to grab data from.

Kraang Prime
  • 9,981
  • 10
  • 58
  • 124