Page 3 of 3
Posted: Tue Aug 15, 2006 10:11 am
by reecec
ole wrote:OK lets get back to basics here:
Code: Select all
mysql_query(); // is a function
$variable // is a variable
= // means we are assigning something to the variable
'mysql_query("SELECT `field` FROM `table`")'; // this is not a function call, it is the name of a function with parameter stored as a string
// as a result:
echo $variable; // will echo 'mysql_query("SELECT `x` FROM `table`")'
echo mysql_query("SELECT `field` FROM `table`"); // will call the function and output the return value from the function.
// A good way to do it
$q = 'SELECT `field` FROM `table`';
echo mysql_query($q); // will use 'SELECT `field` FROM `table`' as a database query
// this will probably output nothing of use because mysql_query only returns a result resource. See the manual http://www.php.net/mysql_query/
In your case you have a problem with this line:
Code: Select all
$searchquery='mysql_query("SELECT * FROM `$table` WHERE ';
Thanks i see the difference between the function and the var my sript shows it as a var and not a function thanks ole for showing this. but still the problem is that the SQL query is not a query but is a string i have been told to cache the results by feyd i can tell this is true because when the query is echoed it gives the query not the resource id.
thanks reece
Posted: Tue Aug 15, 2006 10:34 am
by Ollie Saunders
in PHP a query is a string
Posted: Tue Aug 15, 2006 11:46 am
by reecec
sorry i mean just text
Posted: Tue Aug 15, 2006 1:03 pm
by Ollie Saunders
Reece you are quite testing.
Read the
example 2 for mysql_query in the PHP manual.
Learn what mysql_query does. It is NOT the only function you need to perform this task.
mysql_real_escape_string is needed BEFORE as I have explained
and critically mysql_fetch_assoc is need AFTER.
Posted: Tue Aug 15, 2006 1:05 pm
by RobertGonzalez
Keep this thread amicable please.
Posted: Tue Aug 15, 2006 1:53 pm
by Ollie Saunders
Did you manage to do it Reece?
Posted: Tue Aug 15, 2006 3:49 pm
by reecec
only 15 and dont know how to explain these isues to you
right the loop creates the query fine look it echos this when i have typed in my values from text boxes
look
("SELECT * FROM `select` WHERE `level` = '1' AND `user` = 'test' ")
and level 1 and user and test all come from variables which means these have all gone through the loop fine but just wont go into a query
so once the loop has worked it has this set in its var value
$searchquery="SELECT * FROM `select` WHERE `level` = '1' AND `user` = 'test' "
but will not work as a query
$dosearchquery=mysql_query($searchquery);
Gives this
Query failed: 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 '"SELECT * FROM `select` WHERE `level` = '1' AND `user` = 'test "' at line 1
thanks reece
Posted: Tue Aug 15, 2006 4:03 pm
by RobertGonzalez
Leave the double quotes off of the string. That is what is killing your query right now. Literally, this is what the SQL server is seeing...
Code: Select all
"SELECT * FROM `select` WHERE `level` = '1' AND `user` = 'test "
Run that query without the double quotes and you will be golden...
Code: Select all
SELECT * FROM `select` WHERE `level` = 1 AND `user` = 'test'
PS If you are doing a WHERE comparison on an integer you need to drop the single quote.
Posted: Tue Aug 15, 2006 4:05 pm
by reecec
Yes sorted thanks everah i knew it was more smple that what everone has been posting thats why i showed what was being sent it my last post
thanks again
Posted: Tue Aug 15, 2006 4:10 pm
by RobertGonzalez
Glad I could help.
Posted: Tue Aug 15, 2006 4:14 pm
by reecec
sorry just one last quick question
how come you cant do a mysql_fetch_row(): like this
if the $result is not a direct query
so it wont work if it is like this
Code: Select all
$result="SELECT * FROM anytable";
$doresult=mysql_query($result);
mysql_fetch_row($doresult):
so it would need to have direct var if you no what i mean
Code: Select all
$result=mysql_query("SELECT * FROM anytable");
any ideas
thanks reece
Posted: Tue Aug 15, 2006 4:17 pm
by feyd
who says you can't?
Posted: Tue Aug 15, 2006 4:20 pm
by reecec
sorry my fault spelling mistake on the var nothing to do with the code thanks reece