0

So I've looked at responses from other threads, but I cannot find my error. Here is my php file:

<?php

  define('con', 'con');  
  define('db_name','db_name');
  define('mysql_user','mysql_user');
  define('mysql_pass','mysql_pass');
  define('server_name','server_name');

  $db_name = "commentsdb";
  $mysql_user = "root";
  $mysql_pass = "root";
  $server_name = "localhost";
  $con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);

  if(!con)
  {
    echo "Connection Error...".mysqli_connect_error();
  }
  else {
    echo "<h3>Database connection Success...<h3>";
  }

?>

Here is my error: ( ! )

Warning: mysqli_connect(): in C:\wamp\www\webapp\init.php on line 11 Call Stack # Time Memory Function Location 1 0.0006 134536 {main}( ) ..\init.php:0 2 0.0007 135112 mysqli_connect ( ) ..\init.php:11

I've exhausted all options. Thanks!

naseeba c
  • 1,040
  • 2
  • 14
  • 30
Shanty
  • 101
  • 8

4 Answers4

0

Please use any of the mysql client to test this..connect via mysql workbench or phpmyadmin using the same credentails

Subash
  • 232
  • 1
  • 3
  • 12
0

Use this code:

//Create Databases
$servername = "localhost";
$username = "Your Database";
$password = "Your Password";
$dbname = "Your Database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
 } 

$sql = "INSERT INTO...";

/*Multiple query example:
$sql = "INSERT INTO...";
$sql .= "INSERT INTO...";
$sql .= "INSERT INTO...";
*/

//If you have multiple query use: multi_query()
    if ($conn->query($sql) === TRUE) {
            echo "Succesfully Created!";
    } else {
        echo "<pre>Error in Database! Error: " . $conn->error . "</pre>";
    }
    $conn->close();

I hope it helps!

0

Your code is working for me

Please Check the values of your variables are correct or not ($db_name | $mysql_pass )

$db_name = "commentsdb";
$mysql_user = "root";
$mysql_pass = "root";          
$server_name = "localhost";
J. Shiv
  • 144
  • 4
0

via OpenShift

rhc app create test php-5.4 mysql-5.5

1 <?php
  2     define('con', 'con');
  3     define('db_name','db_name');
  4     define('mysql_user','mysql_user');
  5     define('mysql_pass','mysql_pass');
  6     define('server_name','server_name');
  7
  8     $db_name = getenv('OPENSHIFT_MYSQL_DB_HOST');
  9     $mysql_user = getenv('OPENSHIFT_MYSQL_DB_USERNAME');
 10     $mysql_pass = getenv('OPENSHIFT_MYSQL_DB_PASSWORD');
 11     $server_name = getenv('OPENSHISHT_GEAR_NAME');
 12     $con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);
 13
 14     if(!con)
 15     {
 16       echo "Connection Error...".mysqli_connect_error();
 17     }
 18     else {
 19       echo "<h3>Database connection Success...<h3>";
 20     }
 21   ?>

Database connection Success...

Your MySQL server or user are misconfigured.

PaaS's such Openshift and many others use properly configured servers, you do not need to think about configuration servers. Easy plans usually free of charge.

zirf
  • 334
  • 2
  • 9