0

I've created a database on az.pl I wanted to use old code which works perfectly on other website. I get this error message:

Could not connect to mysql

Here's my code:

$dbh = mysqli_connect("localhost","user","password", "db") or die ("could not connect to mysql");

I've also tried:

$c = mysql_connect("localhost", "user", "password");
mysql_select_db("db");
$result = mysql_query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = mysql_fetch_assoc($result);
echo htmlentities($row['_message']);

Both with the same result, the error message.

I've searched the web but I haven't found exact problem.

Phiter
  • 13,983
  • 14
  • 50
  • 82
Neko
  • 65
  • 10
  • Sounds as though the hostname, username, or password are incorrect and/or the database doesn't exist. – Jamie Bicknell Jun 14 '16 at 15:17
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 14 '16 at 15:17
  • Hello Jay I've tried PDO code:$pdo = new PDO('mysql:host=localhost;dbname=database', 'user', 'password'); $statement = $pdo->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL"); $row = $statement->fetch(PDO::FETCH_ASSOC); echo htmlentities($row['_message']); – Neko Jun 14 '16 at 15:40
  • After trying it I got error:The www.ewelinawoloszyn.com page isn’t working www.ewelinawoloszyn.com is currently unable to handle this request. HTTP ERROR 500 – Neko Jun 14 '16 at 15:41
  • I've added error_reporting(E_ALL); ini_set('display_errors', 1); and here's error I've got:Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) – Neko Jun 14 '16 at 15:53

2 Answers2

0

Consider following the docs when debugging your connection issue:

http://php.net/manual/en/function.mysqli-connect.php

<?php
$link = mysqli_connect("localhost", "user", "password", "db");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;

    // this will tell you why the connection failed
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;

    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);
?>
MonkeyZeus
  • 19,651
  • 3
  • 32
  • 72
  • Hello MonkeyZeus. Thank you for suggestion. I have added your code and here's error I got:Error: Unable to connect to MySQL. Debugging errno: 2002 Debugging error: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) – Neko Jun 14 '16 at 15:27
  • I've added port no 3306. So its localhost:3306 and I got another error: Error: Unable to connect to MySQL. Debugging errno: 2005 Debugging error: Unknown MySQL server host 'localhost:3306' (1) – Neko Jun 14 '16 at 15:30
  • @Neko It sounds like MySQL is either not running, not installed, or configured for the wrong port. Check out http://stackoverflow.com/questions/11657829/error-2002-hy000-cant-connect-to-local-mysql-server-through-socket-var-run – MonkeyZeus Jun 14 '16 at 16:05
  • Thank you MonkeyZeus I'll try contact tech support. I've noticed there one more thing.There is external and internal host name in database info. I've tried to use it instead of local host and the error I get is:Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'db1066530_ewelina'@'h2-lsh09' (using password: YES) – Neko Jun 14 '16 at 16:49
  • Maybe I should try add new user? – Neko Jun 14 '16 at 16:50
  • @Neko Adding a new user sounds like a good idea. Did it solve your issue? – MonkeyZeus Jun 14 '16 at 17:19
  • I think it worked. I get another error message though:Hello, dear MySQL user! Notice: Undefined index: pid in /var/www/vhosts/20/115755/webspace/httpdocs/ewelinawoloszyn.com/videomockup/album_rodzinny/product.php on line 40 Warning: mysqli_query() expects parameter 1 to be mysqli, object given in /var/www/vhosts/20/115755/webspace/httpdocs/ewelinawoloszyn.com/videomockup/album_rodzinny/product.php on line 47 Warning: mysqli_error() expects exactly 1 parameter, 0 given in /var/www/vhosts/20/115755/webspace/httpdocs/ewelinawoloszyn.com/videomockup/album_rodzinny/product.php on line 47 – Neko Jun 14 '16 at 18:09
  • line 40 and following is:if (!$_GET['pid']) { $pageid = '1'; } else { $pageid = ereg_replace("[^0-9]", "", $_GET['pid']); – Neko Jun 14 '16 at 18:10
  • I dont know how to write and access database after using pdo code. – Neko Jun 14 '16 at 18:30
  • I've tried my old code again $dbh = mysqli_connect("localhost","user","password", "db") or die ("could not connect to mysql"); now I get error:Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead. The problem is I don't know how to rewrite the rest of the code. – Neko Jun 14 '16 at 18:32
  • Now I've got the page isnt working error again. Doesnt matter if I use PDO or your code MonkeyZeus.Its a blank page on firefox for some reason – Neko Jun 14 '16 at 19:15
0

I finally connected to the database. All I needed to do is to change isset to if in the following code:

if (!$_GET['pid']) {
    $pageid = '1';
} else {
    $pageid = ereg_replace("[^0-9]", "", $_GET['pid']); // filter everything but numbers for security
}
// Query the body section for the proper page
$sqlCommand = "SELECT pagebody FROM pages WHERE id='$pageid' LIMIT 1"; 
$query = mysqli_query($link, $sqlCommand) or die (mysqli_error()); 
while ($row = mysqli_fetch_array($query)) { 
    $body = $row["pagebody"];
}

I used MonkeyZeus code to connect to the database.Thank you MonkeyZeus.

Neko
  • 65
  • 10