Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100



Go Back   PHP-Editors > Programming Help > PHP Programming Help

PHP Programming Help Post any question relating to PHP Programming here and hopefully someone can help.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 2008-04-02, 01:31 AM
Junior Member
 
Join Date: Apr 2008
Posts: 7
flforlife is on a distinguished road
Default Warning: mysql_fetch_assoc(): supplied argument

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!!!!!!!
Reply With Quote
  #2 (permalink)  
Old 2008-04-14, 01:33 AM
Junior Member
 
Join Date: Apr 2008
Location: Cheltenham, UK
Posts: 12
hoopyfrood is on a distinguished road
Thumbs up FIXED: mysql_fetch_assoc() error

Hi flforlife,

quite an easy solution, looks like you have forgotten to specify the established connection to use when looking for the database to connect to. Just add a second argument to your mysql_select_db() and off should you go!

PHP Code:
//    connect to database
function connect_to_database()
{
    
$database                            =    array();
    
$database['path']                    =    'path';
    
$database['password']                =    'password';
    
$database['user_name']               =    'user_name';
    
$database['name']                    =    'database';

    
$connection                          =    mysql_connect($database['path'], $database['user_name'], $database['password']) or die('no');
    
mysql_select_db($database['name'], $connection) or die(mysql_error());

This is more or less my generic connect_to_database() function, handy if you need to esatblish one and only ever one connection to the same database throughout the lifetime of your application. Easy to modify, hard to hack/hijack...

Good luck with your development,
hoopyfrood
__________________
Love your documents!

DocuView - The best Web 2.0 document management system (DMS) - utilising the latest technology to help your company achieve ISO 9001 certification.
Reply With Quote
Must read Review for Serious PHP Developers


NuSphere PhpED 5.0 : The Staff of php-editors.com recently spent a few days working with NuSphere PhpED 5.0 (a popular PHP IDE) and NuCoder 1.4 (a PHP Encoding Utility), read up on all the details.

Sponsored Links
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 06:39 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0
© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.