1

Hi i am an intermediate at php. I want to fetch data from a table and use that fetched data to select another item from a different table. And then put those all result values into an array.

My original php code is like below.

$res = mysqli_query($con, "select title, nickname, date, cos_id, likecount from A where area_tag='$tag'");
$result=array();
while($row=mysqli_fetch_array($res)){
array_push($result, array('title'=>$row[0], 'nickname'=>$row[1], 'date'=>$row[2], 'cos_id'=>$row[3], 'likecount'=>$row[4]));    
}

echo json_encode(array("result"=>$result));

'cos_id' is a string value such as "ABC, Hello, Good". So if i get the cos_id from the table A, i want to split that String into "ABC", "Hello", "Good". Then use each String such as:

select name from B where tag='ABC',
select name from B where tag='Hello',
select name from B where tag='Good'

Eventually, i also want these three result values to put into array like

array_push($result, array('title'=>$row[0], 'nickname'=>$row[1], 'date'=>$row[2], 'cos_id'=>$row[3], 'likecount'=>$row[4], 'ABCResult'=>$row[5], 'HelloResult'=>$row[6], 'GoodResult'=>$row[7]));   

Is this possible? I found some information of splitting String, however i have no idea how to select and put the new result values into the original result array.

Since the cos_id data is something like ABC,Hello,Good... select contentid, hit from B where tag IN (cos_id)

then i get some values like this. contentid hit 1111 1 2222 2 3333 3

I want to put these results into this array... array_push($result, array('title'=>$row[0], 'nickname'=>$row[1], 'date'=>$row[2], 'cos_id'=>$row[3], 'likecount'=>$row[4]));

13
  • You can use where IN clause in sql to select data. Commented Feb 25, 2017 at 4:54
  • What is the schema of your table B? Commented Feb 25, 2017 at 4:54
  • @MaulikSavaliya For example, i want to fetch name, hit, image from table B where tag is 'ABC'. Do i have to use Loop statements? Commented Feb 25, 2017 at 4:59
  • @TheDongster No you can do it by single query using IN statement in MySql. Check below answer. Commented Feb 25, 2017 at 5:01
  • @TheDongster I am not able to write comment, You can ask here what you want. :) Commented Feb 25, 2017 at 5:12

2 Answers 2

4

instead of below:

select name from B where tag='ABC',
select name from B where tag='Hello',
select name from B where tag='Good'

You can execute query like below:

select name from B where tag IN ("ABC, HELLO, GOOD");
Sign up to request clarification or add additional context in comments.

2 Comments

Wow i should try with this. By finding some info of IN statement, i can actually have a try~! thanks
Can i ask one more? if i got three result value set, then how should i merge these three result value set into that result array? any ideas?
1

Here is a test with an array of values

$arr=["ABC, Hello, Good","DFG, Sup, Bad"];
$temp = implode(',',$arr);

$exp=explode(',',$temp);
   $sql = "SELECT name from B Where tag IN ('" . implode("','", $exp) . "') ";

       echo $sql;

SELECT name from B where tag IN ('ABC','Hello','Good','DFG',' Sup',' Bad')

Substitute $row[3] for $arr

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.