How To Label Sessions With Mysql Tbl Columns Names ?

sunny_pro

New member
Joined
Jun 18, 2017
Messages
86
Points
0
Php Gurus,


Look at this code. Do you see a long list of sessions ? On my membership site project, I'm having to write or copy & paste this long list of sessions on each page like home.php, users_list.php, post.php, delete_post.php, edit_post.php, etc.
Now, I want to cut short on the code.
These session variables are labeled after my mysql tbl column names. Therefore, I reckon, if I write code for the script to grab the column names and then create sessions based on the column names then the code would be cut short on each page.
I am stuck how to achieve this and so anybody's code sample would be most appreciated!

Current lengthy code that needs shortening like the way I just described:
PHP:
<?php 

//Required PHP Files.
include 'config.php';
include 'header.php';

//Check if User is already logged-in or not. Get the login_check() FUNCTION to check. 
if (login_check() === FALSE)
{
	//Redirect User to Log-in Page after 2 secs.
	header("refresh:2; url=login.php");
	exit();
}
else
{
	$user = $_SESSION["user"];
	
	$id = $_SESSION["id"]; 
	$account_activation_status = $_SESSION["account_activation_status"]; 
	$id_video_verification_status = $_SESSION["id_video_verification_status"]; 
	$id_video_verification_url = $_SESSION["id_video_verification_url"]; 
	$sponsor_username = $_SESSION["sponsor_username"]; 
	$recruits_number = $_SESSION["recruits_number"]; 
	$on_day_number_on_7_days_wish_list = $_SESSION["on_day_number_on_7_days_wish_list"]; 
	$primary_website_domain = $_SESSION["primary_website_domain"]; 
	$primary_website_email = $_SESSION["primary_website_email"]; 
	$username = $_SESSION["username"]; 
	$first_name = $_SESSION["first_name"]; 
	$middle_name = $_SESSION["middle_name"]; 
	$surname = $_SESSION["surname"]; 
	$gender = $_SESSION["gender"]; 
	$date_of_birth = $_SESSION["date_of_birth"]; 
	$age_range = $_SESSION["age_range"]; 
	$religion = $_SESSION["religion"]; 
	$education = $_SESSION["education"]; 
	$profession = $_SESSION["profession"]; 
	$marital_status = $_SESSION["marital_status"]; 
	$working_status = $_SESSION["working_status"]; 
	$home_town = $_SESSION["home_town"]; 
	$home_borough = $_SESSION["home_borough"]; 
	$home_city = $_SESSION["home_city"]; 
	$home_county = $_SESSION["home_county"]; 
	$home_region = $_SESSION["home_region"]; 
	$home_state = $_SESSION["home_state"]; 
	$home_country = $_SESSION["home_country"]; 
	
}

?>

Here's my rough attempt but I'm stuck and drowning in the muddy puddle!

PHP:
<?php 

//Required PHP Files.
include 'config.php';
include 'header.php';

//Check if User is already logged-in or not. Get the login_check() FUNCTION to check. 
if (login_check() === FALSE)
{
	//Redirect User to Log-in Page after 2 secs.
	header("refresh:2; url=login.php");
	exit();
}
else
{
	
$sql = "SHOW COLUMNS FROM browsing_histories";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
    $$row['Field'] =  $_SESSION["$row['Field']"]."<br>";
}

?>
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\test\grab_column_names.php on line ...

Attempt 2
PHP:
<?php 

//Required PHP Files.
include 'config.php';
include 'header.php';

