Jump to content

MySQL Rows to a string or even array


laPistola

Recommended Posts

mysql_select_db($database_bedroomBlogsDB, $bedroomBlogsDB);
$query_FLfriends_A_B_RS = "SELECT fid FROM friends_list WHERE `uid` = '$FLgetUID' AND status = '1'";
$FLfriends_A_B_RS = mysql_query($query_FLfriends_A_B_RS, $bedroomBlogsDB) or die(mysql_error());
$row_FLfriends_A_B_RS = mysql_fetch_assoc($FLfriends_A_B_RS);
$totalRows_FLfriends_A_B_RS = mysql_num_rows($FLfriends_A_B_RS);

mysql_select_db($database_bedroomBlogsDB, $bedroomBlogsDB);
$query_FLfriends_B_A_RS = "SELECT `uid` FROM friends_list WHERE fid = '$FLgetUID' AND status = '1'";
$FLfriends_B_A_RS = mysql_query($query_FLfriends_B_A_RS, $bedroomBlogsDB) or die(mysql_error());
$row_FLfriends_B_A_RS = mysql_fetch_assoc($FLfriends_B_A_RS);
$totalRows_FLfriends_B_A_RS = mysql_num_rows($FLfriends_B_A_RS);

function doAB($ab) {
while($ab) {	
	$ab['fid']",";
}
}
$FLab = doAB($row_FLfriends_A_B_RS);
$FLba = "";

$FLabba = $FLab.$FLba;

 

Above is about as far as i got before i tested and errors came up with unexpected T_CONSTANT_ENCAPSED_STRING  line 63

 

Basicly what im trying to do is create a string or it could be an array that contants id's numbers only so it can be run through another mySQL query.

 

The two query's above return only numbers on one colume but i need both query results to end up in the same string/array the string needs to look like 1,4,26,45,2,8 etc

 

any ideas please

Link to comment
Share on other sites

Were you wanting something like this?

 

<?php
$sql = "SELECT fid,uid FROM friends_list WHERE (`uid` = '$FLgetUID' OR `fid`='$FLgetUID') AND status = '1'";
$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);
$array = array();
while($row = mysql_fetch_assoc($result)){
    $array[] = $row['fid'];
    $array[] = $row['uid'];
}
echo '<pre>'.print_r($array,1).'</pre>';
?>

 

If not, you're going to need to explain your problem a bit more carefully.

 

Oh, and there's no need to select the database before each query. Once is sufficient (unless it changes).

Link to comment
Share on other sites

<?php
$sql = "SELECT fid,uid FROM friends_list WHERE (`uid` = '$FLgetUID' OR `fid`='$FLgetUID') AND status = '1'";
$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);
$friend_uid = array();
while($row = mysql_fetch_assoc($result)){
    array_push($friend_uid,$row['fid']);
    array_push($friend_uid,$row['uid']);
}
echo '<pre>'.print_r($friend_uid,1).'</pre>';
?>

 

???

Link to comment
Share on other sites

close enough i think, i didn't use one query with an OR as i thought that some of the results would contain the same value as $FLgetUID which would bring new problems when i run the array in the next sql query.

 

this array will be ran in the next SQL with an implode in the WHERE id IN to return all the members approved friends so having the same value as $FLgetUID which is the logged in users ID in the array would also return themselves as one of there own friends.

 

Im sure with what you have wrote i can adopt to what i need, the only thing im unsure of now is combining two arrays to make one?

 

Thank you

 

Link to comment
Share on other sites

right something isn't right??

 

i have cheched the DB and it should return 6 into colume uid and 8 into colume fid

 

all the extra code (like db calls on every query) is dreamweaver which its easier for me to keep in until im finshed then ill remove whats not needed.

 

<?php
$FLgetUID = 1;

mysql_select_db($database_bedroomBlogsDB, $bedroomBlogsDB);
$query_FLfriends_A_B_RS = "SELECT fid FROM friends_list WHERE `uid` = '$FLgetUID' AND status = '1'";
$FLfriends_A_B_RS = mysql_query($query_FLfriends_A_B_RS, $bedroomBlogsDB) or die(mysql_error());
$row_FLfriends_A_B_RS = mysql_fetch_assoc($FLfriends_A_B_RS);
$totalRows_FLfriends_A_B_RS = mysql_num_rows($FLfriends_A_B_RS);

