dongyan1899 2015-12-04 16:13 采纳率: 100%
浏览 50
已采纳

mysql_query():拒绝访问用户''@ localhost'[重复]

This question already has an answer here:

I see there is alot of these similar questions, but I don't find anyone with similar code.

The script uploads files to the server, but I also want to insert the data about the file that has been uploaded. Files get uploaded as they should without errors, but the table in phpmyadmin stays empty.

Error in 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

The connection seem to be working as I get the output "Localhost via UNIX socket 127.0.0.1 via TCP/IP" instead of a mysql warning/error at the website.

The connection script (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 . "
";

    $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 . "
";
    ?>

The upload PHP script:

<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
     }
    }
    ?>

The error seem to occur at "mysql_query($sql);" at line 65 in the upload file.

</div>
  • 写回答

1条回答 默认 最新

  • doumi6685 2015-12-04 16:15
    关注

    You've created a database connection using mysqli. That isn't available to the obsolete mysql library. Use mysqli consistently.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。