Avatar of CipherIS
CipherIS
Flag for United States of America asked on

PHP - No data displaing from SQL Server database

Data is not loading from database on PHP page.

How can I troubleshoot data coming from the database?

The headers are displaying but no data underneath.

Database is SQL Server and it is using a Security Certificate.  I never added one before.
DatabasesPHPHTML

Avatar of undefined
Last Comment
Ryan Chong

8/22/2022 - Mon
Ryan Chong

The headers are displaying but no data underneath.

quick verifications:

1. try check the HTML generated, and see if there's any issues/ errors?
2. check the query and make sure got data being returned?
Robert Granlund

Can you post your query? and use var_dump on your query?
CipherIS

ASKER
Checked HTML.  No errors on the page or page source.

Used var_dump on the variable and the value is NULL.  

Working on going to the page where the data is obtained and using VAR_DUMP

Question
I have the below

\dir1\index.php
\dir2\api.php

index.php gets the data from api.php.  If i put a var_dump in api.php will it return it to index.php page?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
CipherIS

ASKER
I'm not getting anything.

<?php
	$servername   = "xxx.xxx.xxx.xxx";
	$database = "xxxxx";
	$username = "xxxxxx";
	$password = "xxxxxxx";

	// Create connection
	$conn = new mysqli($servername, $username, $password);
	// Check connection
	if ($conn->connect_error) {
	die("Connection failed: " . $conn->connect_error);
	}
	echo "Connected successfully";
?>

Open in new window

Robert Granlund

What do you get if you add var_dump ($conn );
like:
<?php
	$servername   = "xxx.xxx.xxx.xxx";
	$database = "xxxxx";
	$username = "xxxxxx";
	$password = "xxxxxxx";

	// Create connection
	$conn = new mysqli($servername, $username, $password);
var_dump ($conn );
	// Check connection
	if ($conn->connect_error) {
	die("Connection failed: " . $conn->connect_error);
	}
	echo "Connected successfully";
?>

Open in new window

lenamtl

Hi, check your PHP logs and MySQL logs for error

Let say for a local installation using wampserver  this will be located in c / wamp64 / logs

 you can also enable PHP errors

<?php

// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

?>

Open in new window


Ref http://php.net/manual/en/function.error-reporting.php
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
CipherIS

ASKER
@rgranlund - I get a NULL

@lenamtl - you want me to run that script?
Robert Granlund

Ahhh:
$conn = new mysqli($servername, $username, $password);
You forgot the database.
$conn = new mysqli($servername, $database, $username, $password);

put @lenamtl script at the very top of the page and it will show you all, if any errors.
lenamtl

Hi,
first check your web server logs
PHP logs and MySQL logs for error
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
CipherIS

ASKER
Where do you check the PHP logs on Ubuntu?
CipherIS

ASKER
Also, it is connecting to SQL Server and not MySQL

I put @lenamtl script on top of page - nothing but nulls for the var_dump
CipherIS

