Sql derived session variable not querying database on server

That is a really ancient version. Anyway you can update it to at least 5.6?

It appears nothing is in that table that matches what you want. I would go into PHPMyAdmin and check the table to ensure it has what you think it does.

That is the result on localhost. No error showed on server as to why it does now query anything. If $id generated from $_SESSION[‘id’] is used it will query on server.

That doesn’t follow what I said to do. My guess is nothing is actually wrong, but your database is not seeded.

I don’t understand the term seeded. The database on the server is populated. If I used $_SESSION[country]= ‘Ghana’, it will bring result on the server.this when the variable is not assigned from database table user result.

Your Php is over 300 releases out of date. The first thing you need to do is update your Php to a current version. Current release is version 7.4

You could have mentioned that. Then you should assign a default value to every user, or send an error message that they don’t have a country assigned. You are directly depending on a country being assigned to a user, so if that value isn’t present of course you will have an issue with the query when one doesn’t exist.

It sounds like the session_start() is failing (on one or both pages), which would be producing a php error. Do you have php’s error_reporting set to E_ALL (it should always be this value) and temporarily for debugging do you have php’s display_errors set to ON or do you have php’s log_errors set to ON and you are checking the web server’s error log? All of the php error settings should be in the php.ini on your system, so that they can be set or changed at a single point.

You could also be redirecting and changing the url (host name, i.e. WWW in a url vs no WWW in a url) such that a session started on one page doesn’t exist on the second page.

What does adding var_dump($_SESSION); immediately after the session_start() show?

This also points out a lack of validation and User eXperience (UX) logic in your code. If you were validating inputs and testing if queries (didn’t) match any rows, and were outputting useful messages in these cases, your code would help you pin down that cause of the problem. You do have logic testing if a query matches any row(s), but it doesn’t do anything for the false/else case.

You need to validate inputs to your code. If a ‘required’ input is not present, set up a message telling the user what required input is missing. If an input is optional, set up a default value for it when it doesn’t exist. If a database query is expected/required to match data for the page to work, but it doesn’t, set up a message telling the user that a required piece of data wasn’t found. If data from a database query is optional, set up a default value for it when it doesn’t exist.

BTW - because you set the mysqli error mode to use exceptions, none of the discrete error handling logic in your code will ever be executed and should be removed. On a mysqli error, execution will transfer to the nearest exception handling, which will be php if you don’t have any in your code.

Is nobody going to point out that $country =$SESSION[`country`]; is written wrong? That could be exactly why your program is failing. It’s supposed to be $country = $_SESSION['country'];

1 Like

Thanks for your assistance. The idea of $country =$SESSION[country]; written wrong is not as it is. The code were written correctly here like $country = $_SESSION[‘country’]. I used a new phone for which i am still learning its new keyboard pattern to send the message. Could it be the the properties of the country column on users must have strong similarities with the column country on market. Although, the two are varchar.

This isis what I tried of recent.

<?php
session_start();
require_once "inc/connect.php";
require_once "inc/function.php";

if (isset($_COOKIE["user_country"])){$country= $_COOKIE["user_country"];} 


	// this is index.php. head.php and connect.php are included in this index.php
	 
	 $result = mysqli_query($con,"SELECT * FROM market WHERE country='{$country}'") ;
	 

while ($row = mysqli_fetch_assoc($result)) {echo $row['name']."<br>"; } 
echo $_COOKIE["user_country"];
?>

I still find it hard ooo

Your code is still not validating the input data and outputting a user message if it doesn’t exist or testing if the query matched any data and outputting a user message if there was no matching data.

However, if by switching to use a cookie instead of a session variable, your code still doesn’t work, the most likely reason has already been given -

A redirect that changes the host name will affect both the session_start and using a cookie.

Sponsor our Newsletter | Privacy Policy | Terms of Service