如何在PHP中使用MySQL用户定义的函数? [英] How do I use a MySQL user-defined function from within PHP?

查看:88
本文介绍了如何在PHP中使用MySQL用户定义的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

花了几个小时来寻找答案,但没有成功.我已经在MySQL中编写了一个用户定义的函数,该函数传递了一个标识符,该标识符用于检索各种数据,将其连接为一个字符串并返回它.我想从我的PHP页面调用此函数并输出结果.

Spent several hours searching for an answer without success. I've written a user-defined function in MySQL which is passed an identifier which it uses to retrieve various pieces of data, concatenate it into one string and return it. I want to call this function from my PHP page and output the result.

不成功的尝试包括:

1. $result = mysql_query("select functionName($id)"); 

2. $sql = "select functionName($id)";
   $result = mysql_query($sql, $link);

3. functionName($id) 

有什么想法吗?

推荐答案

1和2关闭,但是$result不会包含函数调用的结果.相反,它将包含来自查询的结果cookie.您可以使用mysql_fetch_row()使用该Cookie来获取实际数据.该函数调用仅返回select语句的值,与"SELECT 42"或"SELECT a FROM MyTable"相同.因此,要获得结果,您将使用与其他任何返回结果的SQL查询相同的机制.也就是说,使用cookie并调用mysql_fetch_row().因此,您的最终代码将如下所示:

1 and 2 are close, but $result is not going to contain the result of the function call. Rather, it is going to contain the result cookie from the query. You can use that cookie to get the actual data, with mysql_fetch_row(). The function call just returns a value for the select statement, just the same as "SELECT 42" or "SELECT a FROM MyTable". So to get the result you would use the same mechanism as with any other SQL query that returns results; that is, use the cookie and call mysql_fetch_row(). So your final code will look like this:

$result = mysql_query("select functionName($id)");
$row = mysql_fetch_row($result, $link);
$returnValue = $row[0];

请注意,您不想将变量直接插值到SQL字符串(可以是攻击的向量)中.但是,我认为这段代码仅供参考.

Note that you don't want to be interpolating variables directly into an SQL string (that can be a vector for attacks). I assume, however, that this code is just for example purposes.

这篇关于如何在PHP中使用MySQL用户定义的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