7
<?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";
    }
}

I have created a function as above which implement connection on given MySQL user credentials and I implemented this function by using

<?php
require 'myFunc.php';
getMysqlConnection("localhost", "wronguser", "wrongpass", " test");
echo $Mysqlerror;

Although i used wrong password and username the Mysqlerror is false . I also used or die function nothing happen

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

I restarted lampp as usual it showed

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.

Is the problem with my code or with my server some thing. How to fix it?

Dharman
  • 30,962
  • 25
  • 85
  • 135
gokul
  • 92
  • 1
  • 1
  • 8

2 Answers2

4

Problem is with this line.

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

Change it to

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

In PHP variables within single quotes are not interpolated with their values.

Check PHP's variable parsing mechanism here

Dharman
  • 30,962
  • 25
  • 85
  • 135
verisimilitude
  • 5,077
  • 3
  • 30
  • 35
0

Check your mysql first..provide username and password correctly(wat u gave in mysql).. sample database connection is available in w3schools.com and tizag.com

manikandan
  • 98
  • 8
  • actually this made the same warning. I am on a project that get users mysql data and get connection. So I used this function to redurn $Mysqlerror if they had given wrong data – gokul Sep 12 '12 at 05:23