Jump to content

[SOLVED] What is missing?


refiking

Recommended Posts

What am I missing in this code?  It returns:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/refiking/public_html/test/uploader.php on line 6

| could not be uploaded!

 

$loan_id = $_GET['loan_id'];
$upload = $_GET['upload'];
$sql = mysql_query("SELECT upfc FROM Loans WHERE loan_id = $loan_id");
while($row = mysql_fetch_assoc($sql)) {
IF ($row[upfc] == 0){
$file_name = $loan_id . "point1";
}
ELSE {
$file_name = $loan_id . "point2";
}
}

Link to comment
https://forums.phpfreaks.com/topic/84743-solved-what-is-missing/
Share on other sites

<?php

$loan_id = (int)$_GET['loan_id'];
$sql = mysql_query("SELECT upfc FROM Loans WHERE loan_id = '$loan_id'");

if( !$sql ) 
    die(mysql_error());

while($row = mysql_fetch_assoc($sql)) {
    if( $row[upfc] == 0 )
        $file_name = $loan_id . "point1";
    else
        $file_name = $loan_id . "point2";
}

?>

 

Untested.[/code]

Your query is failing because of inavlid syntax and your failing to check your query succeeds before attempting to use its result.

 

<?php

 $loan_id = $_GET['loan_id'];
 $upload = $_GET['upload'];
 $sql = "SELECT upfc FROM Loans WHERE loan_id = '$loan_id'";
 if ($result = mysql_query($sql)) {
   if (mysql_num_rows($result)) {
     while ($row = mysql_fetch_assoc($sql)) {
       if ($row['upfc'] == 0) {
         $file_name = $loan_id . "point1";
       } else {
         $file_name = $loan_id . "point2";
       }
     }
   }
 } else {
    echo mysql_error();
  }

?>

 

Also, how many results are you expecting? do you need the loop? If so $file_name will only ever equal the result of the last record.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.