gpt4 book ai didi

php - 无法连接 MYSQL,我做错了什么?

转载 作者:行者123 更新时间:2023-11-29 22:11:40 25 4
gpt4 key购买 nike

我需要一些帮助来设置我的网站。我无法连接MYSQL,请问我哪里做错了?

<?php
$sitename = "csgoprofit.dk";
$link = mysqli_connect("host", "name", "pass", "database") or die("Error " . mysqli_error($link));
$dbname = "u587432735_db";
$db_selected = mysqli_select_db($link, $dbname);
mysqli_set_charset("SET NAMES utf8");
if (mysqli_connect_errno());
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}

function fetchinfo($rowname,$tablename,$finder,$findervalue) {
if($finder == "1") $result = mysqli_query($link, "SELECT $rowname FROM $tablename");
else $result = mysqli_query($link, "SELECT $rowname FROM $tablename WHERE `$finder`='$findervalue'");
$row = mysqli_fetch_assoc($result);
return $row[$rowname];
}
?>

mysqli_query() expects parameter 1 to be mysqli, null given in <b>/home/u587432735/public_html/set.php</b> on line <b>10</b><br /> <br /> <b>Warning</b>: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in <b>/home/u587432735/public_html/set.php</b> on line <b>11</b><br /> <br /> <b>Warning</b>: mysqli_query() expects parameter 1 to be mysqli, null given in

最佳答案

您需要将连接变量传递给您的函数,为此您可以使用golbal

function fetchinfo($rowname, $tablename, $finder, $findervalue) {
global $link;
if ($finder == "1")
$result = mysqli_query($link, "SELECT $rowname FROM $tablename");
else
$result = mysqli_query($link, "SELECT $rowname FROM $tablename WHERE `$finder`='$findervalue'");
$row = mysqli_fetch_assoc($result);
return $row[$rowname];
}

关于php - 无法连接 MYSQL,我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31585953/

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