Mysql连接错误

时间:2012-09-12 05:16:30

标签: mysql mysqli

<?php
   function getMysqlConnection($host,$user,$pass,$database){
     global $mysqli;
     global $Mysqlerror;
       $mysqli = new mysqli('$host','$user','$pass','$database');
  if(empty($mysqli->connect_errorno) == false){
      $Mysqlerror = "true";
  }else{
      $Mysqlerror = "false";
  } }
   ?>

我创建了一个上面的函数,它实现了给定mysql用户凭据的连接 我使用

实现了这个功能
<?php
require 'myFunc.php';
getMysqlConnection("localhost", "wronguser","wrongpass" ," test");
 echo $Mysqlerror;
 ?>

虽然我使用了错误的密码和用户名,但Mysqlerror是错误的。我也用过或死过的功能什么也没发生

Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /opt/lampp/htdocs/mysite/myFunc.php on line 5

Warning: mysqli::mysqli(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in /opt/lampp/htdocs/mysite/myFunc.php on line 5

我像往常一样重新启动了灯泡

Starting XAMPP for Linux 1.8.0...
XAMPP: Starting Apache with SSL (and PHP5)...
XAMPP: Starting MySQL...
Warning: World-writable config file '/opt/lampp/etc/my.cnf' is ignored
XAMPP: Starting ProFTPD...
/opt/lampp/share/lampp/alladdons: line 23: /opt/lampp/share/addons/: is a directory
XAMPP for Linux started.

我的代码或我的服务器有问题吗?如何解决?

2 个答案:

答案 0 :(得分:4)

这条线存在问题。

$mysqli = new mysqli('$host','$user','$pass','$database');

将其更改为

$mysqli = new mysqli($host,$user,$pass,$database);

在PHP中,单引号内的变量不会使用它们的值进行插值。

检查PHP的变量解析机制here

答案 1 :(得分:0)

首先检查你的mysql ..正确提供用户名和密码(wat你在mysql中给出的).. 示例数据库连接可在w3schools.com和tizag.com中找到