solidrocketfuel Posted May 10, 2015 Share Posted May 10, 2015 Hello, I am new to php and stuck on this msqli function. I would like to echo out all the results divided by commas and apostrophe. Example: "23232","324231","342533" With the last one not having a comma. Here is what I came up with and not sure why I am getting errors. I think this is the issue but not sure: $list.= $result->'"'.video_code.'",'; Thanks. $sql = 'SELECT video_code FROM yt_videos WHERE yt_video_item_id="'.$vidid.'" ORDER BY RAND() LIMIT 10'; $query = mysqli_query($db_conx, $sql); $list=''; while ($result = $query ->fetch_object()) { $list.= $result->'"'.video_code.'",'; } echo $list=substr($list,0,-1); Quote Link to comment Share on other sites More sharing options...
Barand Posted May 10, 2015 Share Posted May 10, 2015 Store them in an array then join() the array elements echo join(', ', $theArray) Quote Link to comment Share on other sites More sharing options...
solidrocketfuel Posted May 10, 2015 Author Share Posted May 10, 2015 Thanks for the reply. I don't quite understand how to implement the join function in my code. I think I am close other wise as I am no longer getting an error but not quite the result I need. When I use this code it works: $list.= $result->video_code.'",'; Resulting in 2343234",345353",43533534" How do I write that line to include a " before each row result too. Looking for "2343234","345353","43533534" Thanks. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 10, 2015 Share Posted May 10, 2015 If you have an array, say $arr = [12345,23456,56789]; then using join echo '"' . join('","', $arr) . '"'; gives "12345","23456","56789" Quote Link to comment Share on other sites More sharing options...
solidrocketfuel Posted May 11, 2015 Author Share Posted May 11, 2015 (edited) Ok I tried the join function but now I am getting an error: Warning: join() [function.join]: Invalid arguments passed in... Here is my code. I just don't understand mysqli, I just have some experience and mysql. $sql='SELECT video_code FROM yt_videos WHERE yt_video_item_id="'.$vidid.'" ORDER BY RAND() LIMIT 10'; $result=mysqli_query($db_conx,$sql); mysqli_free_result($result); echo '"' . join('","', $result) . '"'; Edited May 11, 2015 by solidrocketfuel Quote Link to comment Share on other sites More sharing options...
fastsol Posted May 11, 2015 Share Posted May 11, 2015 That's cause you are giving the join() a resource, not an array like it needs. You need to get the array of results first with like mysqli_fetch_assoc() Quote Link to comment Share on other sites More sharing options...
solidrocketfuel Posted May 11, 2015 Author Share Posted May 11, 2015 Thanks Fastol. That helped. Now its only echoing one result. Here is my code now. No errors!! Getting close! $sql = 'SELECT video_code FROM yt_videos WHERE yt_video_item_id="'.$vidid.'" ORDER BY RAND() LIMIT 10'; $result=mysqli_query($db_conx,$sql); // Associative array $row = mysqli_fetch_assoc($result); echo '"' . join('","', $row) . '"'; Quote Link to comment Share on other sites More sharing options...
solidrocketfuel Posted May 11, 2015 Author Share Posted May 11, 2015 Sorry for being such a newbie guys. Just fixed the single echo with a while loop. But the only problem is that the comma between echos is not work. My result is: "_IvF4X658hY""DRF6b1N_KFk""G-keFAKQHy4" My code: $sql = 'SELECT video_code FROM yt_videos WHERE yt_video_item_id="'.$vidid.'" ORDER BY RAND() LIMIT 10'; $result=mysqli_query($db_conx,$sql); // Associative array while ( $row = mysqli_fetch_assoc($result)) echo '"' . join('","', $row) . '"' Quote Link to comment Share on other sites More sharing options...
fastsol Posted May 11, 2015 Share Posted May 11, 2015 The code works for me when I test it like this, basically simulating an array. $a = array( "_IvF4X658hY", "DRF6b1N_KFk", "G-keFAKQHy4" ); echo '"' . join('","', $a) . '"' The problem is likely that $row is not what you want to give the join(), meaning it's probably not formatted right. Do a print_r($row); in the while() and post it back here so I can see what the array looks like. Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted May 11, 2015 Share Posted May 11, 2015 I was curious why you are trying to echo them wrapped double quotes and commas Make it an array in the loop $sql = 'SELECT video_code FROM yt_videos WHERE yt_video_item_id="'.$vidid.'" ORDER BY RAND() LIMIT 10'; $result=mysqli_query($db_conx,$sql); $video_codes = array(); // Associative array while ( $row = mysqli_fetch_assoc($result)){ $video_codes[] = $row['video_code']; echo $row['video_code']."<br />"; } if(!empty($video_codes)){ print_r($video_codes); echo '"' . join('","', $video_codes) . '"'; } Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted May 11, 2015 Share Posted May 11, 2015 (edited) Post replied twice Edited May 11, 2015 by QuickOldCar Quote Link to comment Share on other sites More sharing options...
Barand Posted May 11, 2015 Share Posted May 11, 2015 Store the things you want to join in the array $sql = 'SELECT video_code FROM yt_videos WHERE yt_video_item_id="'.$vidid.'" ORDER BY RAND() LIMIT 10'; $result=mysqli_query($db_conx,$sql); $codes = array(); while ($row = mysqli_fetch_assoc($result)) { $codes[] = $row['video_code']; // add code to the array } // now join the array of codes echo '"' . join('","', $codes) . '"'; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.