We help IT Professionals succeed at work.

PHP - No data displaing from SQL Server database

CipherIS
CipherIS asked
on
130 Views
Last Modified: 2018-12-13
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.
Comment
Watch Question

CERTIFIED EXPERT

Commented:
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?
Can you post your query? and use var_dump on your query?

Author

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?

Author

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

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

CERTIFIED EXPERT

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

Author

Commented:
@rgranlund - I get a NULL

@lenamtl - you want me to run that script?
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.
CERTIFIED EXPERT

Commented:
Hi,
first check your web server logs
PHP logs and MySQL logs for error

Author

Commented:
Where do you check the PHP logs on Ubuntu?

Author

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

Author

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

Author

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

Author

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

Author

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

Author

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

CERTIFIED EXPERT

Commented:
Hi,

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

Author

Commented:
Thanks.  

I figured out the issue.  Restarted Apache and that solved it.
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions