2011-06-16 46 views
0

這是我的代碼:PHP MySQL的問題(if語句可是沒有效果)

if ((isset($_POST['vidcode'])) && (strlen(trim($_POST['vidcode'])) > 0)) {$vidcode = stripslashes(strip_tags($_POST['vidcode']));} else {$vidcode = 'Invalid URL';}; 
if ((isset($_POST['vidtitle'])) && (strlen(trim($_POST['vidtitle'])) > 0)) {$vidtitle = stripslashes(strip_tags($_POST['vidtitle']));} else {$vidtitle = 'No Title';}; 
$vidcode = str_replace('"', '', $vidcode);$vidcode = str_replace("'", "", $vidcode);$vidtitle = str_replace('"', '', $vidtitle);$vidtitle = str_replace("'", "", $vidtitle); 

$db_handle = mysql_connect($server, $user_name, $password);$db_found = mysql_select_db($database, $db_handle); 
$SQL = "SELECT status FROM youtube2mp3 WHERE videocode = '$vidcode' ";$result = mysql_query($SQL); [BUGFIX:Added]$row = mysql_fetch_assoc($result);[/BUGFIX] 


if(mysql_num_rows($result) != false){ 


     // Add to DB & Set Status 
     $SQL = "UPDATE youtube2mp3 SET status='Download Complete' WHERE videocode='$vidcode'"; 
     $result = mysql_query($SQL); 

        [BUGFIX:Removed] 
     // Get Data into variable 
     $row = mysql_fetch_assoc($result); 
        [/BUGFIX] 

     // Check if its been processed 
     if (strcasecmp($row['status'], "Done") != 0){ 

     // Add to DB & Set Status 
     $SQL = "UPDATE youtube2mp3 SET status='Initializing Conversion' WHERE videocode='$vidcode'"; 
     $result = mysql_query($SQL); 

     $filename = $vidcode.'.mp4'; 

     if (!file_exists($filename) && !filesize($filename) >= 10000) { 
      $SQL = "UPDATE youtube2mp3 SET status='Invalid' WHERE videocode='$vidcode'"; 
      $result = mysql_query($SQL); 
     } else { 
      $SQL = "UPDATE youtube2mp3 SET status='Converting' WHERE videocode='$vidcode'"; 
      $result = mysql_query($SQL); 

      //convert file 
      exec('ffmpeg -i '.escapeshellarg($vidcode).'.mp4 -ab 156 -f mp3 '.escapeshellarg($vidtitle).'.mp3 2>&1'); 

      $SQL = "UPDATE youtube2mp3 SET status='Zipping' WHERE videocode='$vidcode'"; 
      $result = mysql_query($SQL); 

      // Zip it up 
      exec('zip "zips/'.$vidcode.'.zip" "'.$vidtitle.'.mp3"'); 

      //delete files 
      //unlink($vidcode.'.mp4'); 
      unlink($vidtitle.'.mp3'); 

      $SQL = "UPDATE youtube2mp3 SET status='Done' WHERE videocode='$vidcode'"; 
      $result = mysql_query($SQL); 
     }; 
     }; 
     }; 
mysql_close($db_handle); 

右鍵僅供參考 - 這是我太傻了!我重新使用了$ result,這給出了意想不到的結果請參閱上面代碼中的[BUGFIX] ...

回答

1

不要使用SELECT *...,請在查詢中明確列出您的列。通過這種方式,通過查看代碼清楚您希望從數據庫中獲得哪些列。另外,如果事實證明你認爲存在的列不存在,你會在問題實際發生的階段出現錯誤 - 在數據檢索時,而不是在以後的代碼中,當你嘗試使用數據。

另請注意,您使用mysql_fetch_arraydocs)正在返回一個數字索引的列數組。對於關聯數組,使用mysql_fetch_assocdocs)。

$db_handle = mysql_connect($server, $user_name, $password); 
$db_found = mysql_select_db($database, $db_handle); 
$sql = ' 
     SELECT 
      `status`, 
      `some_other_field` 
     FROM 
      `table` 
     WHERE 
      `videocode` = "'.$vidcode.'"'; 
$result = mysql_query($sql, $db_handle) or die('Error while performing query: '.mysql_error($db_handle)); 

if (mysql_num_rows($result, $db_handle) < 1) { 
    // you didn't get any rows back... 
} 

if(mysql_num_rows($result) != false){ 
    // Get Data into variable 
    $row = mysql_fetch_assoc($result, $db_handle); 

    // Check if its been processed 
    if ($row['status'] != "Done"){ 
     // CODE HERE IS STILL GETTING EXECUTED EVEN WHEN $row['status'] IS "Done" 

    } 
} 

