Daney11 Posted January 29, 2007 Share Posted January 29, 2007 Hey Guys, Troubles with functions again... Basically i would like to know how to extract a username from a database using functions.[b]Connect_Database.php[/b][code]<?phpfunction ConnectDatabase(){global $db;$db = mysql_connect("localhost","root","") or die("Unable To Connect To The MySQL Database");mysql_select_db("database", $db) or die("Unable To Select The Database");}?>[/code]Above is my database connect script.How would i go about extracting all usernames from the `members` table in the database?Something like this?[code]<?phpfunction GetUsername($username) {$strQuery = "SELECT member_id, member_username FROM members WHERE member_id > '0'";$result = mysql_query($strQuery,$db) or die(mysql_error());$myrow = mysql_fetch_array($result)$username = $myrow['member_username'];}echo GetUsername("$username");[/code].............Where am i going wrong? And how would i go about doing it correctly?Thanks again guys,Dane Link to comment https://forums.phpfreaks.com/topic/36221-solved-functions-again/ Share on other sites More sharing options...
fert Posted January 29, 2007 Share Posted January 29, 2007 http://us2.php.net/manual/en/functions.returning-values.php Link to comment https://forums.phpfreaks.com/topic/36221-solved-functions-again/#findComment-172094 Share on other sites More sharing options...
Daney11 Posted January 29, 2007 Author Share Posted January 29, 2007 For some reason or another that link doesnt help me mate, i went on it yesterday and im still not sure about it. I read my book also and that explains it but i still cannot get it. Link to comment https://forums.phpfreaks.com/topic/36221-solved-functions-again/#findComment-172098 Share on other sites More sharing options...
Jessica Posted January 29, 2007 Share Posted January 29, 2007 $username = $myrow['member_username'];return $username; //Add this line here. Did you really read the link ? Link to comment https://forums.phpfreaks.com/topic/36221-solved-functions-again/#findComment-172100 Share on other sites More sharing options...
Daney11 Posted January 29, 2007 Author Share Posted January 29, 2007 Yes, i really did read the link.I am getting an error.The function is...[code]function GetUsername($username) {$strQuery = "SELECT manager_id, manager_first_name FROM managers WHERE manager_id > '0'";$result = mysql_query($strQuery,$db) or die(mysql_error());$myrow = mysql_fetch_array($result);$username = $myrow['manager_first_name'];return $username;}[/code]ive included it in my script and used...<?php echo GetUsername("$username"); ?>and im getting these errorsNotice: Undefined variable: username in c:\program files\www\premiership.php on line 43Notice: Undefined variable: db in c:\program files\www\included_files\total_goals.php on line 35Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in c:\program files\www\included_files\total_goals.php on line 35and line 35 is$result = mysql_query($strQuery,$db) or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/36221-solved-functions-again/#findComment-172112 Share on other sites More sharing options...
Jessica Posted January 29, 2007 Share Posted January 29, 2007 Why are you passing it $username in the first place? Take that out.function GetUsername() {} Link to comment https://forums.phpfreaks.com/topic/36221-solved-functions-again/#findComment-172118 Share on other sites More sharing options...
Daney11 Posted January 29, 2007 Author Share Posted January 29, 2007 Sorted thanks.i had to take out the $db in this line$result = mysql_query($strQuery,$db) or die(mysql_error());All works fine now.Thanks jesirose Link to comment https://forums.phpfreaks.com/topic/36221-solved-functions-again/#findComment-172121 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.