So, I just installed XAMPP on my PC running Windows 7. Here's what my website looks like:

/Header.php

<?php
session_start();
include("http://localhost/Functions.php");
connect();
?>

/Functions.php

<?php
function connect(){
$username = "dartz654";
$password = "letmein";
$hostname = "localhost";
$con = mysql_connect($hostname,$username,$password) or die ("Unable to connect");
mysql_select_db(database, $con);
}
?>

/My/Register.php

<?php
include("http://localhost/Header.php");

if(!$_POST['submit']){
    echo "<form method=\"post\">";
    echo "<input type=\"text\" name=\"username\">";
    echo "<input type=\"password\" name=\"password\">";
    echo "<input type=\"submit\" name=\"submit\" value=\"Register\">";
    echo "</form>";
}else {

$username = $_POST['username'];
$password = $_POST['password'];

$sql = "INSERT INTO `users` (`username`,`password`) VALUES ('".$username."','".$password."')";
$res = mysql_query($sql) or die(mysql_error());

echo "You've registered.";
}

?>

When I hit 'register' on the register one, it'll show an error like this:

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\My\Register.php on line 15

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs\My\Register.php on line 15
Access denied for user 'ODBC'@'localhost' (using password: NO)

Help please?

P.S. If theres some error in my code, its probably just a mistake I made when cutting out code... The actual register is alot longer, I just took out the unimportant stuff. The real problem is the connection part...

Dani AI

Generated

Short diagnosis and how this usually happens: the MySQL message "Access denied for user 'ODBC'@'localhost' (using password: NO)" normally means PHP tried to run a query without a valid, authenticated MySQL connection. On Windows the client sometimes shows "ODBC" when no username was supplied. In this thread that can happen two ways: the file that defines/opens the connection was not actually loaded into the PHP process (for example, using an HTTP/URL include fetches executed output instead of the source), or the connect call failed and the script continued to call the query anyway. (php.net)

Concrete fixes to apply immediately:

  • Replace any URL-based includes with a local include, for example require_once __DIR__ . '/Functions.php'; so the PHP source (and its function definitions) are available to your script.
  • Make the connection function return an explicit connection object and stop using implicit global connections. Prefer mysqli or PDO (the old mysql_* extension is deprecated/removed). Example (mysqli):
<?php
require_once __DIR__ . '/Functions.php';

function connect_db() {
    $mysqli = new mysqli('localhost', 'db_user', 'db_pass', 'your_db_name');
    if ($mysqli->connect_errno) {
        error_log('DB connect error: '.$mysqli->connect_error);
        die('Database unavailable.');
    }
    return $mysqli;
}
?>

Use that returned $mysqli for queries and prepared statements instead of calling the old mysql_query() with no connection. Using prepared statements also prevents SQL injection. (Also: was correct about quoting the database name or using a variable; avoid undefined-constant notices.) (php.net)

Quick troubleshooting checklist if the error persists:

  • Test the same credentials from the mysql command line (mysql -u user -p) to rule out credential/privilege problems.
  • Inspect grants for the account with SHOW GRANTS FOR 'username'@'localhost'; rather than creating an "ODBC" user as a workaround.
  • Centralise your connection in one file and require_once it on every page; log and stop on connection errors so queries never run without a link. 's idea to centralise is sound; 's note about checking mysql.user is relevant for user/privilege debugging. (dev.mysql.com)

Recommended Answers

All 5 Replies

In line 7 of /Functions.php in your post you put:

mysql_select_db(database, $con);

In this context the keyword database is neither a name or a variable. Perhaps you meant "database" or $database when you typed it?

In my connect function I use the name of my database there:

mysql_select_db('ecusersdb',$con);

Yes, "database" is the name of the database. Do i have to put the ' around it?

Yes. If you don't put either single or double quotes around it PHP will treat it as a keyword/function and not as a name.

each in every page the connection must there.
example you create a connet.php containing
mysql_connect('localhost','root','');
then include connect.php in every page.

if we look at table user :

 Select * From mysql.user;

nothing user with name 'ODBC'
but, default user to use ODBC.
i also don't know why ?

but, you can try to make user ODBC.
and then
try to given access table to user :

 grant all On [db].[tabelName] to [userName];

example

 grant all On mysql.* to root;
 grant all On mysql.* to ODBC;
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.