Problem with an automatic loader

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
JanMolendijk
Posts:282
Location:Holland Rotterdam

Problem with an automatic loader

Hello i`m busy with an notification-system, but i have a problem in my automatic-loader.
It does not show $users[id] on the second &nd furter pages

I hope you could help me out.

Code: Select all

<?php
	//Database config detail
	define('_HOST_NAME','127.0.0.1');
    define('_DATABASE_NAME','comments');
    define('_DATABASE_USER_NAME','comments');
    define('_DATABASE_PASSWORD','123456');

$id = (int) $_GET['id'];

 
    $dbConnection = new mysqli(_HOST_NAME, _DATABASE_USER_NAME, _DATABASE_PASSWORD, _DATABASE_NAME);
    if ($dbConnection->connect_error) {
          trigger_error('Connection Failed: '  . $dbConnection->connect_error, E_USER_ERROR);
    }
	 
//item per page
	$limit = 2; 
	$page =(int)(!isset($_GET['p']))?1: $_GET['p'];
 
	// sql query
	$sqlContent="SELECT * FROM `alert` WHERE context='$users[id]' ORDER BY `id` DESC";
	





	

	//Query start point
	$start =($page * $limit)- $limit;
 
	$resContent=$dbConnection->query($sqlContent);
	$rows_returned = $resContent->num_rows;
	

	// query for page navigation
	if( $rows_returned > ($page * $limit)){
		$next =++$page;
	}
	
	$sqlContent = $sqlContent ." LIMIT $start, $limit";
	$finalContent = $dbConnection->query($sqlContent);
	if($finalContent === false) {
		trigger_error('Error: ' . $dbConnection->error, E_USER_ERROR);
	} else {
		$rows_returned = $finalContent->num_rows;
	}
?>

Admin Posts:805
Hello,
The $users array is not defined in your code; maybe it is created in other file where the code is included.
Anyway, if $users is defined, try use this sql select:

Code: Select all

$sqlContent="SELECT * FROM `alert` WHERE context='".$users['id']."' ORDER BY `id` DESC";

JanMolendijk Posts:282
I don`t understand it, I have on the same page this code also standing

Code: Select all

<?php 
  
include('../../connection.php');

$id = (int) $_GET['id'];

$sql=mysqli_query($conn,"SELECT * FROM `user` WHERE id='" . $id . "' ");
$users=mysqli_fetch_assoc($sql);
?>
But it does not load the user id on the other pages only page one

Code: Select all

<?php
	//Database config detail
	define('_HOST_NAME','127.0.0.1');
    define('_DATABASE_NAME','comments');
    define('_DATABASE_USER_NAME','comments');
    define('_DATABASE_PASSWORD','123456');

$id = (int) $_GET['id'];

    $dbConnection = new mysqli(_HOST_NAME, _DATABASE_USER_NAME, _DATABASE_PASSWORD, _DATABASE_NAME);
    if ($dbConnection->connect_error) {
          trigger_error('Connection Failed: '  . $dbConnection->connect_error, E_USER_ERROR);
    }
	 
//item per page
	$limit = 2; 
	$page =(int)(!isset($_GET['p']))?1: $_GET['p'];
 
	// sql query
	$sqlContent="SELECT * FROM `alert` WHERE context='$users[id]' ORDER BY `id` DESC";
	
	//Query start point
	$start =($page * $limit)- $limit;
 
	$resContent=$dbConnection->query($sqlContent);
	$rows_returned = $resContent->num_rows;
	
	// query for page navigation
	if( $rows_returned > ($page * $limit)){
		$next =++$page;
	}
	
	$sqlContent = $sqlContent ." LIMIT $start, $limit";
	$finalContent = $dbConnection->query($sqlContent);
	if($finalContent === false) {
		trigger_error('Error: ' . $dbConnection->error, E_USER_ERROR);
	} else {
		$rows_returned = $finalContent->num_rows;
	}
?>

Admin Posts:805
The code wiith the $users must be before where you use $users['id'].
Then repace your sql query qith this:

Code: Select all

$sqlContent="SELECT * FROM `alert` WHERE context='".$users['id']."' ORDER BY `id` DESC";
- Or, if you want to use it in other pages, store the $users['id'] in session, and get the user-id from session.
Something like this:

Code: Select all

if(isset($users) && isset($users['id'])) $_SESSION['uid'] = $users['id'];

if(isset($_SESSION['uid'])) $uid = $_SESSION['uid'];
else $uid =1 //default id

$sqlContent="SELECT * FROM `alert` WHERE context='$uid' ORDER BY `id` DESC";

JanMolendijk Posts:282
Thanks for supporting but i have found the problem something with a different .php address