i have problem i am using below script for merge tables and get data from mysql
$myboxexi=mysql_query('select box.id,box.page_name,box.title,box.connect,box.type,box.uid,box.description,box.image,box.url,box.status,box.date,box.time
from boxes as box left join page_boxes as pages on box.uid=pages.uid and pages.uid="'.$session_id.'"');
while($my_boxi=mysql_fetch_array($myboxexi)){
$title=$my_boxi['title'];
$bid=$my_boxi['id'];
$ptitle=$my_boxi['page_name'];
$connect=$my_boxi['connect'];
$type=$my_boxi['type'];
$iuid=$my_boxi['uid'];
$description=$my_boxi['description'];
$image=$my_boxi['image'];
$url=$my_boxi['url'];
$appr=$my_boxi['status'];
$date=$my_boxi['date'];
$time=$my_boxi['time'];
$myinfo=mysql_query('select * from users where id="'.$iuid.'"');
$my_boxi_info=mysql_fetch_array($myinfo);
$user_id=$my_boxi_info['id'];
$user_name=$my_boxi_info['user_name'];
$thmb=$my_boxi_info['thumb'];
if(strpos($thmb,'https://') !== false) {
$thmbs=$thmb;
}else
{
$thmbs='images/thumbs/'.$thmb;
}
}
its working fine but problem is i am getting double item like if i have 1 item then i get 2 item how can i solve this issue ?
I think you get 2 values because you have for example,element [0] and same element with key=name of column , so use only associatif result :
Change this :
while($my_boxi=mysql_fetch_array($myboxexi)){
//data here.
}
To this :
while($my_boxi=mysql_fetch_array($myboxexi, MYSQL_ASSOC)){
//data here.
}
Or to this
while($my_boxi=mysql_fetch_assoc($myboxexi)){
//data here.
}
mysql_fetch_array()
returns a dual-keyed array. e.g.
SELECT foo FROM bar
$row = mysql_fetch_array($result);
will give you the array:
$row = array(
0 => 'value from foo',
'foo' => 'value from foo'
);
Since you're blindly looping on the returned array, you're looking up each of those "duplicate" results. If you only want ONE of the field types, try the dedicated functions, or the optional type argument,e .g
$row = mysql_fetch_assoc($result); // return only string keys
$row = mysql_fetch_array($result, MYSQL_ASSOC); // ditto
Presumably you are getting two items because two pages have the same user/session id that is being passed in:
select box.id, box.page_name, box.title, box.connect,box.type, box.uid, box.description,
box.image, box.url, box.status,b ox.date,box.time
from boxes box left join
page_boxes pages
on box.uid = pages.uid and pages.uid = "'.$session_id.'"'
(To me, it is suspicious that you are setting uid
-- which often means "user id" -- to a variable called $session_id
, but that is another matter.)
If you only want one, add limit 1
to the end of the query:
select box.id, box.page_name, box.title, box.connect,box.type, box.uid, box.description,
box.image, box.url, box.status,b ox.date,box.time
from boxes box left join
page_boxes pages
on box.uid = pages.uid and pages.uid = "'.$session_id.'"'
limit 1;