duanfen2349 2016-10-25 22:16
浏览 35
已采纳

php - 注意:数组转换为字符串

I am trying to compose a SQL query via php. But I get the notice mentioned in a pretty unusual way.

$i=0;
$SQL="";
$tableFieldsQueryFormat=array("hospitals"=>"(location LIKE '%@val%' OR phone_no LIKE '%@val%')","doctors"=>"doc_name LIKE '%@val%'","news"=>"title LIKE '%@val%'","diseases"=>"name LIKE '%@val%'","tips"=>"tip LIKE '%@val%'");
$tableName=array("hospitals","doctors","news","diseases","tips");
$query_str=str_replace("@val",trim("dummy"),$tableFieldsQueryFormat[$tableName[$i]]);
$SQL="SELECT * FROM $tableName WHERE $query_str AND isdeleted='0';"; // Notice is thrown here !!!

I have not treated the '$SQL' variable as an array in any part of the program, in fact I only declared and used it in the above snippet. What could be the reason behind this weird notice? I don't think I've made any syntax or logic errors.

  • 写回答

1条回答 默认 最新

  • drllqg2903 2016-10-25 22:22
    关注

    You are seeing this exception because $tableName is an array. When your string is being interpolated $tableName can not be converted to a string.

    Try this instead:

    $SQL = "SELECT * FROM " . explode(",",$tableName) . " WHERE $query_str AND isdeleted='0';";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了