dprichard Posted August 23, 2007 Share Posted August 23, 2007 Can you put a query in a php function. I moved a query from being in the page to a fuction function user() { $uid = $_SESSION['UID']; $user = mysql_query("SELECT emp_id, emp_name, emp_email, emp_username, emp_auth_level FROM all_employees WHERE emp_id = '$uid'") or die(mysql_error()); $row_user = mysql_fetch_array($user); $row_user_total = mysql_num_rows($user); } Then on my page I changed it from the code above to this include 'functions.php'; user(); but now all my variables give me errors saying they are undefined. Any help would be greatly appreciated. ??? ??? ??? ??? Link to comment https://forums.phpfreaks.com/topic/66398-php-functions-and-queries/ Share on other sites More sharing options...
marcus Posted August 23, 2007 Share Posted August 23, 2007 What variables exactly? Link to comment https://forums.phpfreaks.com/topic/66398-php-functions-and-queries/#findComment-332320 Share on other sites More sharing options...
corbin Posted August 23, 2007 Share Posted August 23, 2007 I think you need to Google variable scopes.... Link to comment https://forums.phpfreaks.com/topic/66398-php-functions-and-queries/#findComment-332323 Share on other sites More sharing options...
dprichard Posted August 23, 2007 Author Share Posted August 23, 2007 Sorry for not being specific enough. Here is one of my functions in the functions.php file: function company() { $company = mysql_query("SELECT company_name, company_ttracker_path, company_site_title, company_home_page_url, company_support_email FROM config") or die (mysql_error()); $row_company = mysql_fetch_array($company); } I call it into the page like so company(); But I get this... PHP Notice: Undefined variable: row_company in C:\DATA\sitename\htdocs\events\index.php on line 12 I use another function above that for the connection information and am not getting a connection error so I assume it is connection to the database correctly. Link to comment https://forums.phpfreaks.com/topic/66398-php-functions-and-queries/#findComment-332326 Share on other sites More sharing options...
Barand Posted August 23, 2007 Share Posted August 23, 2007 <?php function user() { $uid = $_SESSION['UID']; $user = mysql_query("SELECT emp_id, emp_name, emp_email, emp_username, emp_auth_level FROM all_employees WHERE emp_id = '$uid'") or die(mysql_error()); return mysql_fetch_assoc($user); } // call $user = user(); echo $user['emp_name']; Link to comment https://forums.phpfreaks.com/topic/66398-php-functions-and-queries/#findComment-332392 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.