1

I've installed a LAMP server in a Oracle VM VirtualBox using Ubuntu 14.0 desktop recently so I could learn about PHP interactions with MySQL databases.

I cannot establish a connection with my database. It doesn't give any error messages even if I use a wrong username. I know I've set up LAMP correctly because I've run other PHP files on localhost.

This is my script:

<?php 
function conexao() {
$db = 'webservice';
$user = 'xxxxx';
$pass = 'xxxxx';
$host = 'localhost';
$conn = mysql_connect($host,$user,$pass);
mysql_select_db($db);   
}
conexao();
?>

I'm supposed to get a message when my connection info are wrong, right? Well, nothing happens, just like when every info is okay.

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
MuriloRM
  • 59
  • 1
  • 11
  • 1
    Are you using PHP version 7? You should use `mysqli` or `pdo` either way but in PHP 7 `mysql_` functions are gone. Did you check your error logs or just the page generated? – chris85 Mar 21 '16 at 15:14
  • @chris85 Well I'm quite a newbie on the subject. Haven't seen any logs so I'm just observing what people are doing and trying to reproduce it, which is not quite working... – MuriloRM Mar 21 '16 at 15:23
  • 1
    Add `error_reporting(-1); ini_set('display_errors', 1); ini_set('display_startup_errors', 1);` to the start of your script. Not sure where your error log is located but that and your PHP version can be obtained from http://php.net/manual/en/function.phpinfo.php. – chris85 Mar 21 '16 at 15:26
  • You're supposed to get an error when the connection fails (and you have full error reporting enabled). However if everything is OK you won't get anything because your script doesn't really do anything after connecting. – apokryfos Mar 21 '16 at 15:27
  • @chris85 That still gives me a blank page, but one of the answers seems to have made it work using a mysqli method. Thanks anyways! – MuriloRM Mar 21 '16 at 15:47
  • @apokryfos well even when I try to make an echo to this script to report success or failure it still gives me nothing in return, that's why I didn't think things were ok... but the problem seems to be solved when using this mysqli_connect method though. – MuriloRM Mar 21 '16 at 15:49
  • 1
    Okay, don't forget to accept that answer if it does resolve the question. `Mysqli` is a better approach anyway. – chris85 Mar 21 '16 at 15:49

2 Answers2

2

you could try this script that i am using to connect to my db

<?php
define("HOST", "localhost"); // The host you want to connect to.
define("USER", "username"); // The database username.
define("PASSWORD", "password"); // The database password. 
define("DATABASE", "webservice"); // The database name.

date_default_timezone_set('Europe/Stockholm');

$con = mysqli_connect(HOST, USER, PASSWORD, DATABASE);

if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error() . "\n";
}
mysqli_set_charset($con, "utf8");
?>

If this doesn't work it's probably an error with the request where you're calling the script.

Jonathan Nielsen
  • 1,442
  • 1
  • 17
  • 31
  • Thanks for the quick answer. I tried this script and this time when I intentionally type a wrong password, it gives me an error, so I'm assuming it's working? Need to do some tests accessing some data from that db now. – MuriloRM Mar 21 '16 at 15:26
1
function conexao() { 
$db = 'webservice'; 
$user = 'xxxxx';
$pass = 'xxxxx';
$host = 'localhost';

$conn = mysql_connect($host,$user,$pass);
if(!$conn){ 
$retval = 'Could not Connect' . mysql_error(); 
}
else if(!mysql_select_db($db)){ 
$retval = 'Could not select db' . mysql_error(); 
}
else{ 
$retval = 'Success'; 
}

return $retval;
} // end of function 

Now on your function call process the returns accordingly. *** mysql is deprecated use mysqli or PDO instead.

}

Elymentree
  • 823
  • 10
  • 22
  • Yea that doesn't really work for some reason even though we're getting return messages... Still having a blank page as response. – MuriloRM Mar 21 '16 at 15:39