Php function to get mysql values

Hi guys, could anyone help me to convert the following code into a php function? I need this code in every page and i thought maybe creating a function will save me a lot of time. Many thanks for your help

$user_email = $_SESSION['user_email'];

$query_subscription_master = mysqli_prepare ($connmaster, "SELECT master_user_membership_start, master_user_membership_end, master_membership_description, master_membership_file_quota, master_membership_group_quota FROM master_membership LEFT JOIN master_user ON master_user . master_user_membership = master_membership . master_membership_id WHERE master_user_email = ?");

mysqli_stmt_bind_param($query_subscription_master, 's', $user_email);              
mysqli_stmt_execute($query_subscription_master);
mysqli_stmt_bind_result($query_subscription_master, $user_subscription_start, $user_subscription_end, $user_subscription_desc, $subscription_file_quota, $subscription_group_quota);
mysqli_stmt_fetch($query_subscription_master);
mysqli_stmt_close($query_subscription_master);

$today = date("Y-m-d H:i:s");

if ($today > $user_subscription_end){

    // Rimando alla pagina subscription_expired se il file non esiste
    header("location: subscription_expired.php");

}

@vincekaribusanaā€¦since uā€™ve mentioned that require this code in every pageā€¦why dont u create this part as an individual php page and later include this page in other formsā€¦

some.php

    $user_email = $_SESSION['user_email'];

$query_subscription_master = mysqli_prepare ($connmaster, "SELECTmaster_user_membership_start, master_user_membership_end,master_membership_description, master_membership_file_quota, master_membership_group_quota FROM master_membership LEFT JOIN master_user ON master_user . master_user_membership = master_membership . master_membership_id WHERE master_user_email = ?");

     mysqli_stmt_bind_param($query_subscription_master, 's', $user_email);              
mysqli_stmt_execute($query_subscription_master);
 mysqli_stmt_bind_result($query_subscription_master, $user_subscription_start, 
 $user_subscription_end, 
  $user_subscription_desc, $subscription_file_quota, $subscription_group_quota);
mysqli_stmt_fetch($query_subscription_master);
    mysqli_stmt_close($query_subscription_master);

 $today = date("Y-m-d H:i:s");

 if ($today > $user_subscription_end){

// Rimando alla pagina subscription_expired se il file non esiste
header("location: subscription_expired.php");
}

allform.php

include 'some.php';
2 Likes

The down side of including a bit of code like this is that you also have to consider the possibility of variable name clashes.

A function that returns either true or false depending on whether the subscription has expired would be a good thing to do. I would simply do the following:

  • Add a function declaration instead of the first line of code, something like
function checkSubscriptionCurrent($user_email, $dbconn) { 
  • replace the header relocation code at the end with something like
    return false;
  else 
    return true;
  }
  • Change every instance of $connmaster inside the function to be $dbconn because weā€™re passing that in as a parameter to the function.

Then all you need to do is call it

$current = checkSubscriptionCurrent($_SESSION['user_email'], $connmaster);

and youā€™ll get true if it is current, and false if it is not. Your calling code should then handle doing a header redirect if thatā€™s what you want to do*. And your calling code should probably do some validation on the session variable before using it.

If youā€™re going to use the function a lot, you should separate that into a separate php file and include it in each of your other files that use it. For stuff thatā€™s used a lot, I tend to just add them into the same file where I do the database connection, though there might be sound reasons for not doing that.

While youā€™re at it, you could optimise that query quite a bit. Within the function, you only use the $user_subscription_end variable, so thereā€™s no point retrieving the others.

ETA - * you might not always want to do the redirect, hence leaving that out of the function.

2 Likes

Hi @droopsnoot many thanks for your help, Iā€™ve created this function and added to my functions.php file

function check_membership($connmaster, $user_email){

    $query_subscription_master = mysqli_prepare ($connmaster, "SELECT master_user_membership_start, master_user_membership_end, master_membership_description, master_membership_file_quota, master_membership_group_quota FROM master_membership LEFT JOIN master_user ON master_user . master_user_membership = master_membership . master_membership_id WHERE master_user_email = ?");

    mysqli_stmt_bind_param($query_subscription_master, 's', $user_email);              
    mysqli_stmt_execute($query_subscription_master);
    mysqli_stmt_bind_result($query_subscription_master, $user_subscription_start, $user_subscription_end, $user_subscription_desc, $subscription_file_quota, $subscription_group_quota);
    mysqli_stmt_fetch($query_subscription_master);
    return array($user_subscription_desc, $user_subscription_start, $user_subscription_end);

}

