Jump to content

PHP Functions and Queries


dprichard

Recommended Posts

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

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.

 

 

<?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'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.