2011-10-22 44 views
0
if ($area == Null || $area == "Area of City") { 
    $ment = "CREATE OR REPLACE VIEW water 
      AS SELECT postable.postid, description, dated, image, posterid, country, state, city, area, cat 
      FROM postable 
      INNER JOIN ".$_GET['Cat']." 
      ON postable.postid = ".$_GET['Cat'].".postid 
      WHERE ".$_GET["Cat"].".cat = '".$_GET["what"]."' 
      AND postable.country = '".$_GET["Country"]."' 
      AND postable.state = '".$_GET["State"]."' 
      AND postable.city = '".$_GET["City"]."'"; 
} 

$ment = "CREATE OR REPLACE VIEW water 
     AS SELECT postable.postid, description, dated, image, posterid, country, state, city, area, cat 
      FROM postable, ".$_GET["Cat"]." 
     WHERE (
      (postable.postid = ".$_GET["Cat"].".postid 
      AND ".$_GET["Cat"].".cat = '".$_GET["what"]."' 
      AND postable.country = '".$_GET["Country"]."' 
      AND postable.state = '".$_GET["State"]."' 
      AND postable.city = '".$_GET["City"]."' 
      AND postable.area = '".$area."') 
      OR 
      (postable.postid = ".$_GET["Cat"].".postid 
      AND ".$_GET["Cat"].".cat = '".$_GET["what"]."' 
      AND postable.country = '".$_GET["Country"]."' 
      AND postable.state = '".$_GET["State"]."' 
      AND postable.city = '".$_GET["City"]."') 
     )"; 
$tester = "SELECT * FROM water, userguy 
      WHERE userguy.posterid = water.posterid"; 

if (!mysql_query($ment, $con)) { 
    die('There are no posts matching your search, please enter another search in either another location or category, or both. '); 
} 

if (!mysql_query($tester,$con)) { 
    die('There are no posts matching your search, please enter another search in either another location or category, or both.'); 
} 

$result = mysql_query($tester,$con); 


while ($row = mysql_fetch_array($result)) { 
if ($row == Null) { 
    echo "<tr><td colspan'4'>There are no posts matching your search, please enter another search in either another location or category, or both.</td></tr>";} 
    echo "<tr><td colspan='4' class='sult'>"; 
    echo "<div id='main'>".$row['description']."</div> ".$row['dated']." <a  href='mai.php?tofield=".$row['email']."'><b>".$row['email']."</b></a><b>&nbsp".$row['mobile']."</b>"; 
    if ($row['image'] != Null) { 
    echo "<img src='upload/".$row['image']."' class='relt'/>"; 
    } 
    echo "</td></tr>"; 
} 

?> 

<tr> 
    <td colspan='4'> 
    <br/><br/><br/><br/> 
    </td> 
</tr> 
<tr> 
    <td colspan='2' align='center'> 
    <img src='first.jpg'/> 
    </td> 
    <td colspan='2' align='center'> 
    <img src='second.jpg'/> 
    </td> 
</tr> 
<tr> 
    <td colspan='2' align='center'> 
    <img src='high.jpg'/> 
    </td> 
    <td colspan='2'></td> 
</tr> 
</table> 

</body> 

好返回任何東西,現在,這個代碼工作在我的電腦,我用它來開發這個我的網站上,這是它從數據庫返回的行。但是,當我將這段代碼放到我的網站上時,它不會返回任何內容,實際上,腳本甚至不顯示任何內容。我會很感激,如果我可以得到任何幫助,它現在已經讓我不眠之夜太久了...PHP while循環不在服務器上

+2

你有相同的數據和日相同的mysql相同的登錄數據(主機,用戶,密碼)?你是否收到error_reporting的錯誤消息? – Anonymous

+0

