0

I'm having a hard time pushing a ton of data from SQL into an HTML table.

I have a chartbuilder.php file which has this code in it:

$pieQueryCreates = "SELECT Count(Action) FROM dbo.File_System_Profiles WHERE Action like '%create%' AND (DATEDIFF(day,TimeOccurred,getdate()) between 0 and 30)";
$pieQueryResult  = sqlsrv_query($conn, $sQuery);

if ($pieResultCreates === false) {
    die(sqlsrv_errors(SQLSRV_ERR_ERRORS));
}
$pieQueryCreateCount = sqlsrv_fetch_array($pieQueryResult);

This is my HTML:

<?php require '/php/chartbuilder.php'; ?>
<?php echo $pieQueryCreateCount; ?>

And I get nothing from the echo. If I echo $pieQuerCreateCount[0] I still get nothing from the echo. If I echo $pieQueryResult I get a Resource ID #4 which isn't a surprise but leads me to believe the query is being completed. Copy/pasting my SELECT statement into SQL returns the count as expected in a single row/column with no name.

I just need to be able to access that one value and nothing I am doing seems to work, I've read through the documentation for sqlsrv_fetch_array this multiple times and tried a variety of changes based on their examples to find no success.

Joshua
  • 5,032
  • 2
  • 29
  • 45
  • Have you tried using `print_r` instead of `echo`? – FirstOne Jun 07 '17 at 19:18
  • 1
    what is `$sQuery`? shouldn't you use your `$pieQueryCreates` instead? – rbock Jun 07 '17 at 19:19
  • You should be getting errors both from trying to echo an array and using the wrong variable, as mentioned by @rbock. Please, [get error to display](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). – FirstOne Jun 07 '17 at 19:27
  • BTW: The statement `if($pieResultCreates === false){` is never true, since you used the wrong variable (should be using `pieQueryResult`) – FirstOne Jun 07 '17 at 19:29
  • Good eye rbock, didn't resolve my problem but was definitely a problem. – B.Breckenridge Jun 07 '17 at 19:32
  • FirstOne you got me closer with the print_r, thank you for your assistance. I'll work on the error display, didn't want you to think I was ignoring it. – B.Breckenridge Jun 07 '17 at 19:38

1 Answers1

0

In your chartbuilder.php file generate an array with query results.

$pieQueryCreates = "SELECT Count(Action) FROM dbo.File_System_Profiles WHERE Action like '%create%' AND (DATEDIFF(day,TimeOccurred,getdate()) between 0 and 30)";
$pieQueryResult = sqlsrv_query($conn, $pieQueryCreates );
if($pieQueryResult === false){
    die( print_r( sqlsrv_errors(), true));
}
while( $row = sqlsrv_fetch_array($pieQueryResult,SQLSRV_FETCH_NUMERIC) ) {
      $results[] = $row;
}

print_r($results); // check the array

Now in html display $results array in the way you like. echo $results[0][0];

Ravinder Reddy
  • 3,869
  • 1
  • 13
  • 22