0

为什么我的代码在线返回 500 Internal Server 错误$result = mysql_query("SELECT * FROM institutions");我做错了什么可怕的错误?我要做的就是在我刚刚向该表添加一行之后计算 MySQL 表(称为“机构”)中的行数。

    $institution_sql = "
    INSERT INTO `institutions`
    (`InstitutionName`, `HeaderPictureID`, `Description`, `DevicesInfo`, `DoingInfo`, `FacebookPage`, `Location`, `TwitterHandle`, `Website`, `CreatedAt`)
    VALUES
    (" . nz($_POST['TempInstitutionName']) . ", 74, 'N/A', 'N/A', 'N/A', 'N/A', 'On the Internet', 'N/A', 'N/A', NOW())
    ";

    $mysqli->query($institution_sql);
    if ($mysqli->errno) {
        $dbreturn['status'] = "PASSWORD_FAILURE";
    } else {
        $dbreturn['status'] = "EXEC_SUCCESS";

        $result = mysql_query("SELECT * FROM institutions");
        $rows = mysql_num_rows($result);
        echo "There are " . $rows . " rows in my table.";

        $insert_sql = "
        INSERT INTO `users`
        (`Handle`, `Email`, `FirstName`, `LastName`, `InstitutionID`, `TempInstitutionName`, `TwitterHandle`, `ProfilePictureID`, `HeaderPictureID`, `AccountType`, `CreatedAt`)
        VALUES
        (" . nz($_POST['Handle']) . ", " . nz($_POST['Email']) . ", " . nz($_POST['FirstName']) . ", " . nz($_POST['LastName']) . ", $num_rows, " . nz($_POST['TempInstitutionName']) . ", " . nz($_POST['TwitterHandle']) . ", " . nz('75') . ", " . nz('74') . ", " . nz($_POST['AccountType']) . ",NOW())
        ";

        $mysqli->query($insert_sql);
        if ($mysqli->errno) {
            $dbreturn['status'] = "EXEC_FAILURE";
        } else {
        $dbreturn['status'] = "EXEC_SUCCESS";

        $insertid = $mysqli->insert_id;

        $password_sql = "
        INSERT INTO `passwords`
        (`UserID`)
        VALUES
        ('$insertid')
        ";

        $mysqli->query($password_sql);
        if ($mysqli->errno) {
            $dbreturn['status'] = "PASSWORD_FAILURE";
        } else {
            $dbreturn['status'] = "EXEC_SUCCESS";
        }

        } //todo: use a transaction here

    }
4

5 回答 5

2

$result = mysql_query("SELECT count(*) FROM机构");

这将直接返回行数。

这个链接可以详细的告诉你

http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html

于 2013-10-11T09:48:53.280 回答
2

你的问题是你MYSQLIMYSQL

使用 mysqli 重写你的代码

    $result = $mysqli->query("SELECT * FROM institutions");
    $rows = $result->num_rows ;
  //  and so on ...

您通过 mysqli 连接,然后在代码中使用 mysql。

于 2013-10-11T09:50:34.043 回答
1

利用

$result = $mysqli->query($institution_sql);
$result->num_rows;

或者对于普通的旧 mysql

$result = mysql_query($institution_sql);
mysql_num_rows($result);
于 2013-10-11T09:52:53.803 回答
0

尝试这个:

$result = mysql_query("SELECT count(*) FROM institutions");

MySQL 文档:http ://dev.mysql.com/doc/refman/5.0/en/select.html

还有这个: http ://www.w3schools.com/sql/sql_func_count.asp

SQL COUNT(*) Syntax

COUNT(*) 函数返回表中的记录数:

于 2013-10-11T09:56:30.033 回答
0

...另外,应该是:

值 ('" .nz($_POST['TempInstitutionName']) ."', 74

请注意单引号 [除非 'nz' 函数会处理此问题]。

于 2014-09-30T07:09:31.350 回答