Jakebert Posted June 25, 2012 Share Posted June 25, 2012 This is a silly question about functions. Why does this code: <?php //connect to the database $host = "localhost"; $DBuser = "root"; $DBpassword = ""; $database = "ksu"; $connection = new mysqli($host, $DBuser, $DBpassword, $database); if ($connection->connect_error) { die('Connection Error (' . $connection->connect_errno . ') ' . $mysqli->connect_error); } function getUserGroups($id) { $query = $connection->query("SELECT group_id FROM group_membership WHERE user_id = " . $id); // this is line 20, where the error gets thrown $results = mysqli_fetch_assoc($query); return $results; } ?> Throw this error: Notice: Undefined variable: connection in C:\wamp\www\KSU\connect.php on line 20 There must be a really obvious answer I'm missing. Quote Link to comment https://forums.phpfreaks.com/topic/264719-silly-function-question/ Share on other sites More sharing options...
KevinM1 Posted June 25, 2012 Share Posted June 25, 2012 You need to pass $connection into the getUserGroups() function, like you did with $id. Quote Link to comment https://forums.phpfreaks.com/topic/264719-silly-function-question/#findComment-1356748 Share on other sites More sharing options...
Drummin Posted June 25, 2012 Share Posted June 25, 2012 Maybe I'm wrong but shouldn't the query be $query = $connection->query("SELECT group_id FROM group_membership WHERE user_id =$id"); Quote Link to comment https://forums.phpfreaks.com/topic/264719-silly-function-question/#findComment-1356749 Share on other sites More sharing options...
Jakebert Posted June 25, 2012 Author Share Posted June 25, 2012 You are correct! Thanks for finding that for me! Quote Link to comment https://forums.phpfreaks.com/topic/264719-silly-function-question/#findComment-1356754 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.