-4

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_*

NiciBozz
  • 57
  • 1
  • 12
  • Follow a tutorial. When that fails, then get in touch. – Strawberry Feb 02 '17 at 08:00
  • 3
    Don't mix `mysql_*` and `mysqli_*`. – jeroen Feb 02 '17 at 08:00
  • what OS are you using? – Masivuye Cokile Feb 02 '17 at 08:02
  • do you even have `mysqli` extension enabled? – Masivuye Cokile Feb 02 '17 at 08:04
  • I have changed it to mysql, but it still doesn't work – NiciBozz Feb 02 '17 at 08:06
  • please run `phpinfo();` and see if you have mysqli enabled. – Masivuye Cokile Feb 02 '17 at 08:09
  • mysqli is enabled – NiciBozz Feb 02 '17 at 08:13
  • try changing localhost to 127.0.0.1 – Masivuye Cokile Feb 02 '17 at 08:15
  • 2
    That's silly, `mysql_*` has been deprecated for a long time already and now you have invalidated your sql even more as you don't have a database selected. Just get the error that `mysqli` generates. – jeroen Feb 02 '17 at 08:16
  • the database is called 'phpmodul' – NiciBozz Feb 02 '17 at 08:21
  • Also, the `E_ALL` in `error_reporting()` is a constant that represents an integer, so the quotes in your `error_reporting('E_All');` should be removed. The actual issue is because you have a space in your definition of `MYSQL_HOST` - remove that. – Qirel Feb 02 '17 at 08:40
  • 1
    [**Please, don't use `mysql_*` functions in new code**](http://stackoverflow.com/q/12859942). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://php.net/mysql-connect)? Learn about [*prepared statements*](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) can help you decide which. – Qirel Feb 02 '17 at 08:41
  • before you comment, I have already solved the problem in an answer below. – NiciBozz Feb 02 '17 at 08:57
  • `mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE)` that was the first reason why it failed you. mysql_connect only uses 3 arguments as opposed to mysqli_connect that uses 4. Next time, read the manuals; it would have avoided this question entirely. – Funk Forty Niner Feb 02 '17 at 12:47

2 Answers2

1

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

?>
NiciBozz
  • 57
  • 1
  • 12
0

Hello Dear I get you problem,

Please Remove Space From

define('MYSQL_HOST', 'localhost');

OR Replace line

define('MYSQL_HOST ', 'localhost');

TO

define('MYSQL_HOST', 'localhost');
Kaushik solanki
  • 438
  • 3
  • 15