用php调用sql函数

时间:2018-08-23 15:33:34

标签: php sql function

我有一个返回字符串值的SQL函数,我想通过php调用它。应该很容易,但是两天后我的进步很小。

因此,为了进行测试,我在Namcheap的phpMyAdmin中创建了一个名为 testString 的函数,该函数仅返回一个字符串,当我在namecheap服务器上运行它时,它有效

Exception thrown: 'System.DllNotFoundException' in Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.dll
The thread 0x5250 has exited with code 0 (0x0).
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
'WindowsService1.exe' (CLR v4.0.30319: WindowsService1.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.ServiceProcess.resources\v4.0_4.0.0.0_de_b03f5f7f11d50a3a\System.ServiceProcess.resources.dll'. Module was built without symbols.
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
The thread 0x4fa0 has exited with code 0 (0x0).
Microsoft.AspNetCore.Server.Kestrel: Warning: Unable to bind to http://localhost:5000 on the IPv4 loopback interface: 'Die DLL "libuv": Das angegebene Modul wurde nicht gefunden. (Ausnahme von HRESULT: 0x8007007E) kann nicht geladen werden.'.
Exception thrown: 'System.DllNotFoundException' in Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.dll
The thread 0x58c4 has exited with code 0 (0x0).
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
Exception thrown: 'System.DllNotFoundException' in mscorlib.dll
Microsoft.AspNetCore.Server.Kestrel: Warning: Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Die DLL "libuv": Das angegebene Modul wurde nicht gefunden. (Ausnahme von HRESULT: 0x8007007E) kann nicht geladen werden.'.
Exception thrown: 'System.IO.IOException' in Microsoft.AspNetCore.Server.Kestrel.Core.dll
Exception thrown: 'System.IO.IOException' in mscorlib.dll
Exception thrown: 'System.IO.IOException' in mscorlib.dll
Exception thrown: 'System.IO.IOException' in mscorlib.dll
Microsoft.AspNetCore.Server.Kestrel: Critical: Unable to start Kestrel.

在php中,我可以打开数据库选择,获取值等。没问题,但是我无法成功调用函数。当我尝试时,我得到的是布尔类型而不是字符串:

DELIMITER $$
CREATE DEFINER=`user`@`localhost` FUNCTION `testString`(`yo` INT) RETURNS varchar(255) CHARSET latin1
  NO SQL
  SQL SECURITY INVOKER
  return "this is a string"$$
DELIMITER ;

类型:布尔值

如何从php调用此函数?

由于@Mehdi,我确实弄清楚了没有授予用户执行功能的权限,所以我授予了权限,现在它返回一个对象而不是布尔值。好,如何从该对象中提取文本字符串?

2 个答案:

答案 0 :(得分:0)

您最好丢掉整个东西。

$stringQuery = "SELECT testString();";
$stringResult = $con->query($stringQuery);
var_dump($stringResult->fetch_all());

var_dump($stringResult)

答案 1 :(得分:0)

这个简单的答案很酷。谢谢@Mehdi的指导。我从未想到过这么简单的事情会如此复杂。

因此,如果您有一个输出字符串的mySQL函数,并且想通过php检索该字符串值,请尝试以下php代码(对行进行了故障排除):

  $stringQuery = "SELECT testString();"; //testString is the function name.
  $stringResult = $con->query($stringQuery);
  //echo "<br>error: ".$con->error."<br>";
  //var_dump($stringResult);
  $resultArray = $stringResult->fetch_assoc();
  //var_dump($resultArray);
  echo $resultArray["testString()"]; //the function output.

如果有更直接的方法来获得此结果,请分享。

**编辑:这是一种非常直接的方法(尽管很难解析):

$stringVal = $con->query("SELECT testString();")->fetch_assoc()["testString()"];

很显然,如果您要向函数传递值,则该值将放在括号中,如testString(1)。