MySQLi通过$ result = mysqli_query();

时间:2016-07-19 08:33:15

标签: php mysql sql mysqli

我已经回顾了你们为mysqli查询,num_rows和insert问题写的大量答案。我之前已经在mysql上做了这个(程序可能?),其中没有任何类型的mysqli或pdo。浏览网页后,为什么mysql本身会从" trend"我回去把我的代码重写为mysqli。请注意,我在一年前完成了这个基本的注册/登录工作,现在我完全迷失了。

这是我的代码,由2个文件构建,config.php和account.php:

的config.php:

<?php

// DB Settings
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

// Connect to DB
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Signals if there is an error 
if (!$conn) {
    die ("Something went wrong.");
}

我会注意到我没有关闭连接,因为我在我的account.php中使用它,请参阅下面的代码。如果它最终可行,我认为如果我使用mysqli_close($ conn)就行了;在我之前?&gt;关闭标签。

account.php

<?php

require 'config.php';

// Get session variables from input
$address = $_POST['address'];
$password = $_POST['password'];
$pin = $_POST['pin'];

// Starts the query for num_rows function ??
$result = mysqli_query($conn, "SELECT * FROM Users WHERE address='($_POST['address'])'");

if(isset($_POST)) { // Checks if Submit (button) is true
    if (strlen($address) > 26 && strlen($address) < 35) { // Validates that the variable 'address' is in between the range specified.
        if(mysqli_num_rows($result) > 0) {
            echo "Yay, u are registered!";
other code..
        } else {
        mysqli_query($conn, "INSERT INTO Users (address, password, pin) VALUES ('$address')");
        }
    }
    if (!filter_var($pin), FILTER_VALIDATE_INT) === false) {
        mysqli_query($conn, "INSERT INTO Users (address, password, pin) VALUES ('$pin')");
        }
mysqli_query($conn, "INSERT INTO Users (address, password, pin) VALUES ('$password')");
}

$mysqli->close();
?>

我使用这个简单的东西来检查来自输入字段的数据是否已经存在,如果没有,然后将其插入到数据库中。这就是我所需要的全部内容,但是我很长时间没有编码......

我的网站没有返回任何内容,因为它给了我500个内部错误。如果我从account.php中删除整个PHP代码,它会给我一个&#34;出错的地方&#34;来自config.php die()响应的消息。我在某处犯了错误或语法错误,但是我现在无法找到它超过2个小时..

提前致谢!

0 个答案:

没有答案
相关问题