jtanner
jtanner

Reputation: 21

PHP MySQL issue (if statement doesnt have effect)

This is my code:

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);

Right Just FYI - It was me being stupid! I reused $result which gave unexpected results. See [BUGFIX] in code above...

Upvotes: 0

Views: 139

Answers (4)

Phill Pafford
Phill Pafford

Reputation: 85308

Maybe try this:

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

Or try the while loop

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);

Upvotes: 0

Anze Jarni
Anze Jarni

Reputation: 1157

You are using mysql_fetch_array which returns the row as a 0 indexed array. You need to use mysql_fetch_assoc

Try this:

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);

Upvotes: 0

Melsi
Melsi

Reputation: 1462

I haven't read thoroughly what the problem is, in general you can:

  1. Try this:

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

  2. Try alert(response), I mean the the xml http response, it is not true that ajax has no output, ajax will bring the output exactly with all php errors if any! If you use jquery I guess there is something analogous to http response.

  3. When code is executed in strange places, the solution is trivial just put echo inside every if, every else, every function, constructor whatever... As soon you see a block of code is alive when was supposed not to be focus there only!

  4. When you suspect a problem in a very specific place but instead you focus in a big piece of code (as in your case) then in most case you have to forget all the rest of the code, it's not going to be of any help. Sorry if it is too general!

Upvotes: 0

Chris Baker
Chris Baker

Reputation: 50592

Don't use SELECT *..., explicitly list your columns in your queries. This way, it is clear what columns you expect to get from the database by looking at your code. Plus, if it turns out that a column you think exists does not, you'll get an error at the stage where the problem is actually happening - at the data retrieval, instead of later in your code when you're trying to use the data.

Also note, your use of mysql_fetch_array (docs) is returning a numerically-indexed array of columns. Use mysql_fetch_assoc (docs) for an associative array.

$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"

    }
}

Using this code, if the columns you try to select don't exist, then you'll get a database error.

Upvotes: 1

Related Questions