2

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.

2
  • 1
    $tableName is an array and not a string, SQL queries take only string input. Commented Oct 25, 2016 at 22:20
  • @Amol told you the answer, but I'm pretty sure your making this more complicated than it needs to be. Arrays should serve a purpose. Commented Oct 25, 2016 at 22:24

1 Answer 1

0

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';";
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.