Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/102748-mysql_fetch_assoc-error/
Share on other sites

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/";

?>

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.