求一sql语句转换函数

luoryan 2010-04-26 05:25:04
要求我输入一个sql的select语句,要在每张表的前面加上一个前缀
例如
输入select * from student
转换为:select * from student_student

输入select * from student where id=1
转换为:select * from student_student where id=1

嵌套查询里面的表也是一样
请问应该怎么样实现
...全文
43 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mu_rain 2010-04-27
  • 打赏
  • 举报
回复

$old_sql = "select * from student";
$new_sql = add_student($old_sql);
echo $new_sql ;

function add_student($str)
{
$str = str_replace('student','student_student',$str);
return $str;
}
ripya 2010-04-27
  • 打赏
  • 举报
回复

$fromPos = stripos ( $_sql, "from" ); //获得第一次“from”出现的位置。
$wherePos = stripos ( $_sql, "where" ); //获得第一次 where 出现的位置
$from_sql = substr ( $_sql, $fromPos ); //from部分语句 查询条件 $tableName = trim ( substr ( $_sql, $fromPos + 4, $wherePos - $fromPos - 4 ) ); //所操纵的数据表名


表名取到了,自己去拼吧
床上等您 2010-04-26
  • 打赏
  • 举报
回复
查找出 from 后面的一个串,replace 就行了。

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