-1

Can someone please fill in the root, db name, db user, MyPassword in the code below? I cant seem to get it correct.

mysql16.000webhost.com a8592246_dbname a8592246_dbuser MyPassword


<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'practice');
define('DB_USER','root');
define('DB_PASSWORD','');

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " .                   mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " .     mysql_error());
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else
{
echo “Successfully connected to your database…”;
}
?>
6
  • Just change the strings, 'localhost', 'practice', etc. with your real details. What problem are you having? What errors do you see?
    – gen_Eric
    Feb 27, 2014 at 21:24
  • Are you testing your code in the server or locally?
    – esdebon
    Feb 27, 2014 at 21:26
  • do I only have to fill in the 4 first lines (exept <?php ofc) lets start with this question :)
    – NightieeX
    Feb 27, 2014 at 21:26
  • Do you not understand what this code is doing? Yes, you just need to update the define() statements.
    – gen_Eric
    Feb 27, 2014 at 21:29
  • This line will fail echo “Successfully connected to your database…”; - Curly quotes; beautiful, yet deadly. Feb 27, 2014 at 21:54

3 Answers 3

3

Firstly, you're mixing mysqli_* with mysql_* functions and they do not not mix together.

Sidenote: I use the same server myself, by the way. (for testing purposes).

Use this and fill in your own DB credentials.

N.B.:

On that server, you do not use localhost but mysql16.000webhost.com and your curly quotes and would have caused a 500 error, so I changed them to standard quotes ".

<?php
$DB_HOST = "mysql16.000webhost.com";
$DB_NAME = "xxx";
$DB_PASS = "xxx";
$DB_USER = "xxx";

$db = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($db->connect_errno > 0) {
  die('Connection failed [' . $db->connect_error . ']');
}
else
    {
    echo "Successfully connected to your database";
    }
?>
4
  • The 4 first lines I got correct from the help from you guys, however I leave that script, was to much errors, I need to get in contact with a web developer, anyway, thanks for all the answers! :)
    – NightieeX
    Feb 27, 2014 at 22:08
  • 1
    You're welcome. My (complete) answer will get you up and running (used like that with the rest of your DB credentials filled in) because I use the same server as you. @NightieeX - The other answers did not notice or fix the mix of SQL functions. Feb 27, 2014 at 22:11
  • 1
    If you feel that my answer solved the problem (which I believe has), do mark as accepted. Cheers @NightieeX - That way it won't stay in the unanswered category. Feb 27, 2014 at 22:25
  • 1
    You're quite welcome @NightieeX - Glad I could help. Feb 28, 2014 at 0:06
1
<?php
define('DB_HOST', "localhost");
define('DB_NAME', "a8592246_dbname");
define('DB_USER',"a8592246_dbuser");
define('DB_PASSWORD',"MyPassword");

$con = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " .                   mysql_error());
$db = mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " .     mysql_error());
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else
{
echo “Successfully connected to your database…”;
}
?>
1
<?php
define('DB_HOST', 'localhost');  //Normally this keep localhost
define('DB_NAME', 'a8592246_dbname');
define('DB_USER','a8592246_dbuser');
define('DB_PASSWORD','MyPassword');

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " .                   mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " .     mysql_error());
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else
{
echo “Successfully connected to your database…”;
}
?>

Not the answer you're looking for? Browse other questions tagged or ask your own question.