Ralph Posted December 16, 2021 Share Posted December 16, 2021 (edited) <?php // SET variables $servername = ******* your server $username = ******* your user name $password = ******* your password $database = ******* your database $table = ******* table name to return column names to assign to $_SESSION[$column] $column = ******* column to query $value = ******* column value to match column query /** WARNING avoid using on databases where column contents are large eg. BLOBs **/ // Create connection to database $conn = new mysqli($server, $username, $password, $database); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); // echo 'con failed'; } if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Database configuration $db = new mysqli($server, $username, $password, $database); // Check connection if ($db->connect_error) { die("Connection failed: " . $db->connect_error); } // Query to get columns from table $query = 'SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = "'.$database.'" AND TABLE_NAME = "'.$table.'"'; //echo $query; $resultcols = $conn->query($query); //print_r($_SESSION); while($rowcols = $resultcols->fetch_assoc()) { $sql = ' SELECT `'. $rowcols["COLUMN_NAME"].'` FROM `'.$table.'` where `'. $column .'` = "'.$value.'"'; $result = $conn->query($sql); // print_r($_SESSION); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $_SESSION[$rowcols["COLUMN_NAME"]] = $row[$rowcols["COLUMN_NAME"]]; } $err_message = "Success<br>"; } else { $err_message = "unknown information can not complete session variables<br>"; } } // see what you get print_r($_SESSION); echo "<br>".$err_message."<br>"; foreach($_SESSION as $x => $x_value) { echo "SESSION Key=" . $x . ", SESSION Value=" . $x_value; echo "<br>"; } ?> Edited December 16, 2021 by Ralph fix code Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now