mysql_query():用户''@ localhost'拒绝访问

时间:2015-12-04 16:13:06

标签: php mysql mysqli phpmyadmin

我看到有很多这些类似的问题,但我找不到任何有相似代码的人。

脚本将文件上传到服务器,但我还想插入有关已上载文件的数据。文件上传应该没有错误,但phpmyadmin中的表保持为空。

error_log错误:

PHP Warning:  mysql_query(): Access denied for user ''@'localhost' (using password: NO) in *path*/upload.php on line 65
PHP Warning:  mysql_query(): A link to the server could not be established in *path*/upload.php on line 65

当我通过TCP / IP获取输出“Localhost via UNIX socket 127.0.0.1”而不是网站上的mysql警告/错误时,连接似乎正在工作。

连接脚本(uploadconnect.php):

<?php
$mysqli = new mysqli("localhost", "*username*", "*password*", "*database*");
    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }
    echo $mysqli->host_info . "\n";

    $mysqli = new mysqli("127.0.0.1", "*username*", "*password*", "*database*", 3306);
    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }

    echo $mysqli->host_info . "\n";
    ?>

上传PHP脚本:

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<button type="submit" name="btn-upload">upload</button>
</form>

    <?php
    include_once 'uploadconnect.php';
    if(isset($_POST['btn-upload']))
    {    

     $file = rand(1000,100000)."-".$_FILES['file']['name'];
        $file_loc = $_FILES['file']['tmp_name'];
     $file_size = $_FILES['file']['size'];
     $file_type = $_FILES['file']['type'];
     $folder="uploads/";

     $new_size = $file_size/1024;  

     $new_file_name = strtolower($file);

     $final_file=str_replace(' ','-',$new_file_name);

     if(move_uploaded_file($file_loc,$folder.$final_file))
     {
      $sql="INSERT INTO uploadedfiles(file,type,size) VALUES('$final_file','$file_type','$new_size')";
      mysql_query($sql);  #<--- LINE 65
      ?>
      <script>
      alert('successfully uploaded');
            window.location.href='upload.php?success';
            </script>
      <?php
     }
     else
     {
      ?>
      <script>
      alert('error while uploading file');
            window.location.href='upload.php?fail';
            </script>
      <?php
     }
    }
    ?>

错误似乎发生在“mysql_query($ sql);”在上传文件的第65行。

1 个答案:

答案 0 :(得分:5)

您已使用mysqli创建了数据库连接。 obsolete mysql库无法使用该功能。始终使用mysqli