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
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.

 

 

Link to comment
Share on other sites

<?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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.