PHP连接错误,正确定义连接?

时间:2016-03-30 04:18:56

标签: php

所以我看过其他线程的回复,但我找不到我的错误。 这是我的php文件:

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

&GT;

这是我的错误: (!)

  

警告:mysqli_connect():在第11行的C:\ wamp \ www \ webapp \ init.php中       调用堆栈       #时间记忆功能位置       1 0.0006 134536 {main}().. \ init.php:0       2 0.0007 135112 mysqli_connect().. \ init.php:11

我已经筋疲力尽了所有选择。谢谢!

4 个答案:

答案 0 :(得分:0)

请使用任何mysql客户端通过mysql workbench或phpmyadmin使用相同的凭据来测试this..connect

答案 1 :(得分:0)

使用此代码:

//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();

我希望它有所帮助!

答案 2 :(得分:0)

您的代码正在为我工​​作

请检查变量的值是否正确($ db_name | $ mysql_pass)

row_number

答案 3 :(得分:0)

通过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   ?>

数据库连接成功...

您的MySQL服务器或用户配置错误。

PaaS这样的Openshift和许多其他人使用正确配置的服务器,您不需要考虑配置服务器。简单的计划通常是免费的。