哦,我的這段代碼非常需要一些正確的格式和縮進。我強烈建議花一些時間學習和實現Drupal編碼標準(http://drupal.org/coding-standards),或者簡單地做一個Google搜索「php編碼標準」,看看還有什麼。你的灰色細胞(以及你尋求幫助的人)會感謝你。 :) –

+0

@BrianShowalter在易讀性問題中重新分配代碼......並立即發現了幾個問題,同時這樣做... – DaveRandom

回答

0

我推測從$_GET用法,你在瀏覽器中運行這個,而不是從一個控制檯。這可能是因爲你的mysql擴展沒有加載,或者你沒有與數據庫服務器的有效連接。

  • 首先,檢查你的錯誤日誌。這些通常與您的Apache訪問日誌*保持在同一個地方。這會給你一個錯誤信息來處理。
  • 接下來,在您服務器上的空白頁面中運行<?php phpinfo(); ?>,並查看您的基於Web的PHP是否支持MySQL支持。
  • 嘗試運行mysql_error()後可能會失敗的數據庫操作。這也是挖掘錯誤的好方法。

最後要記住,你的腳本目前有SQL注入安全漏洞。修復這些probs之前,你去住這個:-)。

編輯:*您的服務器日誌的位置取決於您正在使用的操作系統,並且您沒有指定此位置。這很容易從搜索引擎中獲得,但是 - 如果您在Windows上,那麼請執行「Windows apache日誌位置」的網頁搜索。簡單;-)

編輯2:如果SSH已被禁用,並且您覺得您需要它,請與您的主機通話。然而,這是一個單獨的問題,你不需要它來解決手頭的問題。如果你的phpMyAdmin有問題,請重新安裝它,或使用你的主機提供的那個。 (如果您使用的是cPanel,則包含在您的控制面板中。)

+0

您需要使用反標符號讓StackOverflow識別內聯代碼。 – Alex

+0

@halfer如果你必須解釋你的代碼不應該看起來像這樣,你肯定有必要這樣做...... – DaveRandom

+0

好吧,現在當我使用phpmyadmin的時候,它不停地給我下面的消息; import.php:缺少參數:import_type(FAQ 2.8) import.php:缺少參數:格式(FAQ 2.8)。加上我的SSh終端已被我的服務提供商禁用,就像我的httpd配置文件,任何幫助將不勝感激。 – invoq

1

我看不到任何直接的原因,爲什麼您的腳本不應該運行,因爲halfer指出您的消息和錯誤日誌是最好的地方看。

順便說一句,雖然:在while循環的第一行if($row = Null)永遠不會爲真(如果它是,while循環將不進入) 考慮

$n_rows = 0; 
while($row = mysql_fetch_array($result)) { 
    $n_rows++; 
    // Process row 
} 
if(!$n_rows) { 
    // No rows message 
} 

(我知道這應該是評論不是一個答案,但我沒有點)

+0

歡迎,並有一些要點:) – halfer

+0

感謝球員,@halfer,在我上面介紹的代碼中的sql查詢工作在我的PC的服務器/數據庫和MySQL支持是由我的基於Web的PHP支持,因爲另一個PHP文件輸入數據到數據庫中,但是,如果你能指示我可以檢查我的錯誤日誌和apache訪問日誌,我將不勝感激。另一個有趣的事情是,PHP代碼之後的表格由於某種原因沒有顯示,我不明白這一點,但它可以在我的PC上運行,並且PHPmyadmin向我顯示錶格和其中的數據。 – invoq

1

試試這個(見註釋代碼更改):

