Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 返回0行,因此它确实存在于数据库中 否则{ //此电子邮件的帐户已存在,请显示错误消息 echo“此电子邮件的帐户已存在。”; $email=“”; }_Php_Pdo_Mysqli - Fatal编程技术网

Php 返回0行,因此它确实存在于数据库中 否则{ //此电子邮件的帐户已存在,请显示错误消息 echo“此电子邮件的帐户已存在。”; $email=“”; }

Php 返回0行,因此它确实存在于数据库中 否则{ //此电子邮件的帐户已存在,请显示错误消息 echo“此电子邮件的帐户已存在。”; $email=“”; },php,pdo,mysqli,Php,Pdo,Mysqli,您应该将其余的查询全部转换为使用PDO。您的if语句将永远不会执行。您需要检查返回的行数。这就是你想要的: 注意:我最初使用的是$stmt->rowCount(),但OP说这对他不起作用。但我很确定这个错误的原因来自其他地方 if (!($stmt = $pdo->prepare("SELECT * FROM table WHERE email = ?"))) { //error } if (!$stmt->execute(array("$email"))) { //

您应该将其余的查询全部转换为使用PDO。

您的
if
语句将永远不会执行。您需要检查返回的行数。这就是你想要的:

注意:我最初使用的是
$stmt->rowCount()
,但OP说这对他不起作用。但我很确定这个错误的原因来自其他地方

if (!($stmt = $pdo->prepare("SELECT * FROM table WHERE email = ?"))) {
   //error
}

if (!$stmt->execute(array("$email"))) {
    //error
}
//The $row3 var you had was useless. Deleted that.

$count = 0;

while ($row = $stmt->fetch()) {
    $count++;
}

//The query returned 0 rows, so you know the email doesn't exist in the DB
if ($count== 0) {

    $query = "INSERT INTO table (username, email, password, join_date) VALUES ('$username', '$email', SHA('$password1'), NOW())";

    if (!mysqli_query($dbc, $query)) {
        //error
    }

    $query = "SELECT * FROM table WHERE username = '$username'";

    if (!($data2 = mysqli_query($dbc, $query))) {
        //error
    }

    while ($row = mysqli_fetch_array($data2)) {

        $recipent = '' . $row['user_id'] . '';

        $query = "INSERT INTO messages (recipent, MsgTit, MsgR, MsgA, sender, time, readb, reada, MsgCon) VALUES ('$recipent', '$MsgTit', '$MsgR', '$MsgA', '$sender', NOW(), '$readb', '$reada', '$MsgCon')";

        if (!mysqli_query($dbc, $query)) {
            //error
        }

       // Aftermath.
       echo '<p>Your new account has been successfully created. You\'re now ready to <a href="game2.php" target="_blank">log in</a>. After this you should implement basic character-details on your users profile to begin the game.</p>';

       mysqli_close($dbc);
       exit();
   }
}
//The query did not return 0 rows, so it does exist in the DB
else {
    // An account already exists for this email, so display an error message
    echo '<p class="error">An account already exists for this e-mail.</p>';
    $email = "";
}
if(!($stmt=$pdo->prepare(“从电子邮件=?”的表格中选择*)){
//错误
}
如果(!$stmt->execute(数组($email))){
//错误
}
//您拥有的$row3 var是无用的。删除了。
$count=0;
而($row=$stmt->fetch()){
$count++;
}
//查询返回了0行,因此您知道数据库中不存在该电子邮件
如果($count==0){
$query=“在表中插入(用户名、电子邮件、密码、加入日期)值('$username','$email',SHA('$password1'),NOW())”;
if(!mysqli_query($dbc,$query)){
//错误
}
$query=“从用户名为“$username”的表中选择*;
if(!($data2=mysqli_查询($dbc,$query))){
//错误
}
而($row=mysqli\u fetch\u数组($data2)){
$recipent='.$row['user_id'.'”;
$query=“在消息(recipent、MsgTit、MsgR、MsgA、sender、time、readb、reada、MsgCon)中插入值(‘$recipent’、‘$MsgTit’、‘$MsgR’、‘$MsgA’、‘$sender’、‘$sender’、‘$readb’、‘$reada’、‘$MsgCon’)”;
if(!mysqli_query($dbc,$query)){
//错误
}
//后果。
echo'您的新帐户已成功创建。您现在可以开始了。在此之后,您应该在您的用户配置文件中实现基本角色详细信息,以开始游戏。

; mysqli_close($dbc); 退出(); } } //查询未返回0行,因此它确实存在于数据库中 否则{ //此电子邮件的帐户已存在,请显示错误消息 echo“

此电子邮件的帐户已存在。

”; $email=“”; }


您应该将其余的查询全部转换为使用PDO。

errr,如果
$stmt->fetch()
返回
false
,则
while
循环已经存在,
while
循环将返回
if
,因此,如果行不存在,您将永远不会输入
if
语句你混合使用PDO和MySQLi有什么特别的原因吗?他说他正在致力于将它逐个查询转换为PDO查询。我猜他不想一下子把它全弄坏。Madara Uchiha:我是PDO的新手,在学习了一点mysql之后,我(轻松地)学习了mysql。朋友和在线评论告诉我,学习mysqli有点浪费时间,因为PDO似乎更好(以及未来的情况),所以我尝试使用PDO。errr,如果
$stmt->fetch()
返回
false
,则
while
循环已经存在,因此,如果行不存在,您将永远不会输入
if
语句。请在
while
循环之前使用类似
if($row->rowCount()>0)
有什么特殊原因让您混合使用PDO和MySQLi吗?他说,他正在一个接一个地将其转换为PDO查询。我猜他不想一下子把它全弄坏。Madara Uchiha:我是PDO的新手,在学习了一点mysql之后,我(轻松地)学习了mysql。朋友和在线评论告诉我,学习mysqli有点浪费时间,因为PDO似乎更好(以及未来),所以我尝试使用PDO代替。我多次尝试使用row_count(以及count(*),但它对我不起作用。错误是“对非对象调用成员函数rowCount()。我了解到rowCount并不总是适用于SELECT查询。非常感谢您的回答,您知道为什么它将自己标识为非对象吗?我以为数组是对象。@SamBowyer“对非对象的调用”通常意味着在某个地方还有另一个问题。不过,我明白你的意思。给我一点时间。@Geoff_Montee-这是我的错。重新排列的代码缺少}。我不确定为什么会出现这个错误,但代码现在可以工作了。非常感谢——和其他答案一样,这让我深入了解了PDO与mysqli的不同之处(几天前才开始学习PDO意味着我还没有学会这一点)。谢谢@桑波耶太棒了!很乐意帮忙。通过切换到PDO,您正朝着正确的方向前进。如何使用
$rows=$stmt->fetchAll(PDO::FETCH_ASSOC);如果(count($rows)==0).
也应该可以。我多次尝试使用row\u count(以及count(*),但都不适用于我。错误是“对非对象调用成员函数rowCount()。我了解到rowCount并不总是适用于SELECT查询。非常感谢您的回答,您知道为什么它将自己标识为非对象吗?我以为数组是对象。@SamBowyer“对非对象的调用”通常意味着在某个地方还有另一个问题。不过,我明白你的意思。给我一点时间。@Geoff_Montee-这是我的错。重新排列的代码缺少}。我不确定为什么会出现这个错误,但代码现在可以工作了。非常感谢——和其他答案一样,这让我深入了解了PDO与mysqli的不同之处(几天前才开始学习PDO意味着我还没有学会这一点)。谢谢@桑波耶太棒了!很乐意帮忙。通过切换到PDO,您正朝着正确的方向前进。如何使用
$rows=$stmt->fetchAll(PDO::FETCH_ASSOC);如果(count($rows)==0).
也应该可以。到您的要点:通过列名选择
if (!($stmt = $pdo->prepare("SELECT * FROM table WHERE email = ?"))) {
   //error
}

if (!$stmt->execute(array("$email"))) {
    //error
}
//The $row3 var you had was useless. Deleted that.

$count = 0;

while ($row = $stmt->fetch()) {
    $count++;
}

//The query returned 0 rows, so you know the email doesn't exist in the DB
if ($count== 0) {

    $query = "INSERT INTO table (username, email, password, join_date) VALUES ('$username', '$email', SHA('$password1'), NOW())";

    if (!mysqli_query($dbc, $query)) {
        //error
    }

    $query = "SELECT * FROM table WHERE username = '$username'";

    if (!($data2 = mysqli_query($dbc, $query))) {
        //error
    }

    while ($row = mysqli_fetch_array($data2)) {

        $recipent = '' . $row['user_id'] . '';

        $query = "INSERT INTO messages (recipent, MsgTit, MsgR, MsgA, sender, time, readb, reada, MsgCon) VALUES ('$recipent', '$MsgTit', '$MsgR', '$MsgA', '$sender', NOW(), '$readb', '$reada', '$MsgCon')";

        if (!mysqli_query($dbc, $query)) {
            //error
        }

       // Aftermath.
       echo '<p>Your new account has been successfully created. You\'re now ready to <a href="game2.php" target="_blank">log in</a>. After this you should implement basic character-details on your users profile to begin the game.</p>';

       mysqli_close($dbc);
       exit();
   }
}
//The query did not return 0 rows, so it does exist in the DB
else {
    // An account already exists for this email, so display an error message
    echo '<p class="error">An account already exists for this e-mail.</p>';
    $email = "";
}