greencoin Posted June 28, 2007 Share Posted June 28, 2007 Here's what I get "Query failed Resource id #2 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #2' at line 1" here's the code; <?php // db connect stuff $name2 = $_POST['state']; for($name2array=0; $name2array < sizeof($name2); $name2array++) { if($name2array < (sizeof($name2)-1)) { $name2_cond = " OR "; } else { $name2_cond = ""; } $name2q = $name2q."\"area\" LIKE'".$name2[$name2array]."'$name2_cond"; } $name2q = "($name2q)"; $query = "SELECT * FROM GC_Tracker WHERE $name2q"; die($query); $sql = mysql_query($query) or die(mysql_error()); if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { $i = 0; echo "<TABLE width=\"850\" cellpadding=\"3\" cellspacing=\"1\" bgcolor=cccccc>\n"; echo "<TR bgcolor=\"ccffcc\"><TD>ID</TD><TD width=130>Customer</TD><TD width=75>Area</TD><TD width=75>City</TD><TD width=100>Phone</TD><TD width=75>Amount</TD><TD>Units</TD></TR>\n"; while ($row = mysql_fetch_array($result)) { if ($i % 2) { echo "<TR bgcolor=\"ccffcc\">\n"; } else { echo "<TR bgcolor=\"white\">\n"; } echo "<TD>".$row['cid']."</TD><TD>".$row['customer']."</TD><TD>".$row['area']."</TD><TD>".$row['city']."</TD> <TD>".$row['phone']."</TD><TD>".$row['amount']."</TD> <TD>".$row['prod1'].", ".$row['prod2'].", ".$row['prod3'].", ".$row['prod4'].", ".$row['prod5'].", ".$row['prod6'].", ".$row['prod7'].", ".$row['prod8'].", ".$row['prod9'].", ".$row['prod10']."</TD>\n"; echo "</TR>\n"; $i++; } echo "</TABLE>\n"; echo "<br>\n"; echo "<input type=Button name=printit value=print onclick=javascript:window.print();>\n"; } else { echo "No results found"; } } else { echo "Query failed<br />" . $sql . "<br />" . mysql_error(); } // } else { // echo "form not submitted"; // } ?> The variables in the query are passing correctly. Using die($query) the string looks like; SELECT * FROM GC_Tracker WHERE ("area" LIKE'ak') Other than there is no space in between LIKE and the variable 'ak', I don't see anything wrong with the SELECT line. Anyone? Thanks in advance ~Rich Link to comment https://forums.phpfreaks.com/topic/57565-solved-modified-query-failed-resource-id-2-error-in-syntax-officially-lost/ Share on other sites More sharing options...
bbaker Posted June 28, 2007 Share Posted June 28, 2007 <?php $name2 = $_POST['state']; for($name2array=0; $name2array < sizeof($name2); $name2array++) { if($name2array < (sizeof($name2)-1)) { $name2_cond = " OR "; } else { $name2_cond = ""; } //$name2q = $name2q."/"area/" LIKE'".$name2[$name2array]."'$name2_cond"; // this is my variable that is on the SELECT line $name2q = $name2q."area LIKE '".$name2[$name2array]."'$name2_cond"; // this is my variable that is on the SELECT line } //$name2q = "($name2q)"; $query = "SELECT * FROM GC_Tracker WHERE $name2q"; // I think this is considered line 1 $sql = mysql_query($query) or die(mysql_error());?> should output as: SELECT * FROM GC_Tracker WHERE area LIKE 'ak' however....if you're looking for field containing 'ak' you'll want to change it to SELECT * FROM GC_Tracker WHERE area LIKE '%ak%' Link to comment https://forums.phpfreaks.com/topic/57565-solved-modified-query-failed-resource-id-2-error-in-syntax-officially-lost/#findComment-284918 Share on other sites More sharing options...
greencoin Posted June 28, 2007 Author Share Posted June 28, 2007 bbaker: I tried your correction and die($query) gave me; SELECT * FROM GC_Tracker WHERE When I uncommented $name2q = "($name2q)"; it gave me; SELECT * FROM GC_Tacker WHERE () I DO need to find a field containing the variable so how would I pass the '%...%' to the SELECT statement? I forgot to mention the form that passes the $name2 variable is a multiple checkbox form that will pass more than one variable at a time. Thanks ~Rich Link to comment https://forums.phpfreaks.com/topic/57565-solved-modified-query-failed-resource-id-2-error-in-syntax-officially-lost/#findComment-284931 Share on other sites More sharing options...
greencoin Posted June 28, 2007 Author Share Posted June 28, 2007 Trying to debug, I changed the code after finding some other people who posted the same errors. Now I have this code; <?php $name2 = $_POST['state']; for($name2array=0; $name2array < sizeof($name2); $name2array++) { if($name2array < (sizeof($name2)-1)) { $name2_cond = " OR "; } else { $name2_cond = ""; } $name2q = $name2q."\"area\" LIKE '%".$name2[$name2array]."%' $name2_cond"; } $name2q = "($name2q)"; $query = "SELECT * FROM GC_Tracker WHERE $name2q"; $sql = mysql_query($query) or die(mysql_error()); Print "<table width=860 border=1 cellpadding=0 cellspacing=0 class=table_results>"; Print "<tr bgcolor=ccffcc>"; Print "<td width=35>Order</td>"; Print "<td width=100>Customer</td>"; Print "<td width=75>Phone</td>"; Print "<td width=50>Area</td>"; Print "<td width=75>City</td>"; Print "<td width=70>Amount</td>"; Print "<td width=100>Products</td>"; Print "<td width=20>Created On</td></tr>"; while($info = mysql_fetch_array( $sql )) { Print "<tr bgcolor=ffffff>"; Print "<td>".$info['cid'] . "</td> "; Print "<td>".$info['customer'] . "</td> "; Print "<td>".$info['phone'] . " </td>"; Print "<td>".$info['area'] . " </td>"; Print "<td>".$info['city'] . "</td> "; Print "<td>".$info['amount'] . " </td>"; Print "<td>".$info['prod1']. " </td>"; $var = $info['time']; list($date,$time) = explode(' ',$var); Print "<td>".$date . " </td></tr>"; } echo $sql; // for debugging only to check my variable Print "</table>"; Print "<br>\n"; Print "<input type=Button name=printit value=print onclick=javascript:window.print();>\n"; ?> It no longer kicks out an error but the beginings of a table, with blank rows of course. I echoed $sql and it returns "Resource #2". die ($query) gives me; SELECT * FROM GC_Tracker WHERE ("area" LIKE '%LA%' ) I'm using MySQL 4.1 on godaddy servers... ~Rich Link to comment https://forums.phpfreaks.com/topic/57565-solved-modified-query-failed-resource-id-2-error-in-syntax-officially-lost/#findComment-284989 Share on other sites More sharing options...
greencoin Posted June 28, 2007 Author Share Posted June 28, 2007 ok - modified the code and now getting a different error. will close this and open another thread based on the new problem. Thanks for the help as always. ~Rich Link to comment https://forums.phpfreaks.com/topic/57565-solved-modified-query-failed-resource-id-2-error-in-syntax-officially-lost/#findComment-285062 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.