Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here is the code in question:

From index.php:

require_once('includes/DbConnector.php');

// Create an object (instance) of the DbConnector
$connector = new DbConnector();

// Execute the query to retrieve articles
$query1 = "SELECT id, title FROM articles ORDER BY id DESC LIMIT 0,5";
$result = $connector->query($query1);

echo "vardump1:";
var_dump($result);
echo "\n";

/*(!line 17!)*/ echo "Number of rows in the result of the query:".mysql_num_rows($result)."\n";
// Get an array containing the results.
// Loop for each item in that array


while ($row = $connector->fetchArray($result)){

echo '<p> <a href="viewArticle.php?id='.$row['id'].'">';
echo $row['title'];
echo '</a> </p>';

From dbconnector.php:

$settings = SystemComponent::getSettings();

// Get the main settings from the array we just loaded
$host = $settings['dbhost'];
$db = $settings['dbname'];
$user = $settings['dbusername'];
$pass = $settings['dbpassword'];

// Connect to the database
$this->link = mysql_connect($host, $user, $pass);
mysql_select_db($db);
register_shutdown_function(array(&$this, 'close'));

} //end constructor

//*** Function: query, Purpose: Execute a database query ***
function query($query) {

echo "Query Statement: ".$query."\n";

$this->theQuery = $query;

return mysql_query($query, $this->link) or die(mysql_error());

}

//*** Function: fetchArray, Purpose: Get array of query results ***
function fetchArray($result) {

echo "<|";
var_dump($result);
echo "|> \n";

/*(!line 50!)*/$res= mysql_fetch_array($result) or die(mysql_error());

echo $res['id']."-".$res['title']."-".$res['imagelink']."-".$res['text'];

return $res;
}

Output:

Query Statement: SELECT id, title FROM articles ORDER BY id DESC LIMIT 0,5 vardump1:bool(true) 
PHP Error Message

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /*path to*/index.php on line 17

Number of rows in the result of the query: <|bool(true) |> 

PHP Error Message

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /*path to*/DbConnector.php on line 50
share|improve this question
 
can you post your query string. –  Babiker Jun 9 '10 at 19:10
 

3 Answers

Your problem is in fallowing line:

return mysql_query($query, $this->link) or die(mysql_error())

it should have been written like this:

$result = mysql_query($query, $this->link);
if(!$result) die(mysql_error());
return $result;

It is common in dynamic languages that or returns first object which evaluates to true, but in PHP the result of X or Y is always true or false.

share|improve this answer
 
I just realized that when I echoed the mysql_error()... thanks people! –  zlance4012 Jun 9 '10 at 19:38
 
excellent shoot –  Your Common Sense Jun 9 '10 at 19:39

As you can learn from the manual, mysql_query return value type is not boolean but resource.
Something in your code converting it

share|improve this answer
 
i bet the link is valid and his db_select is failing? –  Dan Heberden Jun 9 '10 at 19:21
 
mysql_db_select() gives bool(true) in var_dump, so it's not that. –  zlance4012 Jun 9 '10 at 19:30
 
@ Col. Shrapnel - what could convert query result to a boolean? I posted code for all the $result variable lifetime. BTW, query statement works in phpmyadmin and returns valid results. –  zlance4012 Jun 9 '10 at 19:32
 
@zlance4012 nobody question your query statement. I am talking of mysql_query function result –  Your Common Sense Jun 9 '10 at 19:38

Agree with above, you have an issue with your mysql_query. It should never return True. Either false or a resource.

Refactor : mysql_query($query, $this->link) or die(mysql_error())

echo mysqlerror() after your query and see what the error message is. You probably do not have a valid connection.

share|improve this answer
 
Thanks, I will try this now. –  zlance4012 Jun 9 '10 at 19:33
 
@zlance4012 refactor it from silly childish die() to proper trigger_error() –  Your Common Sense Jun 9 '10 at 19:38
 
For example : $dbconn = mysql_pconnect($myhost, $myuser, $mypass); if (!$dbconn) trigger_error('Database connection failure', E_USER_ERROR); When you use trigger_error, the error will be processed according to PHP's error handling routines. –  Gary Jun 9 '10 at 20:55

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.