Using PHP what is the best way to get multiple rows in one MySQL query? -
this question has answer here:
which best way multiple rows in 1 mysql query.
i have array of ids:
$id_array = array('34','341','342','334','344','354','3234','33234','3234','3234');
i get title associated id's mysql database.
i have 2 approaches:
1) example:
foreach($id_array $id){ $query = mysqli_query($con, "select title content id = '$id'"); $id_db = mysqli_fetch_row($query); echo $id_db['title']; }
2) example:
$query = mysqli_query($con, "select title content id = '$id_array[1]' , id = '$id_array[2]' , id = '$id_array[3]' , 'id = $id_array[4]' , id = '$id_array[5]'"); while($result = mysqli_fetch_assoc($query)){ echo $result['title']; }
i working on high load site , use best solution. above code isn't 100% complete, raw implementation of idea. array elements can 1 1k in count.
what solution ?
$ids = implode(',',array_map('intval',$id_array)); $query = mysqli_query($con, "select title content id in ($ids)"); // ..
Comments
Post a Comment