//Check if User is already logged-in or not. Get the login_check() FUNCTION to check. 
if (login_check() === FALSE)
{
	//Redirect User to Log-in Page after 2 secs.
	header("refresh:2; url=login.php");
	exit();
}
else
{
	
$sql = "SHOW COLUMNS FROM browsing_histories";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
    $$row['Field'] =  $_SESSION["\$row['Field']\"]."<br>";
}

?>
Parse error: syntax error, unexpected '>' in C:\xampp\htdocs\test\grab_column_names.php on line ...


Remember, since the column names are like this:
id
username
gender

Then currently I got on my lengthy file, the sessions written like this:

$id = $_SESSION["id"];
$username = $_SESSION["username"];
$gender = $_SESSION["gender"];

I don't want to be writing lines of sessions like this to make the file size smaller and so need to write code so script fetches the column names and creates session variables under those fetched column names (or fetched labels or fetched fields).
In short, on the above mentioned lengthy code, you see a variable "$id" followed by " = $_SESSION" then followed by "['id'] again. I don't want to be typing all these 'id' and so want the script to write them by grabbing the column field name since here the column field name is 'id'. You know what I want to do. And so, care to show a code sample how to do it ? I made a few attempts and no luck.

Why did not my 1st attempt work ? See my following attempts:
PHP:
$sql = "SHOW COLUMNS FROM users";
	$result = mysqli_query($conn,$sql);
	while($row = mysqli_fetch_array($result)){
	${$row['Field']} = $_SESSION["{$row['Field']}"]."<br>";
	}
PHP:
I get errors on my 1st code:

Notice: Undefined index: date_&_time in C:\xampp\htdocs\test\grab_column_names.php on line 6

Notice: Undefined index: account_activation_code in C:\xampp\htdocs\test\grab_column_names.php on line 6

Notice: Undefined index: id_verification_video_file_url in C:\xampp\htdocs\test\grab_column_names.php on line 6

Notice: Undefined index: password in C:\xampp\htdocs\test\grab_column_names.php on line 6

Notice: Undefined index: passport_size_photoh_image in C:\xampp\htdocs\test\grab_column_names.php on line 6

Notice: Undefined index: title in C:\xampp\htdocs\test\grab_column_names.php on line 6

Notice: Undefined index: skin_complexion in C:\xampp\htdocs\test\grab_column_names.php on line 6

Notice: Undefined index: height in C:\xampp\htdocs\test\grab_column_names.php on line 6

Notice: Undefined index: weight in C:\xampp\htdocs\test\grab_column_names.php on line 6

Notice: Undefined index: sexual_orientation in C:\xampp\htdocs\test\grab_column_names.php on line 6

Notice: Undefined index: bio in C:\xampp\htdocs\test\grab_column_names.php on line 6


http://php.net/manual/en/language.variables.variable.php

Why did not my following attempts work ? See my following attempts:
PHP:
$sql = "SHOW COLUMNS FROM users";
	$result = mysqli_query($conn,$sql);
	while($row = mysqli_fetch_array($result)){
	${$row['Field']} = $_SESSION["{$row['Field']}"]."<br>";
	}
PHP:
$sql = "SHOW COLUMNS FROM users";
	$result = mysqli_query($conn,$sql);
	while($row = mysqli_fetch_array($result)){
	${$row['Field']} = $_SESSION["{$row['Field']}"]."<br>";
	echo ${$row['Field']}."<br>";
	echo $_SESSION["{$row['Field']}"]."<br>";
	}
 

sunny_pro

New member
Joined
Jun 18, 2017
Messages
86
Points
0
How To Count Mysql table Column Numbers ?

Hi,

Can someone be kind enough to show me how to implement this following formula to count the mysql table column numbers ?
int mysqli_stmt_field_count ( mysqli_stmt $stmt )

I need to see how to count the column numbers and echo it.
PHP: mysqli_stmt::$field_count - Manual


Here's my rough attempt. Note that, the php manual is not showing any code sample and so I'm totally kicking in the dark wherever I can. :confused:
Note the commented out lines to get an idea how I been experimenting before I came to this final code.

Here is the code I found somewhere. Seems like half done:
PHP:
if (mysqli_stmt_field_count($stmt)) {
    /* this was a select/show or describe query */
    $result = mysqli_stmt_store_result($stmt);

    /* process result set */
    $row = mysqli_stmt_fetch_row($result);

    /* free result set */
    mysqli_free_result($result);

	/* close connection */
	mysqli_close($conn);
Anyway, I tried filling in the blanks but at a total loss. I would be since the php manual showed no sample code. Nor google pulls out any.
PHP:
<?php 

//Required PHP Files.
include 'config.php';
include 'header.php';
include 'account_header.php';

//Check if User is already logged-in or not. Get the login_check() FUNCTION to check. 
if (login_check() === FALSE)
{
	//Redirect User to Log-in Page immediately.
	//header("refresh:0; url=login.php");
	header("location:login.php");
	exit();
}
else
{
	$user = $_SESSION["user"];
	
	$query = "";
    $stmt = mysqli_prepare($conn, $query); 	
	//mysqli_stmt_bind_param($stmt,'ss',$recipient_username,$sender_username); 
	mysqli_stmt_execute($stmt);  
	//$result_1 = mysqli_stmt_bind_result($stmt_1,$matching_rows_count); 
	//mysqli_stmt_fetch($stmt_1);
	//mysqli_stmt_free_result($stmt_1); 
	
	$fields_count = mysqli_stmt_field_count($stmt);
	if (mysqli_stmt_field_count($stmt)) {
    /* this was a select/show or describe query */
    $result = mysqli_stmt_store_result($stmt);

    /* process result set */
    $row = mysqli_stmt_fetch_row($result);

    /* free result set */
    mysqli_free_result($result);

	/* close connection */
	mysqli_close($conn);

		echo "Total Table Column Count: $fields_count<br>";
	printf(" %d fields found.\n",$fields_count); 
}
}
?>
Note my blank: $query =.
I need someone to show me how to do the query here.
 
Older threads
Replies
0
Views
2,980
Replies
0
Views
2,330
Replies
6
Views
2,641
I
  • itarachiu
  • Bing
Replies
2
Views
8,574
itarachiu
Latest threads
Recommended threads
Replies
6
Views
7,373
Replies
1
Views
2,828
Replies
3
Views
7,517
Replies
0
Views
3,880

Referral contests

Referral link for :

Sponsors

Popular tags

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top