Now into any page Iā€™m able to call the variables as an array

include('../config/config.php'); // where $connmanster is stored

$user_email = $_SESSION['user_email'];

$result_array = check_membership($connmaster, $user_email);
$description = $result_array[0];
$start = $result_array[1];
$end = $result_array[2];

I can then check if the subscription is stil valid using an if statement

$today = date("Y-m-d H:i:s");
if ($today > $end){

    // send user to subscription_expired if subscription has expired
    header("location: subscription_expired.php");


}

I donā€™t know if is possible to include the if statement into the function as well. Could you please tell me if this is a good code? And yes Iā€™ll simplify the query removing the variables and tables I donā€™t need :wink:

Many thanks again

sure:

-- returns an empty set if there is no match
SELECT 
    u.master_user_membership_start, 
    u.master_user_membership_end, 
    m.master_membership_description, 
    m.master_membership_file_quota, 
    m.master_membership_group_quota 
FROM 
    master_membership m
LEFT JOIN 
    master_user u
    ON 
        u.master_user_membership = m.master_membership_id 
WHERE 
        u.master_user_email = ?
    AND NOW() < u.master_user_membership_end

note: fetching data off a mysqli_stmt is easier if you use mysqli_stmt::get_result() beforehand.

1 Like

Hi @Dormilich thanks for your reply. I didinā€™t think about inserting the if statement inside the MySQL query, that is a great idea :wink: should it not be NOW() > u.master_user_membership_end? Also how Cani redirect the user to a subscription page if membership has expired? Many thanks for your support

redirect if there is no result (because then either the user doesnā€™t exist or is expired.

Hi Iā€™ve finally created this function

function check_membership($connmaster, $user_email){

    $query_subscription_master = mysqli_prepare ($connmaster, "SELECT master_user_membership_start, master_user_membership_end, master_membership_description, master_membership_file_quota, master_membership_group_quota FROM master_membership LEFT JOIN master_user ON master_user . master_user_membership = master_membership . master_membership_id WHERE master_user_email = ? AND NOW() < master_user_membership_end");

    mysqli_stmt_bind_param($query_subscription_master, 's', $user_email);              
    mysqli_stmt_execute($query_subscription_master);
    mysqli_stmt_bind_result($query_subscription_master, $user_subscription_start, $user_subscription_end, $user_subscription_desc, $subscription_file_quota, $subscription_group_quota);

    mysqli_stmt_store_result($query_subscription_master);
    $numUsers = mysqli_stmt_num_rows($query_subscription_master);
    mysqli_stmt_fetch($query_subscription_master);

    if ($numUsers > 0) {

        return array($user_subscription_desc, $user_subscription_start, $user_subscription_end);

    }else {

        header("location: ../administrator/subscription_expired.php");

        exit();


    }
    


}

Now the only problem is I get this error:

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/MioCondominio/administrator/admin_dashboard.php:85) in /Applications/MAMP/htdocs/MioCondominio/includes/functions.php on line 38

Line 38 of functions.php is
header(ā€œlocation: ā€¦/administrator/subscription_expired.phpā€);

Line 85 of admin_dashboard is

<?php include('includes/admin_topbar.php'); ?>

Hi guys, sorted calling the function before line 85 :slight_smile: many thanks for all your support guys :wink:

Glad it is working for you.

Personally, Iā€™d always have a function return something to the calling code and then act on it in the calling code, rather than having the function do the redirect. I (and again I stress itā€™s a personal thing) dislike functions that have external limitations / requirements unless thereā€™s no alternative - so here, you can only call this function before youā€™ve sent any screen output, otherwise you get ā€˜headers already sentā€™ as you saw. Other similar things are functions that rely on global variables - the limitation being that you have to use the same names everwhere, or your function wonā€™t work - which is why I talked about passing the database connection as a parameter rather than just declaring it as a global.

But I do stress the ā€œpersonallyā€ part of the above, again. If it works for you, then itā€™s not really for me to pick holes in. I guess if your calling code will always do a header redirect under certain circumstances, then it does make some sense.

Hi @droopsnoot your answers are very important for me, Iā€™m learning a lot thanks to your suggestion. Many thanks :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.