I am using wamp server with php and mysql in windows 7.Have a class that connects to mysql database , a db__connect file is used to establish connection.
Below is the code for the db_connect;
<?php include_once 'psl-config.php'; // Needed because functions.php is not included $mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE); if ($mysqli->connect_error) { header("Location: ../error.php?err=Unable to connect to MySQL"); exit(); }
Below is the clas code :
<?php include_once $_SERVER['DOCUMENT_ROOT'].'/datacentre/admin/includes/db_connect.php'; class User { function getPermission( $email, $perms) { if ($stmt = $mysqli->prepare("SELECT members.email , groups.permission FROM members JOIN groups ON members.group_id = groups.group_id WHERE email = ? LIMIT 1")) { $stmt->bind_param('s', $email); // Bind "$email" to parameter. $stmt->execute(); // Execute the prepared query. $stmt->store_result(); // get variables from result. $stmt->bind_result($user_email, $user_perms); print "yes we can ".$user_perms; exit(); if($perms & $user_perms) {return true; exit();} return false; exit(); } } ?>
You need to either declare $mysqli inside your User Class, although I am betting you will want to reuse that connection in other classes. You would be better off passing $mysqli into the User Class
new User($email, $perms, $mysqli);
This of course assumes your instantiating the User class globally and not inside another class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.