PHP网站在localhost上工作正常,但是当我上传到服务器时却没有

时间:2013-11-16 00:50:01

标签: php mysql mysqli

所以我编写了我的第一个使用mySQL数据库的php网站。它在我的localhost上完全正常,但是当我通过Filezilla将它上传到服务器时,它没有。

是不是因为我在一个案例中仍然指向localhost。如果是这样,我应该指向什么呢?有什么东西我完全不见了吗?关于将mySQL数据库导出到服务器的事情?任何想法,建议都将受到超级赞赏!

<?php

function getConnected($host,$user,$pass,$db) {

   $db = new mysqli($host, $user, $pass, $db);

   return $db;
}

$db = getConnected('localhost','root','*********','msg_app');

?>

我收到了这些错误:

Warning: mysqli::mysqli() [mysqli.mysqli]: (28000/1045): Access denied for user 'root'@'localhost' (using password: YES) in /home/hjaramil/public_html/tell_me/db/connect.php on line 5

Warning: mysqli::prepare() [mysqli.prepare]: Couldn't fetch mysqli in /home/hjaramil/public_html/tell_me/index.php on line 33

Fatal error: Call to a member function bind_param() on a non-object in /home/hjaramil/public_html/tell_me/index.php on line 34

这是我的代码:

<!DOCTYPE html>
<html class="no-js" xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>A Message For You</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link href="css/application.css" rel="stylesheet">
  <link rel="shortcut icon" type="image/x-icon" href="images/favicon.ico">
  <link href="//netdna.bootstrapcdn.com/font-awesome/3.1.1/css/font-awesome.css" rel="stylesheet">
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<link href='http://fonts.googleapis.com/css?family=Lusitana|Open+Sans|Varela+Round|Quattrocento+Sans' rel='stylesheet' type='text/css'>
</head>


<?php 
//error_reporting(0);
require 'db/connect.php'; 
require 'functions/security.php'; 
error_reporting(E_ALL);

$records = array();

if(!empty($_POST)){


  if(isset($_POST['first_name'],$_POST['location'],$_POST['message'])) {

    $first_name = trim($_POST['first_name']);
    $location   = trim($_POST['location']);
    $message    = trim($_POST['message']);

    if(!empty($first_name) && !empty($location) && !empty($message)) {
      $insert = $db -> prepare("INSERT INTO message (first_name, location, message, date) VALUES (?,?,?,NOW())");
      $insert->bind_param('sss',$first_name,$location,$message);

      if($insert -> execute()){
        header ('Location: index.php');
        die();
      }
    }

  }
}


if($results = $db->query("SELECT * FROM message ORDER BY id DESC LIMIT 1")) {
  if($results->num_rows) {
    while($row = $results->fetch_object()) {
      $records[] = $row;
    }
    $results -> free();
  }
}

?>



    <?php
      if (!count($records)){
        echo 'No messages';
      } else{

    ?>
      <?php
      foreach($records as $r){
      ?>


<body>
<div class="msg-container">
  <div class="msg-intro">
   <p> I wanted to tell you that:</p>
  </div>
  <div class="msg-content">
    <p><?php echo escape($r->message); ?></p>
  </div>
  <div class="msg-who">
    <p class="name"><?php echo escape($r->first_name); ?></p>
    <p class="location"><?php echo escape($r->location); ?></p>
    <p class="date"><?php echo escape($r->date); ?></p>
  </div>


    <?php
      }}
    ?>

</div>
<div class="form-background">
  <div class="form-container">
  <div class="form-title">
    <p>What do you want to tell?</p>
  </div>
    <form action="index.php" method="post">
          <div class="field msg">
            <!--<label for="message">Message</label>-->
            <textarea name="message" id="message" autocomplete="off"></textarea>
          </div>
          <div class="fields">
          <ul>
            <li> <label for="first_name">Name</label> </li>
            <li> <input type="text" class="s-input" name="first_name" id="first_name" autocomplete="off"> </li>
          </ul>
          </div>
          <div class="fields last">
            <ul>
              <li><label for="location">Location</label></li>
              <li><input type="text" class="s-input" name="location" id="location" autocomplete="off"></li>
            </ul>
          </div>
          <input type="submit" value="Post" id="submit-button">
      </form>
  <div>
</div>






</body>
<script type="text/javascript" src="js/application.js"></script>
</html>

2 个答案:

答案 0 :(得分:1)

您的问题显然在MySQL凭证中:

  

警告:mysqli :: mysqli()[mysqli.mysqli] :( 28000/1045):访问被拒绝    用户'root'@'localhost'(使用密码:YES)in   第5行/home/hjaramil/public_html/tell_me/db/connect.php

您被拒绝使用localhost的用户名和root的{​​{1}}访问password MySQL服务器。 100%的人不应该在本地桌面之外的服务器上使用root。我怀疑您使用的远程服务器允许root访问。您需要在服务器上使用凭据和设备进行数据库设置。一个数据库。很有可能它将是localhost连接,但如果不是,则需要该信息。

总而言之,您需要以下内容:

  1. 数据库:远程服务器上的数据库。 DB的名称应该传递给您。
  2. 凭据:用户名&amp;密码对。
  3. 主机名:应为localhost,但可能不依赖于您的服务器设置。
  4. 获取这些内容,将其替换为您的脚本&amp;你应该好好去。

答案 1 :(得分:0)

您必须确保通过托管您网站的网络服务器实际设置数据库 - 您的托管服务网站中可能存在一些名为托管工具的选项,此选项将在此处。一旦你设置了它(你将不必执行任何类型的MySQL服务器下载),你使用为MySQL服务器提供的IP地址(几乎肯定会与你的网站的IP不同)作为主办。看来你的所有错误都归咎于此。您也可以在发布示例时更改用户名和密码 - 只是为了安全!

相关问题