refiking Posted January 6, 2008 Share Posted January 6, 2008 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 More sharing options...
rab Posted January 6, 2008 Share Posted January 6, 2008 <?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] Link to comment https://forums.phpfreaks.com/topic/84743-solved-what-is-missing/#findComment-431850 Share on other sites More sharing options...
wildteen88 Posted January 6, 2008 Share Posted January 6, 2008 Rather than do: if( !$sql ) die(mysql_error()); Just add an or die clause after mysql_query instead: $sql = mysql_query("SELECT upfc FROM Loans WHERE loan_id = '$loan_id'") or die('Query error: ' . mysql_error()); Link to comment https://forums.phpfreaks.com/topic/84743-solved-what-is-missing/#findComment-431855 Share on other sites More sharing options...
trq Posted January 6, 2008 Share Posted January 6, 2008 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. Link to comment https://forums.phpfreaks.com/topic/84743-solved-what-is-missing/#findComment-431856 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.