从MySQL切换到MySQLi时出错

时间:2013-09-13 11:55:17

标签: mysql mysqli

我是PHP新手,刚刚编写了一组连接,注册和登录代码。 在MySQL上一切正常。现在我正在从MySQL切换到MySQLi,我遇到了问题。我需要一些关于这个问题的建议;我只是在mysql之后添加一个“i”或者是否有我必须改变的东西?

例如:

//ob
ob_start();


//connect to database
$error = "Problem connecting";
mysql_connect('-------','------','------') or die($error);
mysql_select_db('-------') or die($error);

它的工作原理很好,但当我改为:     // OB     ob_start();

//connect to database
$error = "Problem connecting";
mysqli_connect('-------','------','------') or die($error);
mysqli_select_db('-------') or die($error);

如果添加“i”或

,则无法连接
//ob
ob_start();


//connect to database
$error = "Problem connecting";
mysqli_connect('-------','------','------','db') or die($error);

也不像上面那样工作。 我想知道我做错了什么。

谢谢,非常感谢您的帮助,因为在PHP方面我是一个非常棒的人。

Register.php

<?php

ini_set('display_errors', 1); error_reporting(E_ALL);
include 'connect.php';

if ($_POST)
{
 //get form data
 $username = addslashes(strip_tags($_POST['username']));
 $password = addslashes(strip_tags($_POST['password']));
 $email = addslashes(strip_tags($_POST['email']));

 if (!$username||!$password||!$email)
    echo "Please fill out all fields"; 
 else
 {
    //encrypt password
    $password = md5($password);

    //check if username already taken
    $check = mysql_query("SELECT * FROM Test WHERE username = '$username'") or die(mysql_error());
    if (mysql_num_rows($check)>=1)
       echo "Username already taken";

    else
    {


       //send activation email
       $to = $email;
       $subject = "Activate your account";
       $headers = "From: abc.info";
       $body = "Hello $username,\n\nYou registered and 
           need to activate your account. Click the link below or 
           paste it into the URL bar of your browser\n\nhttp://testing.com/activation.php!";

       if (!mail($to,$subject,$body,$headers))
           echo "We couldn't sign you up at this time. Please try again later.";
       else
       {
           //register into database
            mysql_query("INSERT INTO Test (username,password,email) VALUES 
                ('$username','$password','$email');") or die(mysql_error());


echo "1 record added";


       }

    }
 }
}
else
{

?>

<form action='register.php' method='POST'>
Choose username:<br />
<input type='text' name='username'><p />
Choose password:<br />
<input type='password' name='password'><p />
Email:<br />
<input type='text' name='email'><p />
<input type='submit' name='register' value='Register'>
</form>

<?php

}

?>

1 个答案:

答案 0 :(得分:-1)

确保您的登录凭据正确无误。请检查此链接http://www.w3schools.com/php/func_mysqli_connect.asp并尝试此操作。

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>