【发布时间】:2014-07-03 08:44:54
【问题描述】:
我正在开发一个 Android 应用程序,因为我要求用户提供 USER_ID 和密码,然后想要验证该用户,为此我编写了一个 php 脚本 [我正在使用 MySQL 数据库( WAMP Server)] 但不知道为什么,我收到以下错误-
mysql_num_rows() 期望参数 1 是资源,对象在 C:\wamp\www\android_connect\authenticate_user.php 中的第 35 行
我想在这里提到的另一件事是:我正在传递/输入正确的用户名和密码(存在于数据库中)但它仍然给我以下消息
不是有效用户(未找到)
为了更好地理解,请参阅下面给出的authenticate_user.php 的代码和 LogCat 文本。
authenticate_user.php:
<?php
/*
* Following code will authenticate the user
* User_ID & Password are read from HTTP Post Request
*/
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['user_id']) && isset($_POST['password']))
{
$user_id = $_POST['user_id'];
$password= $_POST['password'];
echo "Inside PHP Script" . PHP_EOL;
echo $user_id . PHP_EOL;
echo $password . PHP_EOL;
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
// mysql checking if user exist.
$result = mysqli_query($con,"SELECT * FROM tbl_user_master WHERE user_id = '".$user_id."' AND password = '".$password."'");
if (!empty($result))
{
// check for empty result
if (mysql_num_rows($result) > 0)
{
$response["success"] = 1;
$response["message"] = "Dnyanesh";
// echoing JSON response
echo json_encode($response);
}
else
{
// no product found
$response["success"] = 0;
$response["message"] = "Not a valid user (Not Found)";
// echo no users JSON
echo json_encode($response);
}
}
else
{
// no product found
$response["success"] = 0;
$response["message"] = "Not a valid user (Empty result)";
// 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);
}
?>
以下是我的 LogCat:
注意:这里的 123、123 分别是我的 USER_ID 和 PASSWORD。并且存在于数据库中
07-03 07:12:51.514: V/SubWay(1333): *****register response ******: Inside PHP Script
07-03 07:12:51.514: V/SubWay(1333): 123
07-03 07:12:51.514: V/SubWay(1333): 123
07-03 07:12:51.514: V/SubWay(1333): <br />
07-03 07:12:51.514: V/SubWay(1333): <font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
07-03 07:12:51.514: V/SubWay(1333): <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mysql_num_rows() expects parameter 1 to be resource, object given in C:\wamp\www\android_connect\authenticate_user.php on line <i>35</i></th></tr>
07-03 07:12:51.514: V/SubWay(1333): <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
07-03 07:12:51.514: V/SubWay(1333): <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
07-03 07:12:51.514: V/SubWay(1333): <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0000</td><td bgcolor='#eeeeec' align='right'>138656</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp\www\android_connect\authenticate_user.php' bgcolor='#eeeeec'>..\authenticate_user.php<b>:</b>0</td></tr>
07-03 07:12:51.514: V/SubWay(1333): <tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0200</td><td bgcolor='#eeeeec' align='right'>161512</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mysql-num-rows' target='_new'>mysql_num_rows</a>
07-03 07:12:51.514: V/SubWay(1333): ( )</td><td title='C:\wamp\www\android_connect\authenticate_user.php' bgcolor='#eeeeec'>..\authenticate_user.php<b>:</b>35</td></tr>
07-03 07:12:51.514: V/SubWay(1333): </table></font>
07-03 07:12:51.514: V/SubWay(1333): {"success":0,"message":"Not a valid user (Not Found)"}
请帮忙。谢谢!
【问题讨论】:
-
问题出在
authenticate_user.php,而不是你的安卓应用。您不应该在这里发布所有内容并大声呼救。另外,请使用标准英语。 -
我认为 mysqli 使用 mysqli_num_rows 而不是 mysql_num_rows 函数,即您与 mysqli 建立的连接因此期望参数 1 是资源仅用于 mysqli_num_rows ..