ASKER
How do I get the value of key?
IF($_SERVER["REQUEST_METHOD"] == "POST"){
		$portal_key = (ISSET($_POST["portal_key"]) ? $_POST["portal_key"] : $_COOKIE["portal_key"]);
		ECHO(JSON_ENCODE(ms_select_value("EXEC [Portal].[dbo].[SP_ui_modules_fetchAccess] @key='".$portal_key."', @mod='".$_POST["mod"]."';")));

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
CipherIS

ASKER
I'm looking in the Ubuntu server /var/log/apache2/ and don't see "errors.log" or "php_errors.log"
CipherIS

ASKER
Below is the dbase connection code
<?PHP
	$my_default_conn = ARRAY(
		"xx.xx.xx.xx",
		"xxxxx",
		"xxxxx"
	);
	
	$ms_default_conn = ARRAY(
		"xx.xx.xx.xx",
		"xxxx",
		"xxxx",
		"xxxx"
	);
	
	FUNCTION my_select_value($query, $host = NULL, $user = NULL, $pass = NULL){
		IF($host == NULL || $user == NULL || $pass == NULL){
			GLOBAL $my_default_conn;
			$host = $my_default_conn[0];
			$user = $my_default_conn[1];
			$pass = $my_default_conn[2];
		}
		
		$conn = MYSQLI_CONNECT($host, $user, $pass);
		$resultrow = MYSQLI_FETCH_ARRAY(MYSQLI_QUERY($conn, $query), MYSQLI_NUM);
		MYSQLI_CLOSE($conn);
		RETURN $resultrow[0];
	}
	
	FUNCTION my_select_table($query, $host = NULL, $user = NULL, $pass = NULL){
		IF($host == NULL || $user == NULL || $pass == NULL){
			GLOBAL $my_default_conn;
			$host = $my_default_conn[0];
			$user = $my_default_conn[1];
			$pass = $my_default_conn[2];
		}
		
		$conn = MYSQLI_CONNECT($host, $user, $pass);
		$process = MYSQLI_QUERY($conn, $query);
		$resultset = ARRAY();
		WHILE($resultrow = MYSQLI_FETCH_ARRAY($process, MYSQLI_NUM)){
			$resultset[] = $resultrow;
		}
		MYSQLI_CLOSE($conn);
		RETURN $resultset;
	}
	
	FUNCTION my_run_query($query, $host = NULL, $user = NULL, $pass = NULL){
		IF($host == NULL || $user == NULL || $pass == NULL){
			GLOBAL $my_default_conn;
			$host = $my_default_conn[0];
			$user = $my_default_conn[1];
			$pass = $my_default_conn[2];
		}
		
		$conn = MYSQLI_CONNECT($host, $user, $pass);
		$resultrow = MYSQLI_QUERY($conn, $query);
		MYSQLI_CLOSE($conn);
		RETURN $resultrow;
	}
	
	FUNCTION my_escape_string($raw){
		GLOBAL $my_default_conn;
		$host = $my_default_conn[0];
		$user = $my_default_conn[1];
		$pass = $my_default_conn[2];
		
		$conn = MYSQLI_CONNECT($host, $user, $pass);
		
		RETURN MYSQLI_REAL_ESCAPE_STRING($conn, $raw);
	}
	
	FUNCTION ms_select_value($query, $connectionInfo = NULL){
		IF($connectionInfo == NULL){
			$connstring = $GLOBALS["ms_default_conn"];
			$connection = MSSQL_CONNECT($connstring[0], $connstring[1], $connstring[2]);
		} ELSE {
			$connection = MSSQL_CONNECT($connectionInfo[0], $connectionInfo[1], $connectionInfo[2]);
		}
		
		$process =  MSSQL_QUERY($query, $connection);
		$resultset = MSSQL_FETCH_ARRAY($process);
		
		MSSQL_FREE_RESULT($process);
		MSSQL_CLOSE($connection);
		RETURN $resultset[0];
	}
	
	FUNCTION ms_select_table($query, $server = NULL, $connectionInfo = NULL){
		IF($connectionInfo == NULL){
			$connstring = $GLOBALS["ms_default_conn"];
			$connection = MSSQL_CONNECT($connstring[0], $connstring[1], $connstring[2]);
		} ELSE {
			$connection = MSSQL_CONNECT($connectionInfo[0], $connectionInfo[1], $connectionInfo[2]);
		}
		
		$process =  MSSQL_QUERY($query, $connection);
		
		$resultset = ARRAY();
		WHILE($resultrow = MSSQL_FETCH_ARRAY($process)){
			$resultset[] = $resultrow;
		}
		
		MSSQL_FREE_RESULT($process);
		MSSQL_CLOSE($connection);
		RETURN $resultset;
	}
	
	FUNCTION ms_run_query($query, $server = NULL, $connectionInfo = NULL){
		IF($connectionInfo == NULL){
			$connstring = $GLOBALS["ms_default_conn"];
			$connection = MSSQL_CONNECT($connstring[0], $connstring[1], $connstring[2]);
		} ELSE {
			$connection = MSSQL_CONNECT($connectionInfo[0], $connectionInfo[1], $connectionInfo[2]);
		}
		
		$process =  MSSQL_QUERY($query, $connection);
		$resultset = MSSQL_FETCH_ARRAY($process);
		
		MSSQL_FREE_RESULT($process);
		MSSQL_CLOSE($connection);
		RETURN $resultset;
	}
	
	FUNCTION ms_escape_string($raw){
		$raw = STR_REPLACE("'", "''", $raw);
		$raw = STR_REPLACE("\0","[NULL]", $raw);
		RETURN $raw;
	}
?>

Open in new window

CipherIS

ASKER
Below is the error in error.log

[Fri Dec 07 14:47:51.129733 2018] [:error] [pid xxxxx] [client xxx.xxx.xxx.xxx:xxxxx] PHP Notice:  Undefined variable: query in /home/webserver/www/xxx.xxxx.com/includes/conn.php on line 2

php_errors.log is empty
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
CipherIS

ASKER
After some troubleshooting I need to find how "p_key is set.  This seems to be the prob.
IF($_SERVER["REQUEST_METHOD"] == "POST"){
		$p_key = (ISSET($_POST["p_key"]) ? $_POST["p_key"] : $_COOKIE["p_key"]);

Open in new window

lenamtl

Hi,

check if you have the cookies values
Chrome right click inspect / application / cookies
CipherIS

ASKER
Thanks.  

I figured out the issue.  Restarted Apache and that solved it.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
CipherIS

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Ryan Chong

I figured out the issue.  Restarted Apache and that solved it.

it looks to me that it was a condition which probably not handling well and restarted the Apache server restore the state back to origin.

you may observe if this same error will reoccur in the future.