MySQL的获取数组预计参数1是资源 [英] MySQL fetch array expects parameter 1 to be resource
问题描述
< PHPinclude_once'DB_Connect.php';功能getCategories(){
$ DB =新DB_Connect();
//数组JSON响应
$响应=阵列();
$响应[学生] =阵列(); // mysql的选择查询
$结果= mysql_query(SELECT * FROM学生); 而($行= mysql_fetch_array($结果)){
//临时数组创建单一类别
$ TMP =阵列();
$ TMP [身份证] = $行[身份证];
$ TMP [名称] = $行[名称];
$ TMP [数量] = $行[数量];
$ TMP [地址] = $行[地址];
//类推到最终JSON数组
array_push($响应[学生],$ TMP);
} //保持响应头为JSON
标题(内容类型:应用程序/ JSON'); //呼应JSON结果
回声json_en code($响应);
}getCategories();?>
我有这个API为我的Android应用程序,但我有下面这个错误的问题。任何想法了吗?
< BR />
< B>警告< / B>:mysql_fetch_array()预计参数1是资源,布尔中给出< B> ... \\&get_Student.php LT; / B>上线474; B> 13 LT; / B>< BR />
{学生:[]}
的mysql _ *
正式去precated。请使用 PDO
或的mysqli
。这就是你如何与库MySQLi库做到这一点:
< PHP//连接到数据库
$库MySQLi =新的mysqli(本地主机,my_user,MY_PASSWORD,MY_DATABASE);
如果(mysqli_connect_errno()){
的printf(连接失败:%S \\ n,mysqli_connect_error());
出口();
}//查询功能
功能getCategories()
{
// 在里面
全球$ mysqli的;
$响应=阵列('学生'=>阵()); //查询分贝
$查询=SELECT * FROM学生;
如果($结果= $ mysqli->查询($查询)){
而($行= $ result-> FETCH_ASSOC()){
$响应['学生'] [] =数组(
'ID'=> $行['身份证'],
'名'=> $行[名称]
'数'=> $行['号'],
'地址'=> $行['地址'],
);
}
$ result->免费(); //释放内存
} //完成
标题(内容类型:应用程序/ JSON');
出口(json_en code(响应));
}//测试
getCategories();//关闭连接分贝
$ mysqli->关闭();
您可以有这个code:
//连接到数据库
$库MySQLi =新的mysqli(本地主机,my_user,MY_PASSWORD,MY_DATABASE);
如果(mysqli_connect_errno()){
的printf(连接失败:%S \\ n,mysqli_connect_error());
出口();
}
在一个名为 db_connect.php 并可以将其包含在您需要数据库连接的脚本。因为变量 $的mysqli
启动了函数之外(){...}
范围;你需要使用全球$ mysqli的;
来访问它。无论连接code是在同一个文件或者被包括来自外部文件的适用。快乐编码。
<?php
include_once 'DB_Connect.php';
function getCategories() {
$db = new DB_Connect();
// array for json response
$response = array();
$response["Student"] = array();
// Mysql select query
$result = mysql_query("SELECT * FROM Student");
while ($row = mysql_fetch_array($result)) {
// temporary array to create single category
$tmp = array();
$tmp["id"] = $row["id"];
$tmp["name"] = $row["name"];
$tmp["number"] = $row["number"];
$tmp["address"] = $row["address"];
// push category to final json array
array_push($response["Student"], $tmp);
}
// keeping response header to json
header('Content-Type: application/json');
// echoing json result
echo json_encode($response);
}
getCategories();
?>
I have this API for my Android app, but I have a problem with this error below. Any idea out there?
<br />
<b>Warning</b>: mysql_fetch_array() expects parameter 1 to be resource, boolean given in <b>...\get_Student.php</b> on line <b>13</b><br />
{"Student":[]}
mysql_*
is officially deprecated. Use either PDO
or mysqli
. This is how you do it with MySQLi library:
<?php
// Connect To Db
$mysqli = new mysqli("localhost", "my_user", "my_password", "my_database");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// Query Function
function getCategories()
{
// Init
global $mysqli;
$response = array('Student' => array());
// Query Db
$query = "SELECT * FROM Student";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
$response['Student'][] = array(
'id' => $row['id'],
'name' => $row['name'],
'number' => $row['number'],
'address' => $row['address'],
);
}
$result->free(); // free up memory
}
// Finished
header('Content-Type: application/json');
exit(json_encode(response));
}
// Test
getCategories();
// Close Db Connect
$mysqli->close();
You can have this code:
// Connect To Db
$mysqli = new mysqli("localhost", "my_user", "my_password", "my_database");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
In a file called db_connect.php and you can include it on the script you require db connection. Because the variable $mysqli
is initiated outside of the function() { ... }
scope; you need to use global $mysqli;
to get access to it. This applies regardless of the connection code being on same file or being included from external file. Happy coding.
这篇关于MySQL的获取数组预计参数1是资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!