flforlife Posted April 2, 2008 Share Posted April 2, 2008 Hi I'm a php novice and for some reason I keep getting an error like this: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\ccfnewsarticle\index.php on line 34 when I use a conditional statement to test the querys I get this error: Could not successfully run query (SELECT id, name, posted, summary, author_name, author_email FROM news_articles ORDER BY posted DESC LIMIT 0, 10 ) from DB: No database selected Here is the code <?php //verbus newsletter // index.php - Displays summaries of the most recent articles $hostname = 'localhost'; $username = 'vcounts'; $password = ''; //no password yet $database = 'cffnews'; $mysql = mysql_connect($hostname, $username, $password); mysql_select_db($database); $sql = "SELECT id, name, posted, summary, author_name, author_email "; $sql .= "FROM news_articles "; $sql .= "ORDER BY posted DESC "; $sql .= "LIMIT 0, 10 "; $result = mysql_query($sql); /* if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } */ ?> <h1>Fresh off the Press!</h1> <?php while($row = mysql_fetch_assoc($result)) { $id = $row['id']; $name = $row['name']; $posted = $row['posted']; $summary = $row['summary']; $author_name = $row['author_name']; $author_email = $row['author_email']; $mail_link = "mailto:$author_email"; $read_link = "article.php?id=$id"; echo "<h2>$name</h2>"; echo "<p>Posted By <a href=\"$mail_link\">$author_name</a> on $posted</p>"; echo "<p>$summary</p>"; echo "<p>To read more <a href=\"$read_link\">Click Here</a></p>"; echo "<hr />"; } ?> Here is the database code CREATE TABLE news_articles ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(65) NOT NULL DEFAULT 'Untitled Article', posted DATETIME NOT NULL, summary TEXT NOT NULL, content TEXT NOT NULL, author_name VARCHAR(65) NOT NULL DEFAULT 'Anonymous Author', author_email VARCHAR(128) DEFAULT NULL, PRIMARY KEY(id) ); CREATE TABLE images( id INT NOT NULL AUTO_INCREMENT, thumb VARCHAR(65) NOT NULL, image VARCHAR(65) NOT NULL, caption VARCHAR(128) NOT NULL, PRIMARY KEY(id) ); What is going on here HELP!!!!!!! Link to comment https://forums.phpfreaks.com/topic/99087-warning-mysql_fetch_assoc-supplied-argument/ Share on other sites More sharing options...
rhodesa Posted April 2, 2008 Share Posted April 2, 2008 Try changing: $mysql = mysql_connect($hostname, $username, $password); mysql_select_db($database); to $mysql = mysql_connect($hostname, $username, $password) or die("Could not connect to database: ".mysql_error()); mysql_select_db($database) or die("Could not select DB: ".mysql_error()); See if you get any additional errors. Also, are you using more then one database connection by any chance? Link to comment https://forums.phpfreaks.com/topic/99087-warning-mysql_fetch_assoc-supplied-argument/#findComment-507048 Share on other sites More sharing options...
flforlife Posted April 2, 2008 Author Share Posted April 2, 2008 I think I try to set the connections as variables first. I'll try the conditional statement. Link to comment https://forums.phpfreaks.com/topic/99087-warning-mysql_fetch_assoc-supplied-argument/#findComment-507713 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.