Miks Posted March 30, 2015 Share Posted March 30, 2015 I have created a table in the database to store the winner of the lottery. The table has 3 columns: id, lotuid(userid), lotuname(username), the table successfully collects the users id but doesnt collect the username This is what I have done //Add to winner log $test=$db->query("SELECT `username` FROM `users` WHERE `userid`='$userid'"); $ug=$db->fetch_row($test); $db->query("INSERT INTO `lotterylog` (`id`, `lotuid`, `lotuname`) VALUES ('', '{$userid}', '{$test}')"); Any advice? Quote Link to comment Share on other sites More sharing options...
KyleMassacre Posted March 30, 2015 Share Posted March 30, 2015 You need to do: $ug['username'] Quote Link to comment Share on other sites More sharing options...
Miks Posted March 30, 2015 Author Share Posted March 30, 2015 Ah, thank you, I understand now Quote Link to comment Share on other sites More sharing options...
NonStopCoding Posted March 30, 2015 Share Posted March 30, 2015 (edited) I have created a table in the database to store the winner of the lottery. The table has 3 columns: id, lotuid(userid), lotuname(username), the table successfully collects the users id but doesnt collect the username This is what I have done //Add to winner log $test=$db->query("SELECT `username` FROM `users` WHERE `userid`='$userid'"); $ug=$db->fetch_row($test); $db->query("INSERT INTO `lotterylog` (`id`, `lotuid`, `lotuname`) VALUES ('', '{$userid}', '{$test}')"); Any advice? could easy be changed to if its a auto increment then you don't need to include it in the bit for sql $username = $db->fetch_single($db->query("SELECT `username` FROM `users` WHERE `userid` = {$ir['userid']}"); $db->query("INSERT INTO `lotterylog` (`lotuid`, `lotuname`) VALUES ('{$ir['userid']}', '{$username}')"); or if you wanted to fetch more than 1 column from the table same method can be applied but altered a little $un = $db->fetch_row($db->query("SELECT `username` FROM `users` WHERE `userid` = {$ir['userid']}"); $db->query("INSERT INTO `lotterylog` (`lotuid`, `lotuname`) VALUES ('{$ir['userid']}', '{$un['username']}')"); Edited March 30, 2015 by NonStopCoding Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted March 31, 2015 Share Posted March 31, 2015 could easy be changed to if its a auto increment then you don't need to include it in the bit for sql $username = $db->fetch_single($db->query("SELECT `username` FROM `users` WHERE `userid` = {$ir['userid']}"); $db->query("INSERT INTO `lotterylog` (`lotuid`, `lotuname`) VALUES ('{$ir['userid']}', '{$username}')"); or if you wanted to fetch more than 1 column from the table same method can be applied but altered a little $un = $db->fetch_row($db->query("SELECT `username` FROM `users` WHERE `userid` = {$ir['userid']}"); $db->query("INSERT INTO `lotterylog` (`lotuid`, `lotuname`) VALUES ('{$ir['userid']}', '{$un['username']}')"); This is assuming that the $ir variable has been previously defined as the current player's information (extracted from the database in globals.php) With that being said, the `username` is already part of the $ir associative array. Re-selecting the name shouldn't be necessary. $db->query("INSERT INTO `lotterylog` (`lotuid`, `lotuname`) VALUES (".$ir['userid'].", '".$ir['username']."')"); Quote Link to comment Share on other sites More sharing options...
NonStopCoding Posted March 31, 2015 Share Posted March 31, 2015 This is assuming that the $ir variable has been previously defined as the current player's information (extracted from the database in globals.php) With that being said, the `username` is already part of the $ir associative array. Re-selecting the name shouldn't be necessary. $db->query("INSERT INTO `lotterylog` (`lotuid`, `lotuname`) VALUES (".$ir['userid'].", '".$ir['username']."')"); hello stranger :) ye never thought of that much easier lol 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.