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

时间:2015-07-23 11:24:38

标签: php mysql mysqli

我需要一些帮助来设置我的网站。 我无法连接到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 

2 个答案:

答案 0 :(得分:0)

您需要将连接变量传递给您的函数才能使用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];
}

答案 1 :(得分:0)

您需要将数据库连接传递给您的函数才能使用它:

function fetchinfo($link,$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];
}

然后在调用fetchinfo函数时,请确保传入连接变量。