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.
LVL 2
CipherISAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongSoftware Team Lead, ex-Business Systems Analyst , ex-Senior Application EngineerCommented:
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?
rgranlundCommented:
Can you post your query? and use var_dump on your query?
CipherISAuthor Commented:
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?
SolarWinds® Network Configuration Manager (NCM)

SolarWinds® Network Configuration Manager brings structure and peace of mind to configuration management. Bulk config deployment, automatic backups, change detection, vulnerability assessments, and config change templates reduce the time needed for repetitive tasks.

CipherISAuthor Commented:
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

rgranlundCommented:
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

lenamtlCommented:
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
CipherISAuthor Commented:
@rgranlund - I get a NULL

@lenamtl - you want me to run that script?
rgranlundCommented:
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.
lenamtlCommented:
Hi,
first check your web server logs
PHP logs and MySQL logs for error
CipherISAuthor Commented:
Where do you check the PHP logs on Ubuntu?
CipherISAuthor Commented:
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
CipherISAuthor Commented:
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

CipherISAuthor Commented:
I'm looking in the Ubuntu server /var/log/apache2/ and don't see "errors.log" or "php_errors.log"
CipherISAuthor Commented:
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

CipherISAuthor Commented:
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
CipherISAuthor Commented:
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

lenamtlCommented:
Hi,

check if you have the cookies values
Chrome right click inspect / application / cookies
CipherISAuthor Commented:
Thanks.  

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ryan ChongSoftware Team Lead, ex-Business Systems Analyst , ex-Senior Application EngineerCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.