S A N T A Posted April 24, 2008 Share Posted April 24, 2008 Hi well I'm new to PHP (Like really new) so i don't know how to fix this. I am working on a web blog and i was trying to connect to a database (My database) and i got the mysql_fetch_assoc(): error the full error is Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\index.php on line 10 and my code is (I have 3 files) header.php = <?php require("config.php"); $db = mysql_connect($dbhost, $dbuser, $dbpassword); mysql_select_db($database, $db); ?> <DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title><?php echo $config_blogname; ?></title> <link rel="stylesheet" href="stylesheet.css" type="text/css" /> </head> <body> <div id="header"> <h1><?php echo $config_blogname; ?></h1> </div> <div id="menu"> <a href="index.php">home</a> footer.php = </div> <div id="footer"> © <?php echo $config_author; ?> </div> </body> </html> index.php = <?php require("header.php"); $sql = "SELECT entries.*, categories.cat FROM entries, categories WHERE entries.cat_id = categories.id ORDER BY dateposted DESC LIMIT 1;"; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); echo "<h2><a href='viewentry.php?id=" . $row['id'] . "'>" . $row['subject'] . "</a></h2><br />"; echo "<i>In <a href= viewcat.php?id=" . $row['cat_id'] ."'>" . $row['cat'] . "</a> - Posted on " . date("D jS F Y g.iA", strtotime($row['dateposted'])) . "</i>"; echo "<p>"; echo nl2br($row['body']); echo "</p>"; require("footer.php"); ?> but i cant seem to make it work so all help appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/102748-mysql_fetch_assoc-error/ Share on other sites More sharing options...
947740 Posted April 24, 2008 Share Posted April 24, 2008 You have to connenct to the database to perform a query on your index.php page. EDIT: Arrrgh. You already made the connection in your header.php. I did not realize that. Quote Link to comment https://forums.phpfreaks.com/topic/102748-mysql_fetch_assoc-error/#findComment-526240 Share on other sites More sharing options...
The Little Guy Posted April 24, 2008 Share Posted April 24, 2008 change: $result = mysql_query($sql); to $result = mysql_query($sql)or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/102748-mysql_fetch_assoc-error/#findComment-526245 Share on other sites More sharing options...
S A N T A Posted April 24, 2008 Author Share Posted April 24, 2008 wow thanks now for some reason its just saying "No database selected" and i have another file (config.php) thats suppost to connect heres the code <?php $dbhost = "Localhost"; $dbuser = "root"; $dbpassword = ""; $dbdatabase = "blogtastic"; $config_blogname = "Funny old world"; $config_author = "A person"; $config_basedir = "http://127.0.0.1/sites/blogtastic/"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/102748-mysql_fetch_assoc-error/#findComment-526246 Share on other sites More sharing options...
The Little Guy Posted April 24, 2008 Share Posted April 24, 2008 Try and change this: $db = mysql_connect($dbhost, $dbuser, $dbpassword); to this: $db = mysql_connect($dbhost, $dbuser); and lowercase the word "Localhost" to "localhost" (may be case sensitive) Quote Link to comment https://forums.phpfreaks.com/topic/102748-mysql_fetch_assoc-error/#findComment-526262 Share on other sites More sharing options...
wildteen88 Posted April 24, 2008 Share Posted April 24, 2008 In config.php you are assigning the $dbdatabase variable your database name. However in header.php on this line: mysql_select_db($database, $db); You are using a variable called $database. Change $database to $dbdatabase in header.php Quote Link to comment https://forums.phpfreaks.com/topic/102748-mysql_fetch_assoc-error/#findComment-526324 Share on other sites More sharing options...
S A N T A Posted April 24, 2008 Author Share Posted April 24, 2008 ok well now for some reason i get the error Table 'blogtastic.categories' doesn't exist and it does? sorry for me being noobish its just this is my first time working with MySQL and PHP Quote Link to comment https://forums.phpfreaks.com/topic/102748-mysql_fetch_assoc-error/#findComment-526544 Share on other sites More sharing options...
wildteen88 Posted April 24, 2008 Share Posted April 24, 2008 blogtastic should be your database and categories should be your table. Make sure you have spelt the table name correctly in your query. You may have misspelt the table name when your created it, but didn't realise it at the time. Quote Link to comment https://forums.phpfreaks.com/topic/102748-mysql_fetch_assoc-error/#findComment-526558 Share on other sites More sharing options...
S A N T A Posted April 24, 2008 Author Share Posted April 24, 2008 WOOT!!!!! THANK YOU SOOOO MUCH!!!!!!!! IT WORKS Quote Link to comment https://forums.phpfreaks.com/topic/102748-mysql_fetch_assoc-error/#findComment-526559 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.