使用此代碼,如果你試圖選擇不存在的列,那麼你會得到一個數據庫錯誤。

+1

mysql_fetch_array默認MYSQL_BOTH作爲結果類型,因此將包括數值陣列以及締陣列。雖然不知道,似乎不是問題。 – Suroot 2011-06-16 22:04:20

0

您正在使用mysql_fetch_array,它將該行作爲0索引數組返回。 您需要使用mysql_fetch_assoc

試試這個:

if ((isset($_POST['vidcode'])) && (strlen(trim($_POST['vidcode'])) > 0)) { 
    $vidcode = stripslashes(strip_tags($_POST['vidcode'])); 
} else { 
    $vidcode = 'Invalid URL'; 
}; 

if ((isset($_POST['vidtitle'])) && (strlen(trim($_POST['vidtitle'])) > 0)) { 
    $vidtitle = stripslashes(strip_tags($_POST['vidtitle'])); 
} else { 
    $vidtitle = 'No Title'; 
}; 

$vidcode = str_replace('"', '', $vidcode); 
$vidcode = str_replace("'", "", $vidcode); 
$vidtitle = str_replace('"', '', $vidtitle); 
$vidtitle = str_replace("'", "", $vidtitle); 

$db_handle = mysql_connect($server, $user_name, $password); 
$db_found = mysql_select_db($database, $db_handle); 
$SQL = "SELECT * FROM table WHERE videocode = '$vidcode' "; 
$result = mysql_query($SQL); 
if(mysql_num_rows($result) != false) { 
     // Get Data into variable 
     $row = mysql_fetch_assoc($result); 

     // Check if its been processed 
     if (strcasecmp($row['status'], "Done") != 0) 
     { 

     // CODE HERE IS STILL GETTING EXECUTED EVEN WHEN $row['status'] IS "Done" 
     } 
    }; 
}; 
mysql_close($db_handle); 
+1

assoc/array沒有區別 – jtanner 2011-06-16 22:07:04

+0

(意思是在我的代碼中改變時,它沒有任何作用) – jtanner 2011-06-16 22:32:22

0

我還沒有仔細閱讀是什麼問題,一般您可以:

  1. 試試這個:

    使用error_reporting(E_ALL); ini_set('display_errors','1'); ini_set('log_errors',1); ini_set('error_log','error_log.txt');我的意思是xml的http響應,它不是真的,ajax沒有輸出,ajax會帶來所有php錯誤的輸出,如果有的話!如果你使用jQuery,我猜這有點類似於http響應。

  2. 當代碼在奇怪的地方執行時,解決方案很簡單,只需在每個if,every,every函數,構造函數中插入echo即可......只要你看到一塊代碼是活的,只關注那裏!

  3. 當你在一個非常特定的地方懷疑問題,而是把注意力集中在一大塊代碼上時(在你的情況下),那麼在大多數情況下你必須忘記所有其他的代碼,它不會有任何幫助。對不起,如果它太籠統了!

+0

「錯誤」是它跳過並執行代碼...... php沒有報告任何錯誤(我的報告是在) – jtanner 2011-06-16 22:07:44

0

也許嘗試這樣:

if(strcasecmp(trim($row['status']), "DONE") != 0) { 

或者嘗試在while循環

function validatePostValues($alt_response = 'Default', $post_value = NULL) { 
    if((isset($post_value)) && (strlen(trim($post_value)) > 0)) { 
     $return_value = stripslashes(strip_tags($post_value)); 

     $return_value = str_replace('"', '', $return_value); 
     $return_value = str_replace("'", "", $return_value); 
    } else { 
     $return_value = $alt_response; 
    } 
    return $return_value; 
} 
$vidcode = validatePostValues('Invalid URL', $_POST['vidcode']); 
$vidtitle = validatePostValues('No Title', $_POST['vidtitle']); 

$db_handle = mysql_connect($server, $user_name, $password); 
$db_found = mysql_select_db($database, $db_handle); 
$SQL  = "SELECT * FROM table WHERE videocode = '$vidcode' "; 
$result = mysql_query($SQL); 

if(mysql_num_rows($result)) { 
    // Loop through the results 
    while($row = mysql_fetch_assoc($result)) { 
     // Added for debugging, enclose w/ PIPE for 
     // whitespace check 
     echo "Status is: |".$row['status']."|<br />\n"; 

     // Check if its been processed 
     if($row['status'] != "Done"){ 
      // CODE HERE IS STILL GETTING EXECUTED 
      // EVEN WHEN $row['status'] IS "Done" 
      echo "Row: ".print_r($row,true)."<br />\n"; 
     } 
    } 
} 
mysql_close($db_handle);