无法通过mysqli_connect存储数据

时间:2015-06-05 08:02:21

标签: php android mysqli

嘿伙计目前我正在尝试为Android应用程序创建登录活动,我不知道为什么用户数据在注册时没有存储。 php文件有什么问题,如下所示?

Register.php存储用户注册

<?php
    $con = mysqli_connect("mysql13.000webhost.com", "a1641537_fetch", "password", "a1641537_fetchdb");

    $name = $_POST["name"];
    $username = $_POST["username"];
    $password = $_POST["password"];

    $statement = mysqli_prepare($con, "INSERT INTO Fetch (name, username, password) VALUES (?, ?, ?) ");

    mysqli_stmt_bind_param($statement, "sss", $name, $username, $password);
    mysqli_stmt_execute($statement);

    mysqli_stmt_close($statement);

    mysqli_close($con);
?>

FetchUserData.php获取用户的用户名和密码。

<?php
    $con = mysqli_connect("mysql13.000webhost.com", "a1641537_fetch", "password", "a1641537_fetchdb");

    $username = $_POST["username"];
    $password = $_POST["password"];

    $statement = mysqli_prepare($con, "SELECT * FROM Fetch WHERE username = ? AND password = ?");
    mysqli_stmt_bind_param($statement, "ss", $username, $password);
    mysqli_stmt_execute($statement);

    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement, $name, $username, $password);

    $user = array();

    while(mysqli_stmt_fetch($statement)) {
        $user[name] = $name;
        $user[username] = $username;
        $user[password] = $password;
    }

    echo json_encode($user);

    mysqli_stmt_close($statement);

    mysqli_close($con);
?>

1 个答案:

答案 0 :(得分:3)

fetch是mysql中的reserved word所以你需要在反引号中引用它。

例如:

$statement = mysqli_prepare($con, "INSERT INTO `Fetch` (name, username, password) VALUES (?, ?, ?) ");

要捕获这些类型的错误,添加错误处理很有用。我喜欢通过让msyqli在出现任何问题时抛出异常来做到这一点。

要激活它,只需将mysqli_report(MYSQLI_REPORT_STRICT);添加到脚本的顶部,并确保显示错误。

如果您没有自己添加任何try-catch块,php会在出现问题时向您显示详细的unhandled exception error,以便您可以从那里获取它。显然,在实时代码中,这不是处理它的首选方法。