2015-09-19 68 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