Mysql SELECT 查询确实有效,现在 2 分钟后没有(没有更改) [英] Mysql SELECT query did work, now doesn't 2 minutes later (without changes)

查看:45
本文介绍了Mysql SELECT 查询确实有效,现在 2 分钟后没有(没有更改)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了一些奇怪的问题,我的代码运行得很糟糕,运行一秒钟然后就无法运行了!!

I've been having weird problems lately, my code is playing up badly, working one second and then not working!!

我有这个查询

$stuff_get = mysqli_query($con,"SELECT users.id, users.email, users.hash, users.username FROM users WHERE users.username = '$username' AND users.id=$id4");
$gotstuff = mysqli_fetch_array($stuff_get, MYSQL_ASSOC);

提供此错误消息

 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given

通常这意味着它通常不起作用,但它实际上提前 2 分钟起作用(没有任何更改).

Usually that means it just generally doesn't work but it worked literally 2 minutes beforehand (without any changes).

这是完整的代码

<?php
session_start();
$username = $_SESSION['userlogin'];
include("connect.php");


$id_get = mysqli_query($con,"SELECT units.id, users.id, users.username FROM units, users WHERE       users.username = '$username' AND units.id = users.id");
$gotid = mysqli_fetch_array($id_get);
$id4 = $gotid["id"];

$stuff_get = mysqli_query($con,"SELECT users.id, users.email, users.hash, users.username FROM users WHERE users.username = '$username' AND users.id=$id4");
$gotstuff = mysqli_fetch_array($stuff_get, MYSQL_ASSOC);
$hash2 = $gotstuff['hash'];
$email2 = $gotstuff['email'];
$username2 = $gotstuff['username'];
echo "Your username is ",$username2,"<br>";
echo "Your activation code is ",$hash2,"<br>";

?>

它应该是我在注册时发送的电子邮件的一些内容,它在我第一次测试注册时有效,但此后我在相同的条件下又测试了 5 个,但没有成功!

It is supposed to be some of the contents of an email I send upon registration, it worked the first time I tested registration but I've since tested 5 more in the same conditions to no luck!

编辑

现在可以使用了

我不知道怎么做,我什么都没改变.我认为这与会议有关.

I have no idea how, I didn't change a thing. I think it was to do with the sessions.

推荐答案

加载结果后,必须检查是否成功.因为总是有可能服务器已经消失,或者没有可用的免费连接.

After loading the result, you must check if it was successful. Because there is always a possibility that the server has gone away, or there are no free connections available.

如果是这种情况,则 mysqli_query 将返回 false,这似乎是您遇到的行为.

If this is the case, then mysqli_query will return false, which appears to be the behaviour you're experiencing.

$username = mysqli_real_escape_string($username);
$id4 = mysqli_real_escape_string($id4);

$stuff_get = mysqli_query($con,
    "SELECT users.id, users.email, users.hash, users.username
     FROM users
     WHERE 
         users.username = '$username' 
         AND users.id=$id4");

if ($stuff_get)
{
    $gotstuff = mysqli_fetch_array($stuff_get, MYSQL_ASSOC);
}
else
{
    throw new \Exception('Could not retrieve result.');
}

(注意:我在运行查询之前转义了您的参数……只是为了确保有人复制/粘贴代码时不会忘记它.顺便说一句,您可以考虑切换到准备好的语句……更容易使用,更安全.)

(NOTE: I'm escaping your parameters before running the query … just to be sure it's not forgotten when somebody copy/pastes the code. Btw, you could consider switching to prepared statements … much easier to use, and much safer.)

这篇关于Mysql SELECT 查询确实有效,现在 2 分钟后没有(没有更改)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