Hi,
I have spent some time on this and can't get to the bottom of it. I want to re-use my SQL query so decided to put it in a fucntion. The query works fine if i run it in the function "registered_table" which eventually displays the query in a table, but when I put the query in a seperate function I recieve a SQL syntax error.
function registered_table is used to display my data.
function search_field is the query
If I echo the query in the search_field funtion I can see the SQL Resource but when I return $dbResult to registered_table I recieve the SQL syntax error.
"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 'LIKE '%%' ORDER BY date DESC LIMIT ,' at line 1"
From this I understand that the SQL Resource is not returned but rather the query dbResult which is then executed but I am unsure why it doesn't work. I have also tried using set values in the query rather than my variables eg "SELECT * FROM registered_members WHERE name LIKE luke ORDER BY date DESC LIMIT 0, 5" but I recieve the "Unknown column 'luke' in 'where clause'" but again this works if it is in the registered_table function.
The basic outline of the code is below, I removed other parts to make it easier to identify the problem area. Any help on this will be greatly appreciated.
Thanks Luke
function registered_table() {
$conn = db_connect();
$recordperPage = 5;
if (isset($_GET['page'])) {
$page = $_GET['page'];
}
else {
$page=1;
}
$startFrom = ($page-1) * $recordperPage;
if (isset($_POST['searchField'])){
$searchCat = addslashes($_POST['searchCat']);
$searchField = addslashes($_POST['searchField']);
search_field($startFrom, $recordperPage, $searchCat, $searchField);
echo search_field($dbResult);
break;
}
//Script IF statement and Script continues
function search_field($startFrom, $recordperPage, $searchCat, $searchField) {
$conn = db_connect();
$query_dbResult = "SELECT * FROM registered_members WHERE $searchCat LIKE
'%$searchField%' ORDER BY date DESC LIMIT $startFrom, $recordperPage";
$dbResult = mysql_query($query_dbResult, $conn) or die(mysql_error());
db_connect_error($dbResult);
//echo $dbResult;
//break;
return $dbResult;
}