NiciBozz NiciBozz - 1 year ago 57
MySQL Question

Why is this SQL in this PHP not working?

My SQL in this PHP script cannot connect to my phpmyadmin-database on my XAMMP server:

<?php
//Shows every Error
error_reporting('E_All');
//used Host
define('MYSQL_HOST ', 'localhost');
//Name of SQL user
define('MYSQL_USER', 'root');
//Password of User
define('MYSQL_PASSWORD', '');
//Name of database
define('MYSQL_DATABASE', 'phpmodul');
?>

<?php
$db_link = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);
if ($db_link)
{
echo 'Connection OK:', print_r($db_link);
}
else
{
die('No connection: ' . mysql_error());
}
?>


When I execute it, it just prints
No connection: php_network_getadressinfo failed: The Host is unkown.

I am using PHP version 5.2.0 and XAMPP version 5.6.28

Edit1 Changed mysqli_* to mysql_*

Answer Source

I have found the solution:

<?php
$con = mysqli_connect("localhost","root","","phpmodul");

if (mysqli_connect_error())
  {
      echo "Connection not possible: " . mysqli_connect_error();
  }
else
  {
      echo "Connection succsessfully established.";
  }

?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download