本文介绍了与mysqli()连接不工作,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Microsoft Azure中连接到mysql数据库时,我遇到了一个奇怪的问题。当我使用此命令连接到数据库时,查询成功。

I have a strange problem when connecting to a mysql database in Microsoft Azure. When I use this command to connect to the databases the queries works successfully.

$con = mysql_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password) or die("Could not connect database");
mysql_select_db($mysql_db_database, $con) or die("Could not select database");

但是当我使用这个命令时,它将无法工作:

But when I use this command it won't work:

$con = new mysqli($mysql_db_hostname, $mysql_db_user, $mysql_db_password, $mysql_db_database);
if ($con->connect_error)
{
    die("Database selection failed: " . $con->connect_error);
}



我没有收到消息数据库选择失败:。 $ con> connect_error。任何想法为什么第一个代码工作,而不是另一个?

I do not get the message "Database selection failed: " . $con->connect_error. Any idea why the first code work and not the other one?

我正在使用这个PHP代码的连接:

I'm using the connection for this php code:

<?php  require_once ("/connect.php");
if(isset($_POST['username']) && !empty($_POST['username'])){
     $username=mysql_real_escape_string($_POST['username']);
}else{
     $message[]='Please enter username';
}
if(isset($_POST['password']) && !empty($_POST['password'])){
     $password=mysql_real_escape_string($_POST['password']);
}else{
     $message[]='Please enter password';
}
$countError=count($message);
if($countError > 0){
 for($i=0;$i<$countError;$i++){
          echo ucwords($message[$i]).'<br/><br/>';
 }
}else{
   $query="select * from users where username='$username' and password='$password'";
   $res=mysql_query($query);
   $checkUser=mysql_num_rows($res);
   if($checkUser > 0){
      echo 'correct';
   }else{
       echo ucwords('please enter correct user details');
   }
}


推荐答案

不能混合&匹配mysql_ *函数和mysqli_ *函数。您可以使用ALL mysql_ *(已弃用)或使用所有mysqli_ *函数。
你改变了连接,显然,但还没有改变,例如,mysqli_query到mysqli_query等。

You cannot mix & match between mysql_* functions and mysqli_* functions. Either you use ALL mysql_* (deprecated) or you use ALL mysqli_* functions.You changed the connection, apparently, but did not yet change, for instance, mysqli_query to mysqli_query and etc.

请注意,不一定有1 2对应的API。换句话说,你不能只是添加一个i,希望它总是工作 - 有一些概念和语法等变化,你需要考虑到。

Do note that there is not necessarily a 1-to-1 correspondence between the 2 APIs. In other words, you can't just add an "i" and hope it will always work - there are some conceptual and syntactic and etc changes that you will need to take into account.

无论如何,看看你的PHP代码中每一次出现的mysql_,并将其转换为使用mysqli_ *函数,这应该可以解决你的问题。

In any event, look at every occurrence of "mysql_" in your PHP code and convert it to use "mysqli_*" functions and that should solve your problem.

这篇关于与mysqli()连接不工作,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 19:42