<?php 

    if ($area == Null || $area == "Area of City") { 
    $ment = "CREATE OR REPLACE VIEW water 
      AS SELECT postable.postid, description, dated, image, posterid, country, state, city, area, cat 
       FROM postable 
      INNER JOIN ".$_GET['Cat']." 
       ON postable.postid = ".$_GET['Cat'].".postid 
      WHERE ".$_GET["Cat"].".cat = '".$_GET["what"]."' 
       AND postable.country = '".$_GET["Country"]."' 
       AND postable.state = '".$_GET["State"]."' 
       AND postable.city = '".$_GET["City"]."'"; 
    } else { 
    // Pretty sure this should be in an else block 
    // As it was, the query above would never be executed, because it would 
    // always be overwritten by this one 
    $ment = "CREATE OR REPLACE VIEW water 
      AS SELECT postable.postid, description, dated, image, posterid, country, state, city, area, cat 
       FROM postable, ".$_GET["Cat"]." 
      WHERE (
       (postable.postid = ".$_GET["Cat"].".postid 
       AND ".$_GET["Cat"].".cat = '".$_GET["what"]."' 
       AND postable.country = '".$_GET["Country"]."' 
       AND postable.state = '".$_GET["State"]."' 
       AND postable.city = '".$_GET["City"]."' 
       AND postable.area = '".$area."') 
       OR 
       (postable.postid = ".$_GET["Cat"].".postid 
       AND ".$_GET["Cat"].".cat = '".$_GET["what"]."' 
       AND postable.country = '".$_GET["Country"]."' 
       AND postable.state = '".$_GET["State"]."' 
       AND postable.city = '".$_GET["City"]."') 
      )"; 
    } 

    $tester = "SELECT * FROM water, userguy 
      WHERE userguy.posterid = water.posterid"; 

    /* 
    Do you really not want the result of this query? 
    Shouldn't you be catching the results in a variable? 
    If not, consider adding a LIMIT 1 clause to the query, as the 
    database will have to do more work to return a full result set 
    you never use... 
    Regardless of that, testing for !mysql_query doesn't tell you there 
    were no results, it tells you whether the query itself failed. You 
    test the number of results using mysql_num_rows() or a SELECT count() query 
    */ 
    if (mysql_num_rows(mysql_query($ment, $con)) < 1) { 
    die('There are no posts matching your search, please enter another search in either another location or category, or both. '); 
    } 

    // Same goes for this. 
    // However, since you definitely do want the results of this, why run it twice? 
    $result = mysql_query($tester,$con); 
    if (mysql_num_rows($result) < 1) { 
    die('There are no posts matching your search, please enter another search in either another location or category, or both.'); 
    } 

    // Use mysql_fetch_assoc() instead of mysql_fetch_array(), it's more efficient 
    while ($row = mysql_fetch_assoc($result)) { 
    // if ($row == Null) { 
    // Sorry, what? If the row is null?? 
    // It will never be null... if it were, the loop would break immediately 
    // and never reach this point, plus mysql_fetch_* never returns null! 
    // echo "<tr><td colspan'4'>There are no posts matching your search, please enter another search in either another location or category, or both.</td></tr>"; 
    // } 
    echo "<tr><td colspan='4' class='sult'>"; 
    echo "<div id='main'>".$row['description']."</div> ".$row['dated']." <a  href='mai.php?tofield=".$row['email']."'><b>".$row['email']."</b></a><b>&nbsp".$row['mobile']."</b>"; 
    if ($row['image'] != Null) { 
     echo "<img src='upload/".$row['image']."' class='relt'/>"; 
    } 
    echo "</td></tr>"; 
    } 

?> 

<tr> 
    <td colspan='4'> 
    <!-- 
     Really? You can't just use a CSS height? 
    --> 
    <br/><br/><br/><br/> 
    </td> 
</tr> 
<tr> 
    <td colspan='2' align='center'> 
    <img src='first.jpg'/> 
    </td> 
    <td colspan='2' align='center'> 
    <img src='second.jpg'/> 
    </td> 
</tr> 
<tr> 
    <td colspan='2' align='center'> 
    <img src='high.jpg'/> 
    </td> 
    <td colspan='2'></td> 
</tr> 
</table> 

</body> 
+0

徹底的答案,+1。雖然我會說這些屬性應該使用雙引號而不是單引號 - 儘管當然,用雙引號字符串指定它們是一種痛苦......;) – halfer