简体   繁体   中英

Using a function within a MySQL query

I have the following line of code :

mysql_query("SELECT name FROM details WHERE md5(name) = '".md5($input_name)."'");

This query works just fine , however , when i change the query to the following :

mysql_query("SELECT name FROM details WHERE salt(name) = '".salt($input_name)."'");

The query doesn't seem to work.

The salt function is as follows :

function salt ($name) {


global $salt;



return $salt.$name;

}

where $salt is a global variable ( an md5 hash)

Why doesn't the second query work ?

MySQL has no access to functions you define in PHP. You can only use functions that MySQL defines in a MySQL query, or functions that you have written in SQL. You'll have to rethink what you're doing and express it in a way that does not require MySQL to use PHP functions.

Functions in PHP and functions in MySQL is two seperate things.

When sending MySQL queries, MySQL will be responsible for parsing the string you are sending. And MySQL doesn't know of any of the PHP code you made - and vice versa.

Wouldnt this be easily done by:

$saltinput = salt($input_name)
mysql_query("SELECT name FROM details WHERE salt(name) = '$salitinput'");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM