Guys I have 2 files. index.php and connect.php

I'm getting database connection error.
This is the error i got

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in H:\wamp\www\index.php on line 3

Then i added the error detection code in the third line. I got the following message

No database selected

I'm sure i have database, username and password correct but don't understand why my database not selected. please anyone post solution

connect.php

<?

$DB_HOST = "localhost";
$DB_USER  = "admin";
$DB_PASSWORD = "admin";
$DB_NAME = "minitaskr";
$timeoutseconds = 1200; // length of session, 20 minutes is the standard
$timestamp=time();
$timeout=$timestamp-$timeoutseconds;

$connect = mysql_connect($DB_HOST,$DB_USER,$DB_PASSWORD);
mysql_select_db($DB_NAME) or die("MySQL error: Could not connect to database.\n".mysql_error());
$session = mysql_query("SELECT * FROM members WHERE id = '$_SESSION[id]' AND password = '$_SESSION[password]'");
$session = mysql_fetch_array($session);



parse_str("$QUERY_STRING");

$db = mysql_connect($DB_HOST, $DB_USER, $DB_PASSWORD) or die("Could not connect.");
if(!$db) 
	die("no db");
if(!mysql_select_db($DB_NAME,$db))
 	die("No database selected.");

if(!get_magic_quotes_gpc())
{
  $_GET = array_map('mysql_real_escape_string', $_GET);
  $_POST = array_map('mysql_real_escape_string', $_POST);
  $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);
}
else
{
   $_GET = array_map('stripslashes', $_GET);
   $_POST = array_map('stripslashes', $_POST);
   $_COOKIE = array_map('stripslashes', $_COOKIE);
   $_GET = array_map('mysql_real_escape_string', $_GET);
   $_POST = array_map('mysql_real_escape_string', $_POST);
   $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);
}

?>

index.php

<?php session_start();  include("connect.php");
$result = mysql_query("SELECT * FROM sitesettings")or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$title = "".$row["domain"]." - ".$row["tagline"]."";
$reload = $_SERVER['PHP_SELF'];
$rpp = 18; // results per page
$adjacents = 4;
$page = intval($_GET["page"]);
if(!$page) $page = 1;
$siteurl =  $row["site_url"];
}
include("header.php");

?>
Member Avatar for P0lT10n

At connection line 20 why connect again if you have already connected at line 11...

Member Avatar for diafol

You start with '<?' - make it a full tag.

When you reconnect to already existing mysql connection, you must select the database you want to work with again.

This is a mechanism in the mysql engine. it drops the database that was already selected as it assumes you want to select a different database.

There was no need for reconnection in your script as you are still using the same DB.

P0lT10n was right.! ;)

Explore :)

thanks to everyone

Member Avatar for P0lT10n

;)

We are here cos of U ;)

Explore :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.