gpt4 book ai didi

PHP 警告:mysqli::query():无法获取 mysqli

转载 作者:行者123 更新时间:2023-11-29 07:10:03 29 4
gpt4 key购买 nike

这是我当前的代码:

<?php 
$key = $_REQUEST['key'];
$url = $_REQUEST['url'];

include_once '../../dbconnect.php';

$query = $conn->query("SELECT * FROM members WHERE apikey='$key' && status='Active'");
$userRow=$query->fetch_array();
$conn->close();

/// Verify the URL starts with http:// or https://
if (0 === strpos($url, 'http://') || 0 === strpos($url, 'https://')) {
$url = $url;
} else {
$url = "http://$url";
}

/// Verify the key is 32 characters
if (!preg_match('/[^A-Za-z0-9]/', $key) && (strlen($key) == 32)) {

/// Verify the URL isn't malicious
if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
die('Error: Invalid URL');
} else {

if ($userRow['status'] === 'Active') {

function generateRandomString($length = 8) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}

$redirect = generateRandomString();

$addshort = $conn->query("INSERT INTO short_".$redirect[0]." (redirect, apikey, url) VALUES ('".$redirect."','".$key."','".$url."')");

if ($conn->query($addshort) === TRUE) {
echo "added correctly";
} else {
echo "there was an error";
}

$conn->close();

} else {
echo "Error: Account Not Active";
}

}

} else {
die('Error: Invalid API Key');
}
?>

这是错误日志:

[18-Oct-2016 12:21:31 America/New_York] PHP Warning:  mysqli::query(): Couldn't fetch mysqli in /home/username/public_html/subdomains/url/index.php on line 40
[18-Oct-2016 12:21:31 America/New_York] PHP Warning: mysqli::query(): Empty query in /home/username/public_html/subdomains/url/index.php on line 42
[18-Oct-2016 12:21:31 America/New_York] PHP Warning: mysqli::close(): Couldn't fetch mysqli in /home/username/public_html/subdomains/url/index.php on line 48

你可以在第7行看到我第一次连接数据库的地方:

$query = $conn->query("SELECT * FROM members WHERE apikey='$key' && status='Active'");

那条线正在工作。但是,第二次连接执行 INSERT 时,我收到上述错误:

$addshort = $conn->query("INSERT INTO short_".$redirect[0]." (redirect, apikey, url) VALUES ('".$redirect."','".$key."','".$url."')");

我是否因为长时间盯着这段代码而错过了一些东西?

最佳答案

您的连接已关闭,在执行第一个 SELECT 语句后,这意味着连接提前关闭:

$conn->close();

您需要在所有查询或重新建立连接后使用close()。这是更好的选择。

您正在获取用户输入$_REQUEST['key'],这意味着您的查询对 SQL 注入(inject)开放,这将帮助您了解如何防止您的代码使用 SQL注入(inject):How can I prevent SQL injection in PHP?

关于PHP 警告:mysqli::query():无法获取 mysqli,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40113437/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com