-1

我的 php 版本是 5.2,这是旧版本。这是因为我使用免费服务器它是我的网站http://driverrecord.hostzi.com/android_login_api/include/profile.php?username=4

我希望有一个人可以帮助我。问题可能与代码有关。

错误信息:

警告:mysql_query() [function.mysql-query]:第 20 行 /home/a6578726/public_html/android_login_api/include/profile.php 中的用户 'a6578726'@'localhost'(使用密码:NO)的访问被拒绝

配置文件.php

 // array for JSON response
 $response = array();

 // include db connect class
 require_once dirname(__FILE__). '/DB_Connect.php';

 // connecting to db
 $db = new DB_CONNECT();


 // check for post data
 if (isset($_GET["username"])) {

     $username = $_GET['username'];

     // get a product from products table
    $result = mysql_query("SELECT * FROM user WHERE user_id = $username");

     if (!empty($result)) {
         // check for empty result
         if (mysql_num_rows($result) > 0) {

             $result = mysql_fetch_array($result);

             $username = array();
           $username["username"] = $result["user_id"];
           $username["first_name"] = $result["first_name"];
           $username["last_name"] = $result["last_name"];
        $username["email"] = $result["email"];
        $username["tel"] = $result["tel"];
        $username["age"] = $result["age"];
        $username["gender"] = $result["gender"];
        // success
        $response["success"] = 1;

        // user node
         $response["username"] = array();

         array_push($response["username"], $username);

        // echoing JSON response
         echo json_encode($response);
      } else {
         // no product found
         $response["success"] = 0;
         $response["message"] = "No product found";

         // echo no users JSON
         echo json_encode($response);
       }
     } else {
      // no product found
      $response["success"] = 0;
       $response["message"] = "No product found";

    // echo no users JSON
    echo json_encode($response);
  }
  } else {
    // required field is missing
    $response["success"] = 0;
   $response["message"] = "Required field(s) is missing";

  // echoing JSON response
  echo json_encode($response);


  }
   ?>

DB_Connect.php

 class DB_Connect {

    // constructor
   function __construct() {

  }

  // destructor
  function __destruct() {
      // $this->close();
  }

    // Connecting to database
    public function connect() {
        require_once 'include/config.php';
         // connecting to mysql
          $con = mysql_connect("XXXXXwebhost.com", "a6578726_driver", "abcde");
       // selecting database
       mysql_select_db(DB_DATABASE);

      // return database handler
      return $con;
    }

  // Closing database connection
  public function close() {
    mysql_close();
  }

 }

 ?>

配置文件

   <?php
  /**
   * Database config variables
   */
 define("DB_HOST", "XXXXwebhost.com");
  define("DB_USER", "a6578726_driver");
  define("DB_PASSWORD", "abcde");
  define("DB_DATABASE", "a6578726_driver");
  ?>
4

2 回答 2

1

您永远不会连接到数据库。您正在构建一个DB_Connect实例,但DB_Connect::connect永远不会调用其中的代码。因此,当您运行时mysql_query,没有活动连接。如果没有活动连接,mysql_query它将尝试使用一些默认用户名和密码建立连接,它从沙发后面或任何地方拼凑起来,这显然失败了。

于 2013-04-17T03:07:17.167 回答
1

创建DB_Connect对象不足以连接到数据库。调用connect()该对象以连接到数据库。

于 2013-04-17T02:52:17.810 回答