-4

Master. I need help. I got some the following error :

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/uxxxxxxxxx/public_html/index.php on line ...

DB :

CREATE TABLE IF NOT EXISTS `pages` (
`pageID` int(11) NOT NULL AUTO_INCREMENT,
`pageTitle` varchar(255) DEFAULT NULL,
`pageCont` text,
PRIMARY KEY (`pageID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

index.php

<?php require('includes/config.php'); ?>
<!DOCTYPE>
<html>
<head>
<title>My Page</title>
</head>
<body>
<?php   
if(!isset($_GET['p'])){
    $q = mysql_query("SELECT * FROM pages WHERE pageID='$id'");
} 

//get page data from database and create an object
$r = mysql_fetch_object($q);

//print the pages content
echo "<h2>$r->pageTitle</h2>";
echo $r->pageCont;
?>
</body>
</html>

Thanks in advance.

1 Answers1

0

You need to assign a value to $id, before using it within your mysql query.

You should additionally catch any mysql errors (see the proposed duplicate of Mike W).

Last but not least mysql_ is deprecated by now you should use mysqli_ or PDO instead. Further it is not adviseable to insert any variable into a mysql query, without escaping it. Use a parameterized query instead. This prevents sql injection attacks.

Daniel
  • 3,541
  • 3
  • 33
  • 46