Why am I not getting anything from my fetchAll? Why, why, why?

Here's my code:

for($y=1; $y<=84; $y++)
	{
	$statement = $mssql_pdo->prepare("select * from gant_table where DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'");
	echo "select * from gant_table where DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'.'<br>";
	$statement->execute();
	$row_count = $statement->fetchColumn(); 
		
		if($row_count>1)
		{
		$results=$statement->fetchAll(PDO::FETCH_ASSOC);
		echo "something";
			foreach($results as $row) 
			{
				echo $row['TotalCellSpan'];
				if($row['TotalCellSpan']>1)
				{
					echo "right here";

Open in new window


The bottom line is that I can utilize "echo" to track where I've got some data being seen by my query, but then when I go to "fetchAll" I don't get any data. It's seemingly there, but how do I get the goodness to show up on my screen?

Here's a screen shot of my misery...

screenshot
brucegustPHP DeveloperAsked:
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.

Marco GasiFreelancerCommented:
Hi brucegust.
What with var_dump?

foreach($results as $row) 
{
      var_dump($row);

Open in new window

0
Julian HansenCommented:
I suspect this is not doing what you expect
$row_count = $statement->fetchColumn(); 

Open in new window

The above code is going to fetch the value of column 0 in the next result in the result set.

If you want to check for > 1 rows returned use rowCount()
for($y=1; $y<=84; $y++) {
  $statement = $mssql_pdo->prepare("select * from gant_table where DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'");
  echo "select * from gant_table where DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'.'<br>";
  $statement->execute();
  $row_count = $statement->fetchColumn(); 
  
  // ALTERNATIVE CHECK FOR ROW COUNT  
  if($statement->rowCount() > 1) {
    $results=$statement->fetchAll(PDO::FETCH_ASSOC);
    echo "something"; 
    foreach($results as $row) {
      echo $row['TotalCellSpan'];
      if($row['TotalCellSpan']>1) {
        echo "right here";

Open in new window

0
brucegustPHP DeveloperAuthor Commented:
Hey, guys!

Here's what I did:

      for($y=1; $y<=84; $y++)
      {
      $statement = $mssql_pdo->prepare("select * from gant_table where DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'");
      echo "select * from gant_table where DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'.'<br>";
      $statement->execute();
      $results=$statement->fetchAll(PDO::FETCH_ASSOC);            
             if($statement->rowCount() > 1)
             {
            
            echo "something";
            foreach($results as $row)
            {
            var_dump($row);
              echo $row['TotalCellSpan'];
                  if($row['TotalCellSpan']>1)
                  {
                        echo "right here";

No data - nothing "echoed." What else do you think it could be?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Marco GasiFreelancerCommented:
So try to see what happens with

echo "<pre>";
var_dump($results);
echo "</pre>";

Open in new window

0
Ray PaseurCommented:
Could be that $statement->execute(); failed.  Your script can and should test the return values from these functions, then you can know whether the function worked, or if it failed, you can display the error information.

Man page on the execute() method:
http://php.net/manual/en/pdostatement.execute.php

Examples showing how to catch these sorts of errors are shown in the PDO examples here:
http://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
Ray PaseurCommented:
Sidebar note... This may not be causing you a debugging cycle yet but it's fraught with danger.
 $statement = $mssql_pdo->prepare("select * from gant_table where DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'");
      echo "select * from gant_table where DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'.'<br>";
      $statement->execute();

Open in new window

The reason this is dangerous is that your echo statement looks like it is producing an accurate representation of the SELECT query, but it's actually deriving its content independently from the query.  You could change the query and you would have to remember to change the echo statement, too.  That's a risky design because it violates the DRY principle.  

Instead you might want to write the code more like this:
$sql = "SELECT * FROM gant_table WHERE DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'";
$statement = $mssql_pdo->prepare($sql);
echo "$sql<br>";
$res = $statement->execute();
if (!$res) /* DETECTED FAILURE */

Open in new window

0
brucegustPHP DeveloperAuthor Commented:
As always, gentlemen, I so appreciate your time.

Ray, I will incorporate your suggestion in a moment, but I want to get y'all a comprehensive snapshot of everything that's going on, in case there was a mistake made previously that's affecting what I've shown you thus far.

Here's the whole page from top to bottom:

<?php
//ERROR REPORTING
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL); 
require_once('projectpageclass.php');
require_once('model_class.php');

$new_page= new ProjectPage;
$page_title="South Area Network Development Project List";
$new_date=new DateCalc;

$current_row="";

if(!isset($_GET['year']))
{
	$this_year=date("Y");
}
else
{
	$this_year=$_GET['year'];
}
$the_date=$this_year;
$the_date.="-01-01";

$year_one_calc = date('c', strtotime("$the_date - 1 year"));
$year_one=date("Y", strtotime($year_one_calc)); 

$year_two=$this_year;

$year_three_calc = date('c', strtotime("$the_date + 1 year"));
$year_three=date("Y", strtotime($year_three_calc)); 

$dayNumber = date("z") + 1; 
//echo $dayNumber;

//this code is a temporary thing in order to ensure we're grabbing from live data
//include("mssql_db_cred.php");
include("carter_pdo.php");
$mssql_pdo = new PDO('mysql:host=localhost; dbname=jimmy', 'root', ''); 
$mssql_pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
if(!$mssql_pdo)
{
	echo "no connection";
}
else
{
	//echo "good to go";
}

include("gant_header.php");

$number_of_cells=1;

$statement_1 = $mssql_pdo->prepare("select DataDisplay_Row from gant_table order by DataDisplay_Row DESC LIMIT 1");
$statement_1->execute();
$results_1 = $statement_1->fetchAll(PDO::FETCH_ASSOC);

foreach($results_1 as $row_1)
{
	 $number_of_rows=$row_1['DataDisplay_Row'];
}

for($i=1; $i<=$number_of_rows; $i++)
{
$body.="<tr>";
	for($y=1; $y<=84; $y++)
	{
	$statement = $mssql_pdo->prepare("select * from gant_table where DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'");
	echo "select * from gant_table where DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'.'<br>";
	$statement->execute();
	$row_count = $statement->fetchColumn(); 
		
		if($row_count>1)
		{
			echo "yes";
			$results=$statement->fetchAll(PDO::FETCH_ASSOC);
			echo "<pre>";
			var_dump($results);
			echo "</pre>";
			foreach($results as $row) 
			{                                         
				if($row['TotalCellSpan']>1)
				{
					if($row['TotalCellSpan']>=84)
					{
						$the_colspan=84;
					}
					else
					{
						$the_colspan=$row['TotalCellSpan'];
					}
				$body.="<td style=\"background-image:";
					if($row['str_Tool']=="South Area Website (PHP)")
					{
						$body.="url(images/cell_background_green.jpg);";
					}
						elseif($row['str_Tool']=="Capital Budget Tool")
						{
							$body.="url(images/cell_background_purple.jpg);";
						}
						elseif($row['str_Tool']=="Expense Budget Tool")
						{
							$body.="url(images/cell_background_violet.jpg);";
						}
						elseif($row['str_Tool']=="Real Estate Tracker")
						{
							$body.="url(images/cell_background_blue.jpg);";
						}
						elseif($row['str_Tool']=="CARRIE")
						{
							$body.="url(images/cell_background_aqua.jpg);";
						}
						elseif($row['str_Tool']=="360 Survey")
						{
							$body.="url(images/cell_background_dark_red.jpg);";
						}
						elseif($row['str_Tool']=="Regional Budget Tools")
						{
							$body.="url(images/cell_background_grey.jpg);";
						}
						elseif($row['str_Tool']=="Dev Tools")
						{
							$body.="url(images/cell_background_black.jpg);";
						}
						elseif($row['str_Tool']=="SA Portal (MVC)")
						{
							$body.="url(images/cell_background_orange.jpg);";
						}
						elseif($row['str_Tool']=="LTE Offload Tool")
						{
							$body.="url(images/cell_background_lime.jpg);";
						}
						elseif($row['str_Tool']=="Sys Perf Exec Dashboard")
						{
							$body.="url(images/cell_background_sweet_blue.jpg);";
						}
						elseif($row['str_Tool']=="CARTN Inbuilding Mailbox")
						{
							$body.="url(images/cell_background_hot_red.jpg);";
						}
						elseif($row['str_Tool']=="Infrastructure")
						{
							$body.="url(images/cell_background_scum.jpg);";
						}
						elseif($row['str_Tool']=="Cruncher Box")
						{
							$body.="url(images/cell_background_smoke.jpg);";
						}
					else
					{
						$body.="url(images/cell_background_green.jpg)";
					}
				$body.=" background-repeat:repeat-x; font-size:10pt; color:#ffffff; padding-top:3px;\" colspan=\"";
				$body.=$the_colspan;
				$body.="\">";
				$body.=$row['DataDisplay_Row'];
				$body.="</td>";
				$i=$i+($the_colspan-1);
				$the_colspan=0;
				}
				else
				{
					$body.="<td></td>";
				}
			}
		}
		else
		{
			if($i %7 !=0)
			{
				$body.="<td>&nbsp;</td>";
			}
			else
			{
				$body.="<td class=\"right_cell\"></td>";
			}
			if($i==84)
			{
				$body.="</tr>";
			}
		}
	}	
}	
				
$body.="
	<!-- this last row is just for aesthetics so there's a little room at the bottom of the page. It also gurantees a certain with for all of the \"daily\" cells -->";
	include("spacer_single.php");
	$body.="
</table><br><br>";
		
$new_page->setBody($body);

echo $new_page->display();

?>

Open in new window


Marco - line 78-80 gave me this:

array[0] {

}

Julian - one of the things that prompted me to include the whole page was something I've struggled with in that I wasn't always able to get my select statement to increment correctly. In other words, I should be getting "echo "select * from gant_table where DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'.'<br>";" a total of 1428 times ($i goes up to 17 [based on what I'm getting on line 61] and $y goes up to 84). I noticed a mistake on line 72 where I called the variable $number_of_rows instead of $row_count. $number_of_rows was already used on line 61, hence the code didn't repeat like it should've.  When I used $row_count, I got a more accurate response, as far as there being data present. But I'm still not seeing that data displayed...

Ray - standby...
0
Marco GasiFreelancerCommented:
Ok, brucegust. That means that your query fails and returns an empty array. But I can't guess why...
0
brucegustPHP DeveloperAuthor Commented:
Ray, here's what I did:

$sql = "SELECT * FROM gant_table WHERE DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'";
	$statement = $mssql_pdo->prepare($sql);
	echo "$sql<br>";
	$res = $statement->execute();
	if (!$res) {
	echo "something went south";
	}	/* DETECTED FAILURE */                                          
	/*$statement = $mssql_pdo->prepare("select * from gant_table where DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'");
	echo "select * from gant_table where DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'.'<br>";
	$statement->execute();*/

Open in new window


I didn't get an error, but here's what the page looks like right now:

screenshot
Is it possible that my result isn't an array? It seems like I read somewhere the difference between fetchAll and fetch was the nature of the data being retrieved...those are vowel movements, but I'm trying to figure this out.
<?php
//ERROR REPORTING
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL); 
require_once('projectpageclass.php');
require_once('model_class.php');

$new_page= new ProjectPage;
$page_title="South Area Network Development Project List";
$new_date=new DateCalc;

$current_row="";

if(!isset($_GET['year']))
{
	$this_year=date("Y");
}
else
{
	$this_year=$_GET['year'];
}
$the_date=$this_year;
$the_date.="-01-01";

$year_one_calc = date('c', strtotime("$the_date - 1 year"));
$year_one=date("Y", strtotime($year_one_calc)); 

$year_two=$this_year;

$year_three_calc = date('c', strtotime("$the_date + 1 year"));
$year_three=date("Y", strtotime($year_three_calc)); 

$dayNumber = date("z") + 1; 
//echo $dayNumber;

//this code is a temporary thing in order to ensure we're grabbing from live data
//include("mssql_db_cred.php");
include("carter_pdo.php");
$mssql_pdo = new PDO('mysql:host=localhost; dbname=jimmy', 'root', ''); 
$mssql_pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
if(!$mssql_pdo)
{
	echo "no connection";
}
else
{
	//echo "good to go";
}

include("gant_header.php");

$number_of_cells=1;

$statement_1 = $mssql_pdo->prepare("select DataDisplay_Row from gant_table order by DataDisplay_Row DESC LIMIT 1");
$statement_1->execute();
$results_1 = $statement_1->fetchAll(PDO::FETCH_ASSOC);

foreach($results_1 as $row_1)
{
	 $number_of_rows=$row_1['DataDisplay_Row'];
}

for($i=1; $i<=$number_of_rows; $i++)
{
$body.="<tr>";
	for($y=1; $y<=84; $y++)
	{
	$statement = $mssql_pdo->prepare("select * from gant_table where DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'");
	echo "select * from gant_table where DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'.'<br>";
	$statement->execute();
	$row_count = $statement->fetchColumn(); 
		
		if($row_count>1)
		{
			echo "yes";
			$results=$statement->fetchAll(PDO::FETCH_ASSOC);
			echo "<pre>";
			var_dump($results);
			echo "</pre>";
			foreach($results as $row) 
			{                                         
				if($row['TotalCellSpan']>1)
				{
					if($row['TotalCellSpan']>=84)
					{
						$the_colspan=84;
					}
					else
					{
						$the_colspan=$row['TotalCellSpan'];
					}
				$body.="<td style=\"background-image:";
					if($row['str_Tool']=="South Area Website (PHP)")
					{
						$body.="url(images/cell_background_green.jpg);";
					}
						elseif($row['str_Tool']=="Capital Budget Tool")
						{
							$body.="url(images/cell_background_purple.jpg);";
						}
						elseif($row['str_Tool']=="Expense Budget Tool")
						{
							$body.="url(images/cell_background_violet.jpg);";
						}
						elseif($row['str_Tool']=="Real Estate Tracker")
						{
							$body.="url(images/cell_background_blue.jpg);";
						}
						elseif($row['str_Tool']=="CARRIE")
						{
							$body.="url(images/cell_background_aqua.jpg);";
						}
						elseif($row['str_Tool']=="360 Survey")
						{
							$body.="url(images/cell_background_dark_red.jpg);";
						}
						elseif($row['str_Tool']=="Regional Budget Tools")
						{
							$body.="url(images/cell_background_grey.jpg);";
						}
						elseif($row['str_Tool']=="Dev Tools")
						{
							$body.="url(images/cell_background_black.jpg);";
						}
						elseif($row['str_Tool']=="SA Portal (MVC)")
						{
							$body.="url(images/cell_background_orange.jpg);";
						}
						elseif($row['str_Tool']=="LTE Offload Tool")
						{
							$body.="url(images/cell_background_lime.jpg);";
						}
						elseif($row['str_Tool']=="Sys Perf Exec Dashboard")
						{
							$body.="url(images/cell_background_sweet_blue.jpg);";
						}
						elseif($row['str_Tool']=="CARTN Inbuilding Mailbox")
						{
							$body.="url(images/cell_background_hot_red.jpg);";
						}
						elseif($row['str_Tool']=="Infrastructure")
						{
							$body.="url(images/cell_background_scum.jpg);";
						}
						elseif($row['str_Tool']=="Cruncher Box")
						{
							$body.="url(images/cell_background_smoke.jpg);";
						}
					else
					{
						$body.="url(images/cell_background_green.jpg)";
					}
				$body.=" background-repeat:repeat-x; font-size:10pt; color:#ffffff; padding-top:3px;\" colspan=\"";
				$body.=$the_colspan;
				$body.="\">";
				$body.=$row['DataDisplay_Row'];
				$body.="</td>";
				$i=$i+($the_colspan-1);
				$the_colspan=0;
				}
				else
				{
					$body.="<td></td>";
				}
			}
		}
		else
		{
			if($i %7 !=0)
			{
				$body.="<td>&nbsp;</td>";
			}
			else
			{
				$body.="<td class=\"right_cell\"></td>";
			}
			if($i==84)
			{
				$body.="</tr>";
			}
		}
	}	
}	
				
$body.="
	<!-- this last row is just for aesthetics so there's a little room at the bottom of the page. It also gurantees a certain with for all of the \"daily\" cells -->";
	include("spacer_single.php");
	$body.="
</table><br><br>";
		
$new_page->setBody($body);

echo $new_page->display();

?>

Open in new window

0
Julian HansenCommented:
If the query fails then why did you get a "something" in your output as per your original screenshot - to get that far it would have had to get past the if statement.
Secondly, why check for the rowCount at all - simply do a fetchAll - if there are no results fetchAll will return an empty array - so if you are doing a foreach over the returned array - it will drop through if no rows returned.

Going back to your code - I am intrigued by this snippet
foreach($results_1 as $row_1) {
  $number_of_rows=$row_1['DataDisplay_Row'];
}

Open in new window

Some code to spin the CPU's wheels? Going through every result and coming out with the value of the last row?
What is it you are trying to achieve with this?
0
Julian HansenCommented:
The following is derived from your code. The commented out section in the middle was used to populate a test table defined like so
CREATE TABLE `gant_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `DataDisplay_Row` int(11) DEFAULT NULL,
  `DataDisplay_StartCell` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) 

Open in new window

Running this code produces results dumped by the print_r statement
<?php
$mssql_pdo = new PDO('mysql:host=localhost; dbname=ee', 'user', 'password'); 
$mssql_pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
if(!$mssql_pdo)
{
  echo "no connection";
}
else
{
  //echo "good to go";
}
/*
CODE TO POPULATE TABLE
$number_of_rows = 84;
for($i=1; $i<=$number_of_rows; $i++)
{
  for($y=1; $y<=84; $y++) {
    $statement = $mssql_pdo->prepare("insert into gant_table (DataDisplay_Row,DataDisplay_StartCell) values ('$i','$y')");
    $statement->execute();
  }  
}
die();
*/
$number_of_cells=1;

$statement_1 = $mssql_pdo->prepare("select DataDisplay_Row from gant_table order by DataDisplay_Row DESC LIMIT 1");
$statement_1->execute();
$results_1 = $statement_1->fetchAll(PDO::FETCH_ASSOC);
// WHEEL SPIN - NEEDS A LOOK 
foreach($results_1 as $row_1)
{
  $number_of_rows=$row_1['DataDisplay_Row'];
}
echo "NOR: {$number_of_rows}<br/>";
for($i=1; $i<=$number_of_rows; $i++)
{
  for($y=1; $y<=84; $y++) {
    $statement = $mssql_pdo->prepare("select * from gant_table where DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'");

    $statement->execute();
    $results=$statement->fetchAll(PDO::FETCH_ASSOC);
    echo "<pre>" . print_r($results, true) . "</pre>";
  }  
}
?>

Open in new window

0

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
brucegustPHP DeveloperAuthor Commented:
I'm building a GANT calendar. I've already built it using MySQL and a "procedural" rich approach to my code. Here's the way it looks in production:

gant calendar
I'm converting what I did to a PDO dynamic. To answer you question about:

foreach($results_1 as $row_1) {
  $number_of_rows=$row_1['DataDisplay_Row'];
}

The way that I've got things set up is the code iterates according to a specified number of rows. The row number and the cell number determines how the data is displayed.

Having built this once, the logic is sound enough, but the frustrating part right now is why am I not seeing any data, when there is data to be retrieved and, as you say, the query doesn't appear to be failing.
0
brucegustPHP DeveloperAuthor Commented:
Just saw your other post, Julian. I'm kicking the tires now...
0
brucegustPHP DeveloperAuthor Commented:
Julian!

This works:

for($i=1; $i<=$number_of_rows; $i++)
{
  for($y=1; $y<=84; $y++)
  {
    $statement = $mssql_pdo->prepare("select * from gant_table where DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'");
    $statement->execute();
    $results=$statement->fetchAll(PDO::FETCH_ASSOC);
    //echo "<pre>" . print_r($results, true) . "</pre>";
      foreach($results as $row)
      {
            echo $row['str_Tool'];
      }                  
  }  
}

But now, can you show me how to implement something that allows me to determine whether or not there's any data to be retrieved? This goes back to my $row_count earlier. Because if I do this:

for($i=1; $i<=$number_of_rows; $i++)
{
  for($y=1; $y<=84; $y++)
  {
    $statement = $mssql_pdo->prepare("select * from gant_table where DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'");
    $statement->execute();
      $row_count = $statement->fetchColumn();
    $results=$statement->fetchAll(PDO::FETCH_ASSOC);
    //echo "<pre>" . print_r($results, true) . "</pre>";
      foreach($results as $row)
      {
            if($row_count>0)
            {
                  echo $row['str_Tool'];
            }
            else
            {
                  echo "no";
            }
      }                  
  }  
}

I get nothing.
0
Julian HansenCommented:
Lets go back a step to this
foreach($results_1 as $row_1) {
  $number_of_rows=$row_1['DataDisplay_Row'];
}

Open in new window

If we examine this statement - your code is fetching 0 - N rows - and looping through every one of them setting a variable - each time overwriting the previous value such that $number_of_rows is set to the value in the last row.

Why are you doing this as opposed to say just fetching all the rows and iterating through them? In other words

$statement = $mssql_pdo->prepare("select * from gant_table");
$statement->execute();
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
foreach($results as $res) {
//    process row here
}

Open in new window

0
brucegustPHP DeveloperAuthor Commented:
Below is a snapshot of the way the data appears in the database:

another screenshot
The events have to be positioned relative to one another and not every event occupies its own row. The DataDisplay_Row value is not the number of rows in the database. Rather, it's the number of rows that will be displayed on the screen for the user based on the way each of the event's start date overlaps (or not) with another.
0
brucegustPHP DeveloperAuthor Commented:
Bottom line: I need the max value of the DataDisplay_Row. I trust your judgement. If there's another way to do it that doesn't eat up a lot of digital gasoline, I'm definitely open...
0
brucegustPHP DeveloperAuthor Commented:
And I still need that if statement - cannot figure that out!

Thanks for your time!
0
Julian HansenCommented:
I need the max value of the DataDisplay_Row.
Ok, except your current code does not find that - it may give the right result by accident because the last value happens to have the max value in it - but if the data changes that code will give the wrong value.

But now, can you show me how to implement something that allows me to determine whether or not there's any data to be retrieved?
This is tripping you up
$row_count = $statement->fetchColumn();

Open in new window

fetchColumn advances the record pointer so you loose the record.
This is probably why you were getting an empty return
You want to do
$row_count = $statement->rowCount();

Open in new window

0
Julian HansenCommented:
To find the max DataDisplay_Row
SELECT MAX(DataDisplay_Row) AS MaxRow FROM gant_table

Open in new window

0
brucegustPHP DeveloperAuthor Commented:
We are poised on the threshold of greatness, but there's still one "piece" that's alluding me.

Here's what I've got:

 

The MaxRow thing is working perfectly, but the $row_count thing is still less than what I need. Check it out:

$statement_1 = $mssql_pdo->prepare("SELECT MAX(DataDisplay_Row) AS MaxRow FROM gant_table");
$statement_1->execute();
$results_1 = $statement_1->fetchAll(PDO::FETCH_ASSOC);

foreach($results_1 as $row_1)
{
  $number_of_rows=$row_1['MaxRow'];
}

for($i=1; $i<=$number_of_rows; $i++)
{
  for($y=1; $y<=84; $y++) 
  {
    $statement = $mssql_pdo->prepare("select * from gant_table where DataDisplay_Row='$i' AND DataDisplay_StartCell='$y'");
    $statement->execute();
	$row_count = $statement->rowCount();
    $results=$statement->fetchAll(PDO::FETCH_ASSOC);
    //echo "<pre>" . print_r($results, true) . "</pre>";
	foreach($results as $row) 
	{ 
		if($row_count>0)
		{
			echo "yes";
		}
		else
		{
			echo "no";
		}
		
	}			
  }  
}

Open in new window


I need to be able to do something if the $row_count is not greater than 0. Right now, it doesn't seem to register anything at all and I don't know why.

Here's what my screen looks like when I run what I've got documented above:

yesyesyes
How can I get my code to say "no" when there's no value?
0
Julian HansenCommented:
On reflection, while this may be ok for MySQL, when dealing with PDO portability is a factor.
If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.
http://php.net/manual/en/pdostatement.rowcount.php

Try this instead - drop the line completely
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
if (count($results) > 0) {

Open in new window

0
Ray PaseurCommented:
I don't think there is any hope for me to get the code right without any test data, but I can give a couple of ideas that you may be able to use here and in the future.

First, look very closely at the nature of PHP type-coercion and duck-typing.  Many things can be "falsy" in that these data constructs can give you an evaluation result that looks like PHP false.  Some of them:

zero // NUMERIC VALUE
3-3 // COMPUTED NUMERIC VALUE
'0' // STRING CONTAINING THE NUMBER ZERO
'a' // STRING CONTAINING LETTERS ONLY, IN SOME CONTEXTS
an unset variable // OR NULL
an empty() array // OR EMPTY OBJECT

The PHP if() statement and the iterators use true/false evaluation to control program logic.

For this reason, many programming standards today are recommend identicality testing with three equal signs ===, not equality testing with two equal signs ==.  Identicality testing forces you to think clearly about the exact data types and values, and is thereby less prone to error.  PHP is by no means the only language with this problem, but it has a larger population of inexperienced programmers than, say, Java or Ruby, so the problem is exacerbated here.  Some PHP links you must understand if you're going to write PHP applications:
http://php.net/manual/en/language.expressions.php
http://php.net/manual/en/language.operators.comparison.php
http://php.net/manual/en/types.comparisons.php
http://php.net/manual/en/language.types.type-juggling.php
http://php.net/manual/en/language.types.string.php#language.types.string.conversion

At this point we have reached the question, "What is causing my logical comparisons to go awry?"  The only sure way to answer that question is to look at the data.  You can do this with var_dump().  You might want to take some time learning to use var_dump(), and you might want to insert it into your PHP scripts in places that can help you understand the contents of your variables.
http://php.net/manual/en/function.var-dump.php
0
brucegustPHP DeveloperAuthor Commented:
Julian, I'm glad you included that last post because I had seen that in the PHP documentation. I was able to come up with a workaround last nite, but I've since incorporated your most recent suggestion (count($results)) and it's working great.

Ray, you're right. Perhaps it's because PDO is more particular about what constitutes a legitimate result, but in the midst of doing var_dump, I could see why the system was recording a "result," though it wasn't "data." As always, I appreciate you taking the time to explain the "why" and not just the "what."

Thanks, guys!
0
Julian HansenCommented:
You are welcome.
0
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
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.