Creating a MYSQL query in a loop SEE LAST POST

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post 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
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

in PHP a query is a string
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

sorry i mean just text
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Keep this thread amicable please.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Did you manage to do it Reece?
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Glad I could help.
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

who says you can't?
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

sorry my fault spelling mistake on the var nothing to do with the code thanks reece
Post Reply