PHP 7 - MySQL错误mysqli_query()参数

时间:2017-04-10 18:01:30

标签: php mysql mysqli

您好我已将此代码编码为使用PHP 5,这是我几年前做的,我正在尝试使用PHP 7,这是我当前的网站版本,但我一直收到此错误而不是知道如何修复它,我试图将它全部改为它在PHP网站上所说的内容但是自从我用PHP编码以来已经很长时间了所以所有的帮助都非常感激。感谢。

这是我的错误:

  

警告:mysqli_query()需要至少2个参数,第20行的send_url.php中给出1个警告:mysqli_fetch_assoc()期望参数1为资源,在第22行的send_url.php中给出null,不再下载

这是我的代码:

// Create connection
$connect = new mysqli($host, $username, $password, $database);

// Check connection
if ($connect->connect_error) {
die("Connection failed: " . $connect->connect_error);
} 
echo "Connected successfully";

$q = $_GET[q];
if (!$q)
{
$q = "0";
}

$query = "SELECT * FROM links WHERE link = '$q'";
$result = mysqli_query($query);

$row=mysqli_fetch_assoc($result);

$filepath = $row["getfilename"];
$dltimes = $row["dltimes"];
$minusone = $dltimes-1;

if ($dltimes>0)
{

$location = 'myfiles/'. $filepath;

$changequery = "UPDATE links SET dltimes = '$minusone' WHERE link = '$q'";
$changeresult = mysqli_query($changequery);

       $filename = 'dummy.zip';
       $filename = realpath($location);

       $file_extension = strtolower(substr(strrchr($filename,"."),1));

       switch ($file_extension) {
           case "pdf": $ctype="application/pdf"; break;
           case "exe": $ctype="application/octet-stream"; break;
           case "zip": $ctype="application/zip"; break;
           case "doc": $ctype="application/msword"; break;
           case "xls": $ctype="application/vnd.ms-excel"; break;
           case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
           case "gif": $ctype="image/gif"; break;
           case "png": $ctype="image/png"; break;
           case "jpe": case "jpeg":
           case "jpg": $ctype="image/jpg"; break;
           default: $ctype="application/force-download";
       }

       if (!file_exists($filename)) {
           die("NO FILE HERE");
       }

       header("Pragma: public");
       header("Expires: 0");
       header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
       header("Cache-Control: private",false);
       header("Content-Type: $ctype");


       header("Content-Disposition: attachment; filename=\"".$filepath. "\";");

       header("Content-Transfer-Encoding: binary");
       header("Content-Length: ".@filesize($filename));
       set_time_limit(0);
       @readfile("$filename") or die("File not found.");

}
else
{
echo "No more downloads";
}

1 个答案:

答案 0 :(得分:2)

您需要修复代码,因为php在错误消息中说:

$connection = mysqli_connect($host, $username, $password, $database);

$result = mysqli_query($connection, $query);

然后,$result将不会是null,因此您可以抓取它:

$row=mysqli_fetch_assoc($result);

Aslo,使用MySQLi时最好使用objet-oriented style,例如:

$mysqli = new mysqli($hostname, $username, $password, $database);

$result = $mysqli->query($query);

while ($row = $result->fetch_assoc()) {
     $filepath = $row["getfilename"];
     $dltimes = $row["dltimes"];

     // rest of your code
}