dph23577 2015-09-07 23:43
浏览 73
已采纳

在MySQL查询中使用PHP函数

Pretty much I have a function that converts a string to a URL friendly string.

Example: $string = "Hello World!", when passed through my function, it would read url($string) = "hello-world"

So what I want to do is find in my database where the row = "hello-world" But my databases aren't formatted like that so I want to pretty use my function is the sql query.

Example:

$sql = "SELECT * FROM table WHERE url(row) = 'hello-world'";

I don't know if this is possible or not. I know I could get the results use the function and then pass it though the database again. But I was hoping there was a simpler way.

By the way I am using MySQLi Procedural if it makes a difference.

  • 写回答

1条回答 默认 最新

  • dtrnish3637 2015-09-07 23:49
    关注

    You can use your function within the query string itself like this, it allows the function to be used like a string and prints in the "field" you type in, then your function can escape the characters and presets, then returns the string.

     $sql = "SELECT * FROM table WHERE ".url("field")." = 'hello-world'";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况