mysql_select_db($database_bedroomBlogsDB, $bedroomBlogsDB);
$query_FLfriends_B_A_RS = "SELECT `uid` FROM friends_list WHERE fid = '$FLgetUID' AND status = '1'";
$FLfriends_B_A_RS = mysql_query($query_FLfriends_B_A_RS, $bedroomBlogsDB) or die(mysql_error());
$row_FLfriends_B_A_RS = mysql_fetch_assoc($FLfriends_B_A_RS);
$totalRows_FLfriends_B_A_RS = mysql_num_rows($FLfriends_B_A_RS);

$FLab = array();
while($ab = mysql_fetch_assoc($FLfriends_A_B_RS)) {
array_push($FLab,$ab['fid']);
}

$FLba = array();
while($ba = mysql_fetch_assoc($FLfriends_B_A_RS)) {
array_push($FLba,$ba['uid']);
}

$FLabba = array_merge($FLab,$FLba);

mysql_select_db($database_bedroomBlogsDB, $bedroomBlogsDB);
$query_FLmembersGetRS = "SELECT username, photo, imgtype FROM members WHERE id IN ('" . implode(",",$FLabba) . "')";
$FLmembersGetRS = mysql_query($query_FLmembersGetRS, $bedroomBlogsDB) or die(mysql_error());
$row_FLmembersGetRS = mysql_fetch_assoc($FLmembersGetRS);
$totalRows_FLmembersGetRS = mysql_num_rows($FLmembersGetRS);

// these two echos are just just to see if its working
echo "Username: ".$row_FLmembersGetRS['username']."<br />";
echo "logged in user id: ".$FLgetUID;

mysql_free_result($FLFLgetMemberIdRS);

mysql_free_result($FLfriends_A_B_RS);

mysql_free_result($FLfriends_B_A_RS);

mysql_free_result($FLmembersGetRS);
?>

 

the pages shows as

Username: 
logged in user id: 1

Link to comment
Share on other sites

just tested a few things

 

//$FLabba = array_merge($FLab,$FLba);
$FLabba = array("6","8");

 

worked, it echoed testing which is user id 6

 

tried

$query_FLmembersGetRS = "SELECT username, photo, imgtype FROM members WHERE id IN ('" . implode(',',$FLab) . "')";

and

$query_FLmembersGetRS = "SELECT username, photo, imgtype FROM members WHERE id IN ('" . implode(',',$FLba) . "')";

 

which both failed to show any user

 

also i echoed $row_FLfriends_A_B_RS['fid'] and $row_FLfriends_B_A_RS['uid']

which shown

Username: 
logged in user id: 1
ab query: 8
ba query: 6

Link to comment
Share on other sites

Right i have hit a problem today with this

 

Everything works until i get to the implode which don't seem to be doing its thing

 

the code is

<?php
$FLab = array();
while($ab = mysql_fetch_assoc($FLfriends_A_B_RS)) {
array_push($FLab,$ab['fid']);
}

$FLba = array();
while($ba = mysql_fetch_assoc($FLfriends_B_A_RS)) {
array_push($FLba,$ba['uid']);
}

$FLabba = array_merge($FLab,$FLba);

$query_FLmembersGetRS = "SELECT `id`, username, photo, imgtype FROM members WHERE `id` IN ('" . implode(',',$FLabba) . "') ORDER BY username ASC";
$FLmembersGetRS = mysql_query($query_FLmembersGetRS, $bedroomBlogsDB) or die(mysql_error());
$totalRows_FLmembersGetRS = mysql_num_rows($FLmembersGetRS);

?>

 

i have echo'ed $FLabba and it has the correct values 6 and 8 but when it goes through the query with the implode ist only seeing the first value in the array $FLabba which is 6, i confirmed this by echoing $totalRows_FLmembersGetRS whcih returned as 1, should be 2

 

Any ideas??

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.