无法连接到数据库

时间:2011-03-06 07:56:36

标签: mysql phpmyadmin

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

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs\Login\register.php on line 39

Warning: mysql_insert_id() [function.mysql-insert-id]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\Login\register.php on line 42

Warning: mysql_insert_id() [function.mysql-insert-id]: A link to the server could not be established in C:\xampp\htdocs\Login\register.php on line 42
Error: User not added to database.

dbConfig.php

<?
// Replace the variable values below
// with your specific database information.
$host = "localhost";
$user = "root";
$pass = "";
$db   = "charitydatabase";

// This part sets up the connection to the 
// database (so you don't need to reopen the connection
// again on the same page).
$ms = mysql_connect($host, $user, $pass);


if ( !$ms )
        {
        echo "Error connecting to database.\n";
        }

// Then you need to make sure the database you want
// is selected.
mysql_select_db($db);
?>

register.php

<?php

        // dbConfig.php is a file that contains your
        // database connection information. This
        // tutorial assumes a connection is made from
        // this existing file.
        include ("dbConfig.php");


//Input vaildation and the dbase code
        if ( $_GET["op"] == "reg" )
  {
  $bInputFlag = false;
  foreach ( $_POST as $field )
        {
        if ($field == "")
    {
    $bInputFlag = false;
    }
        else
    {
    $bInputFlag = true;
    }
        }
  // If we had problems with the input, exit with error
  if ($bInputFlag == false)
        {
        die( "Problem with your registration info. "
    ."Please go back and try again.");
        }

  // Fields are clear, add user to database
  //  Setup query
  $q = "INSERT INTO `dbuser` (`username`,`password`,`email`) "
        ."VALUES ('".$_POST["username"]."', "
        ."PASSWORD('".$_POST["password"]."'), "
        ."'".$_POST["email"]."')";
  //  Run query
  $r = mysql_query($q);

  // Make sure query inserted user successfully
  if ( !mysql_insert_id() )
        {
        die("Error: User not added to database.");
        }
  else
        {
        // Redirect to thank you page.
        Header("Location: register.php?op=thanks");
        }
  } // end if


//The thank you page
        elseif ( $_GET["op"] == "thanks" )
  {
  echo "<h2>Thanks for registering!</h2>";
  }

//The web form for input ability
        else
  {
  echo "<form action=\"?op=reg\" method=\"POST\">\n";
  echo "Username: <input name=\"username\" MAXLENGTH=\"16\"><br />\n";
  echo "Password: <input type=\"password\" name=\"password\" MAXLENGTH=\"16\"><br />\n";
  echo "Email Address: <input name=\"email\" MAXLENGTH=\"25\"><br />\n";
  echo "<input type=\"submit\">\n";
  echo "</form>\n";
  }
        // EOF
        ?>

1 个答案:

答案 0 :(得分:0)

我怀疑mysql_connect()没有建立新的连接(使用用户名root),而是重用现有的用户名ODBC

尝试更改

$ms = mysql_connect($host, $user, $pass); 

$ms = mysql_connect($host, $user, $pass, true);

因此它将被迫建立新的连接。

同时更改

$host = "localhost";

$host = "127.0.0.1";

因为Windows Vista,Windows 7和Windows Server 2008中存在一些奇怪的配置问题。