0

i need some assistance to setup my website. I cant connect to the MYSQL, and i would like to ask what i did wrong?

<?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 
Ushanka
  • 9
  • 2
  • Are you seeing errors? Are you showing them? – Fluffeh Jul 23 '15 at 11:26
  • Please show the error message! – Jens Jul 23 '15 at 11:27
  • My Website is just replying with "Failed to connect to MYSQL" – Ushanka Jul 23 '15 at 11:28
  • mysqli_query() expects parameter 1 to be mysqli, null given in /home/u587432735/public_html/set.php on line 10

    Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /home/u587432735/public_html/set.php on line 11

    Warning: mysqli_query() expects parameter 1 to be mysqli, null given in
    – Ushanka Jul 23 '15 at 11:30
  • Another thing to note, your query inside the `fetchinfo` function could be susceptible to SQL Injection. To solve this you should use prepared statements: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php – Jamesking56 Jul 23 '15 at 12:28
  • Just bumping this up, as i havent found a anwser yet, – Ushanka Jul 23 '15 at 18:40

2 Answers2

0

You need to pass your connection variable to your function for this you can use 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];
}
Saty
  • 22,443
  • 7
  • 33
  • 51
0

You need to pass your database connection into your function to use it like so:

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];
}

Then when calling the fetchinfo function make sure you pass in your connection variable.

Jamesking56
  • 3,683
  • 5
  • 30
  • 61