我有3个php文件来连接phpmyadmin数据库。
/Android/db_connect.php的内容:
<?php
/**
* A class file to connect to database
*/
class DB_CONNECT {
// constructor
function __construct() {
// connecting to database
$this->connect();
}
// destructor
function __destruct() {
// closing db connection
$this->close();
}
/**
* Function to connect with database
*/
function connect() {
// import database connection variables
require_once __DIR__ . '/db_config.php';
// Connecting to mysql database
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
// Selecing database
$db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
// returing connection cursor
return $con;
}
/**
* Function to close db connection
*/
function close() {
// closing db connection
mysql_close();
}
}
?>
/Android/db_config.php的内容:
<?php
/*
* All database connection variables
*/
define('DB_USER', "u155019120_movie"); // db user
define('DB_PASSWORD', "xxxxxxxxx"); // db password (mention your db password here)
define('DB_DATABASE', "u155019120_movie"); // database name
define('DB_SERVER', "mysql.hostinger.web.tr"); // db server
?>
/ publichtml / get_all_products的内容:
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/../Android/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// get all products from products table
$result = mysql_query("SELECT *FROM products") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["products"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["pid"] = $row["pid"];
$product["name"] = $row["name"];
$product["price"] = $row["price"];
$product["created_at"] = $row["created_at"];
$product["updated_at"] = $row["updated_at"];
// push single product into final response array
array_push($response["products"], $product);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echo json_encode($response);
}
?>
当我打开http://moviestolike.pe.hu/get_all_products.php页面时(也可以查看),它会显示
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/u155019120/Android/db_connect.php on line 28
{"products":[{"pid":"1","name":"sogan","price":"19.30","created_at":"2016-03-12 15:08:38","updated_at":"0000-00-00 00:00:00"},{"pid":"2","name":"faltak","price":"22.00","created_at":"2016-03-12 15:08:38","updated_at":"0000-00-00 00:00:00"}],"success":1}
它提供了正确的产品。但是,当我将弃用的方法更改为
时 // Connecting to mysql database
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
在我写的第一个文件中,输出是
Deprecated: mysql_select_db(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/u155019120/Android/db_connect.php on line 31
Access denied for user ''@'10.2.1.37' to database 'u155019120_movie'
有什么问题?我甚至都不知道10.2.1.37是什么。我的用户名是我在这里写的。
此外,我无法将mysql_select更改为mysqli_select,它还需要一个参数。
答案 0 :(得分:0)
更正mysqli_connect的语法
mysqli_connect(host,username,password,dbname,port,socket);
端口和套接字字段是可选的,您可以将它们用作
来忽略它们mysqli_connect(host,username,password,dbname);