Hi folks,
Back after a long gap!
Got a rather complex MySQL query which I'm trying to get working, however I am being given the rather tedious Invalid Result Resource error message.
Some information on situation:
It's basically a searchable directory. One of the searches is by area. I have 3 select boxes where the user can select any multiple of Post code areas, towns or counties from the selection given. These are passed back through arrays and parsed into various variables. A string is created as the data is parsed, which is appended to a normal MySQL query.
Example String:
postcode LIKE '%GU1%' OR '%GU2%' OR '%GU9%' OR '%GU11%' OR '%GU14%' OR areacovered LIKE '%Aldershot%' OR '%Beaconsfield%' OR '%Bracknell%' OR '%Burnham%' OR '%Camberley%' OR county LIKE '%Berks%' OR '%Bucks%' OR '%Hants%' OR
Example Query:
"SELECT * FROM users WHERE postcode LIKE '%GU1%' OR '%GU2%' OR '%GU9%' OR '%GU11%' OR '%GU14%' OR areacovered LIKE '%Aldershot%' OR '%Beaconsfield%' OR '%Bracknell%' OR '%Burnham%' OR '%Camberley%' OR county LIKE '%Berks%' OR '%Bucks%' OR '%Hants%' OR"
The error message given:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/xxxxxxx/public_html/search.php on line 284
The relevant section of PHP code which parses (and calls) the MySQL query:
<?php elseif(isset($_POST['sub_
// If the directory was searched for an area, do this
// First store the variables from the POST
$postcode = $_POST['postcode'];
$town = $_POST['town'];
$county = $_POST['county'];
// Series of iterations to determine what is displayed as the 'you searched for'
if(isset($_POST['postcode'])){
$n = count($_POST['postcode']);
$searchdisplay = ($searchdisplay . "Postcode(s): " . implode(", ",$postcode) . ", ");
$pquery = (" postcode LIKE ");
for($i=0; $i < $n; $i++){
${$postcode.$i} = $postcode[$i];
$pquery = $pquery . "'%" . $postcode[$i] . "%' OR ";
echo $pquery . "<br>";
}
}
$searchdisplay = ($searchdisplay . " ");
if(isset($_POST['town'])){
$n = count($_POST['town']);
$searchdisplay = ($searchdisplay . "Town(s): " . implode(", ",$town) . ", ");
$pquery = $pquery . " areacovered LIKE ";
for($i=0; $i < $n; $i++){
${$town.$i} = $town[$i];
$pquery = $pquery . "'%" . $town[$i] . "%' OR ";
echo $pquery . "<br>";
}
}
$searchdisplay = ($searchdisplay . " ");
if(isset($_POST['county'])){
$n = count($_POST['county']);
$searchdisplay = ($searchdisplay . "County(ies): " . implode(", ",$county) . ", ");
$pquery = $pquery . " county LIKE ";
for($i=0; $i < $n; $i++){
${$county.$i} = $county[$i];
$pquery = $pquery . "'%" . $county[$i] . "%' OR ";
echo $pquery . "<br>";
}
}
?>
<tr><td> <i>You searched by area for <strong><?php echo $searchdisplay; ?></strong>here are your results:<br>
Please remember to scroll down to see all results for your search.</i><p></td></tr>
<?php
$name = "mind";
// Now we have the area to search, we build the query, execute the query and then display the results, after they have been formatted
global $conn;
$q = "SELECT * FROM users WHERE " . $pquery;
$result = mysql_query($q,$conn);
while($row = mysql_fetch_array($result))
{
Example output of $searchdisplay variable:
You searched by area for Postcode(s): GU1, GU2, GU9, GU11, GU14, Town(s): Aldershot, Beaconsfield, Bracknell, Burnham, Camberley, County(ies): Berks, Bucks, Hants, here are your results:
Please remember to scroll down to see all results for your search.
That variable displays correctly, so I know the parsing works correctly, also, when I echo out $pquery at each stage, the output looks just as it should (let me know if you want to check it please).
Thats all the information I can think of that might help right now.
Basically, anyone got any ideas if the query I'm using is valid? And if so, why it isnt working?
Many thanks
James