2015-09-19 37 views
-1

我已经看过其他的问题,但看不到任何相同的东西,所以如果我错过了一个道歉。PHP的MySQL访问不在网站的根目录

我在开发它的子目录时创建了一个站点,然后将它移动到根目录,移动到根目录,通过php停止工作访问MySQL数据库。它停在的特定代码行是

$pdo=new PDO("mysql:dbname=NAME;host=127.0.0.1","USERNAME","PASSWORD"); 

只要它移动到根目录之外的一个目录中,脚本就会正常工作。任何想法可能是什么问题?

指数

<!DOCTYPE html> 
<html> 
<head> 
    <link href='http://fonts.googleapis.com/css?family=Raleway:600,500,400' rel='stylesheet' type='text/css'> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
    <link type="text/css" rel="stylesheet" href="/assets/css/ProtoStyle_1.css"/> 
    <?php 
     require_once('./php/autoloader.php'); 
     $feed = new SimplePie('http://mywebsite.eu/blog/feed'); 

     function returnImage ($text) { 
      $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8'); 
      $pattern = "/<img[^>]+\>/i"; 
      preg_match($pattern, $text, $matches); 
      $text = $matches[0]; 
      return $text; 
     } 


     function scrapeImage($text) { 
      $pattern = '/src=[\'"]?([^\'" >]+)[\'" >]/'; 
      preg_match($pattern, $text, $link); 
      $link = $link[1]; 
      $link = urldecode($link); 
      return $link; 
     } 
    ?> 
</head> 

<?php flush(); ?> 

<body onresize="setFontSize()" > 
    <div id="kvh_page"> 
     <div id="kvh_header"><?php include("nav-bar.html"); ?></div> 

     <div id="kvh_content"> 
      <div id="kvh_latestproject"> 
       <div class="kvh_screen" > 
        <div class="kvh_page1"> 
         <img src="/assets/images/balloons.png"/> 
         <h1>Current Project</h1> 
        </div> 
        <div class="kvh_page2"> 
         <img src="/assets/images/left-right.jpg"/> 
         <h1>Left/Right</h1> 
         <h2>Left/Right is a mobile app designed to push your reaction speed to the limit.</h2> 
         <a href="/projects/leftright"><button type="button">More</button></a> 
        </div> 
       </div>      
      </div> 

      <div id="kvh_latestblog"> 
       <div> 
        <div class="kvh_screen" > 
         <div class="kvh_page1"> 
          <img src="/assets/images/road.png"/> 
          <h1>Latest Blog Post</h1> 
         </div> 
         <?php $item = $feed->get_item(); 
            $Description = $item->get_description(); 
            $image = returnImage($Description); 
            $Description = str_replace($image, "", $Description); 
            $image = scrapeImage($image); ?>  

         <div class="kvh_page2" style="background-image: url('<?php print $image; ?>')"> 
          <span class="kvh_textmask"> 
           <h1><?php print $item->get_title(); ?></h1> 
           <h2><?php print $Description; ?></h2> 
           <?php $author = $item->get_author(); ?> 
           <h3>Posted on <?php print $item->get_date('j F, Y'); ?> by <strong><?php print $author->get_name(); ?></strong></h3> 
           <a href="<?php print $item->get_permalink(); ?>"><button type="button">Read more</button></a> 
          </span> 
         </div> 
        </div> 
       </div> 
      </div> 

      <div id="kvh_latestresource"> 
       <div> 
        <div class="kvh_screen" > 
         <div class="kvh_page1"> 
          <img src="/assets/images/rain.png"/> 
          <h1>Thing of the day</h1> 
         </div> 
         <?php include("gethomestuff.php"); ?> 
         <!--<div class="kvh_page2" style="background-image: url('/assets/images/MangaDice.png')"> 

          <span class="kvh_textmask"> 
           <h1>This is the name of the thing</h1> 
           <h2>- Category</h2> 
           <a href="#"><button type="button">Check it out</button></a> 
          </span> 
         </div>--> 
        </div>      
       </div> 
      </div> 
     </div> 
    </div> 
    <?php include("footer.php"); ?> 
    <script src="/assets/scripts/proto1.js"></script> 
</body> 
</html> 

gethomestuff

<?php 
    try { 
    $pdo=new PDO("mysql:dbname=NAME;host=127.0.0.1","USERNAME","PASSWORD"); 
    } catch (PDOException $e) { 
     echo 'Connection failed: ' . $e->getMessage(); 
    } 
    $statement=$pdo->prepare("SELECT * FROM DailyStuff limit 1"); 
    $statement->execute(); 
    $results=$statement->fetchAll(PDO::FETCH_ASSOC); 
    foreach($results as $row) 
    { 
     if(strlen($row['Image']) < 10) 
      echo '<div class="kvh_page2">'. PHP_EOL; 
     else echo '<div class="kvh_page2" style="background-image: url(\''. $row['Image'] . '\')">'. PHP_EOL ; 
     echo '<span class="kvh_textmask">'. PHP_EOL . '<h1>'. $row['Name'].'</h1>'. PHP_EOL . '<h2>- '. $row['Category'] .'</h2>' . PHP_EOL .'<a href="'.$row['Link']. '"><button type="button">Check it out</button></a>'. PHP_EOL .'</span>'. PHP_EOL .'</div>' . PHP_EOL; 
    } 
?> 
+0

单行代码是不够的。检查错误。 –

+0

该声明与脚本的位置没有任何关系,因此其他内容也是如此。此代码是否从使用数据库的脚本中的其他脚本文件中包含? – RiggsFolly

+0

该行代码是脚本的第一行。从根目录调用它时,它永远不会超过它。 这条线没有错误,如果这就是你的意思,因为它在其他地方工作正常,否则我不确定要检查什么错误或如何? – Kaevan

回答

0

您应经常检查到数据库的所有调用的结果,使该行的代码更改

try { 
    $pdo=new PDO("mysql:dbname=NAME;host=127.0.0.1","USERNAME","PASSWORD"); 
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

    echo 'Database connection OK'; 
} catch (PDOException $e) { 
    echo 'Connection failed: ' . $e->getMessage(); 
} 

这应该显示您尝试连接到数据库时生成的错误。

+0

应该在哪里显示这个错误?我已经更新了文件并尝试了两个index.php,并直接转到gethomestuff.php,但没有任何输出。 – Kaevan

+0

那么怎么办这个'index.php'告诉我你进入浏览器地址栏的内容 – RiggsFolly

+0

www.mywebsite.eu/index.php或者www.mywebsite.eu/gethomestuff.php – Kaevan