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"; } } Quote 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] Quote 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()); Quote 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. Quote Link to comment https://forums.phpfreaks.com/topic/84743-solved-what-is-missing/#findComment-431856 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.