Can't connect database

I’m 100% sure I’ve entered the correct details, but keep getting error.

<?php
include("config.php");
$dbservertype='mysql';

connecttodb($host,$dbname,$dbuser,$dbpwd);

function connecttodb($host,$dbname,$dbuser,$dbpwd)
{
  global $link;
  $link=mysql_connect ("$host","$dbuser","$dbpwd");
  if(!$link){die("Could not connect to MySQL");}
   mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}


if( !function_exists('apache_request_headers') ) {
function apache_request_headers()
{
    $headers = array();
    foreach ($_SERVER as $k => $v)
    {
        if (substr($k, 0, 5) == "HTTP_")
        {
            $k = str_replace('_', ' ', substr($k, 5));
            $k = str_replace(' ', '-', ucwords(strtolower($k)));
            $headers[$k] = $v;
        }
    }
    return $headers;
}
}
?>
<? 
$dbname = '';	// The name of the database 
$dbuser = ''; 	// MySQL eliteboo_bootuse 
$dbpwd = '';	// MySQL Password 
$host = 'localhost';	// Most likely you wont need to change this 
?>

I just removed the details, but on my host I got correct details.

As a general rule, whenever something’s not working it’s a good thing to look for issues in the error log. After that, go to php.net to check the proper syntax of the PHP functions so you are sure you’re doing it right.

In your code aboe mysql_connect is not used properly. You should do it like this:

mysql_connect($host, $dbuser, $dbpwd);

Then mysql_select_db:

mysql_select_db($dbname, $link);

Just in case, run this script (self explanatory):

<?php
if (!function_exists("mysql_query")) {
   echo "WARNING! The MySQL module for PHP doesn't seem to be installed!";
}
?>

What you did wrong was using the variables between quotes. Now if your script doesn’t work at least mention the error message(s) you get. Good luck!

I’ve followed your instructions and got this error :

Warning: mysql_connect() [function.mysql-connect]: Access denied for user ‘ODBC’@‘localhost’ (using password: NO) in C:\Users\Cruel\Desktop\xampp\htdocs\orgy\booter\login\includes\db.php on line 11
Could not connect to MySQL

From the error it looks like you are using empty password to login, which seems to be the problem.

include(“config.php”);

And config.php includes :

<?
$dbname = ‘’; // The name of the database
$dbuser = ‘’; // MySQL eliteboo_bootuse
$dbpwd = ‘mypasshere’; // MySQL Password
$host = ‘localhost’; // Most likely you wont need to change this
?>

I see. I noticed you used the short tag to open your config.php file. If short tags are disabled in your PHP configuration it’s normal for that error to happen because the configuration file won’t have any effect.

As a general rule you should only use the following PHP open tag:

<?php

Once I add <?php then when I click login it does nothing.

OK, maybe I should’ve been more explicit:

<?[COLOR="Red"]php[/COLOR]
$dbname = '[COLOR="Red"]database_name[/COLOR]'; // The name of the database
$dbuser = '[COLOR="Red"]database_user[/COLOR]'; // MySQL eliteboo_bootuse
$dbpwd = 'mypasshere'; // MySQL Password
$host = 'localhost'; // Most likely you wont need to change this
?>

P.S. By the way, If by “nothing” you mean executing that script you mentioned above, then everything’s OK. Your script only connects to your database - it does nothing else than connecting to your database.

As an advice, you don’t need “global $link” in your function:

function connecttodb($host,$dbname,$dbuser,$dbpwd)
{
  [COLOR="Red"]global $link; //remove this line[/COLOR]
  $link=mysql_connect ("$host","$dbuser","$dbpwd");
  if(!$link){die("Could not connect to MySQL");}
   mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}

To sum it up, this is how your function should be:

function connecttodb($host, $dbname, $dbuser, $dbpwd) {
  $link = mysql_connect($host, $dbuser, $dbpwd);
  if (!$link) { die("Could not connect to MySQL"); }
    mysql_select_db($dbname, $link) or die ("could not open db: ".mysql_error());
}

I’ve added the database name and user, I’m a beginner, but not that bad :stuck_out_tongue:

I got the info correct. But still not doing anything.

Well… it’s a good thing you paid attention to those details. I just highlighted them to avoid further questions. But you’ll have to keep in mind the <?php vs <? thing. Always use the long form, not the short one. And when you use code from someone else and notice that code doesn’t work, make sure the long tags are in there.

Here’s something you can use to test your database connection.

config.php:

<?php
$dbname = 'database_name'; // The name of the database
$dbuser = 'database_user'; // MySQL eliteboo_bootuse
$dbpwd = 'mypasshere'; // MySQL Password
$host = 'localhost'; // Most likely you wont need to change this
?>

mysql-test.php:

<?php
connecttodb($host, $dbname, $dbuser, $dbpwd);

$result = mysql_query("SHOW DATABASES");
if (mysql_num_rows($result)) {
  while ($row = mysql_fetch_assoc($result)) {
    print_r($row);
  }
} else {
  echo "Apparently this user doesn't have any databases.";
}

function connecttodb($host, $dbname, $dbuser, $dbpwd) {
  $link = mysql_connect($host, $dbuser, $dbpwd);
  if (!$link) { die("Could not connect to MySQL"); }
  mysql_select_db($dbname, $link) or die ("could not open db: ".mysql_error());
}
?>

Create mysql-test.php and access it from your browser. If you’ll

It is working, because I have another script that is working.

There is just something wrong with the current script but I can’t figure.

Whenever you want to find out what’s wrong enable error reporting and their display in the browser. See this post for the code. Put it at the beginning of the PHP file you want to test and remove it after you finish with it.

Do you got msn or anything so you could help me, if you want.

I think it would be best if we would solve this here because others will benefit from this knowledge. Let’s give it a try. So far we know you can connect to your database so half the problem is solved.

What I ment its working, I ment the other script I ran on my host.

I’m 100% positive that those scripts are allowed and work, but the code I posted in my first post is something where the problem is.

I’ll post my login files to if needed.