PHP MySQL问题(if语句没有效果)

时间:2011-06-16 21:41:07

标签: php mysql

这是我的代码:

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] ...

4 个答案:

答案 0 :(得分: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 :(得分: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);

答案 2 :(得分:0)

我没有仔细阅读问题所在,一般情况下你可以:

  1. 试试这个:

    的error_reporting(E_ALL); ini_set('display_errors','1'); ini_set('log_errors',1); ini_set('error_log','error_log.txt');

  2. 尝试提醒(响应),我的意思是xml http响应,ajax没有输出是不正确的,ajax会带来输出完全带有所有php错误(如果有的话)!如果你使用jquery,我猜有些东西类似于http响应。

  3. 当代码在陌生的地方执行时,解决方案很简单,只需将echo放在每个if,every else,every function,constructor中......很快就会看到一段代码是活着的只关注那里!

  4. 如果你怀疑某个特定地方有问题,而是你专注于一大段代码(如你的情况那样)那么在大多数情况下你必须忘记所有其余的代码,它不会去有任何帮助。对不起,如果太笼统了!

答案 3 :(得分: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);