Jump to content

PHP Code mysteriously blank


bluethundr

Recommended Posts

Hey Guys

 

 

Thanks for your consistent and excellent help. I am really trying to master PHP connecting to MySQL. Every once in a while tho I run into a script that mysteriously refuses to be useful and I could use some help. That's why I have come to this board again, it's excellent.

 

That said.. I wrote a very simple script that is meant to access a template and render information from am MySQL database.

 

 

It should be noted that:

 

error_reporting E_ALL

display_errors = 1

 

is set in my php.ini file.

 

Here is the code. For some strange reason the page it renders is completely blank.

example.7-5.php

<?php
     require_once "HTML/Template/IT.php";
     require 'db.inc';
     
     if (!($connection = @ mysql_connect($hostname, $username, $password)))
         die("Cannot connect");
         
     if (!(mysql_select_db($databaseName, $connection)))
        showerror();
     
     if (!$regionresult = @ mysql_query ("SELECT * FROM region LIMIT 10", 
                                          $connection)))
        showerror();
        
        
     $template = NEW HTML_TEMPLATE_IT("./templates");
     $template->loadTemplatefile("example.7-4.tpl",true, true);
     
     while ($regionrow = mysql_fetch_array($regionresult));
     {
         $template->setCurrentBlock("REGION");
         $template->setVariable("REGIONNAME", $regionrow["region_name"]]);
         
          if(!($wineryresult = 
              @ mysql_query ("SELECT * FROM winery
                              WHERE region_id = {$regionrow["$region_id"]}",
                              $connection)))
                              
              showerror();
              
           while ($wineryrow = mysql_fetch_array($wineryresult))
           {
               $template->setCurrentBlock("WINERY");
               $template->setVariable("WINERYNAME", $wineryrow["winery_name"]);
               $tempalte->parseCurrentBlock();
               
           }
           $template->setCurrentBlock("REGION");
           $template->parseCurrentBlock();
     }
     $template->show();
?>

 

This code accesses a db include file that has all the login information:

 

db.inc

<?php
   // This file is the same as example 6-7, but includes mysqlclean() and shellclean() 

   $hostName = "localhost";
   $databaseName = "winestore";
   $username = "thatguy";
   $password = "shhhh";   

   function showerror()
   {
      die("Error " . mysql_errno() . " : " . mysql_error());
   }

   function mysqlclean($array, $index, $maxlength, $connection)
   {
     if (isset($array["{$index}"]))
     {
        $input = substr($array["{$index}"], 0, $maxlength);
        $input = mysql_real_escape_string($input, $connection);
        return ($input);
     }
     return NULL;
   }

   function shellclean($array, $index, $maxlength)
   {
     if (isset($array["{$index}"]))
     {
       $input = substr($array["{$index}"], 0, $maxlength);
       $input = EscapeShellArg($input);
       return ($input);
     }
     return NULL;
   }
?>

 

this is the simple template file living in the ./templates directory one directory down from my html docs directory

 

example.7-4.tpl

<!DOCTYPE HTML PUBLIC
               "-//W3C//DTD HTML 4.01 Transitional//EN"
               "http://www.w3.org/TR/html401/loose.dtd">
<html>
<head>
     <meta http-equiv="Content Type" content="text/html; charset-iso-8859-1">
     <title>Regions and Wineries</title>
<body>
<ul>
<!-- BEGIN REGION -->
<li>Region: {REGIONNAME}
<ul>
<! -- BEGIN WINERY -->
<li>{WINERYNAME}.
<! -- END WINERY -- >
</ul>
<! -- END REGION -- >
</ul>
</body>
</html>

 

And for some reason the page, even tho errors are turned on turn up blank. Any thoughts?  :wtf:

 

Link to comment
Share on other sites

I'm not sure why you're errors aren't reporting properly, but a blank page is usually indicative of parse errors.  A parse error is when you make a mistake with the PHP syntax, such as adding extra brackets or forgetting semicolons.

 

I was able to find the syntax errors in your source files by running PHP with the -l flag from the command line.

 

If you open a command prompt, you can run PHP directly from the command line.

 

roopurt18> php -v
PHP 5.2.11 (cli) (built: Sep 16 2009 19:39:46)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
    with DBG v3.8.6, (C) 2000,2009, by Dmitri Dmitrienko
    with NuSphere PhpExpress v2.0.3, Copyright (c) 2002-2009 NuSphere Corp, by D
mitri Dmitrienko

 

If you use the -l flag, PHP will check your file for syntax errors.

roopurt18> cd \path\to\my\files
roopurt18@\path\to\my\files> php -l junk.php

Parse error: syntax error, unexpected ')' in junk.php on line 12
Errors parsing junk.php

 

By doing this, I found the following in your code:

 

// line 12 has an extra close-paren
if (!$regionresult = @ mysql_query ("SELECT * FROM region LIMIT 10", 
                                          $connection))) // <-- RIGHT HERE, REMOVE ONE OF THE )

 

// line 21 has an extra ]
$template->setVariable("REGIONNAME", $regionrow["region_name"]]); // <-- remove the extra ] in "region_name"]]

Link to comment
Share on other sites

awesome tip trying it on the command line! this trick will come in very handy.

 

I checked the syntax errors, got rid of them.

 

but now it's reporting an SQL error and I can't seem to find it. would you mind having a look?

 

this is the error I get when I load the page now...

 

Error 1064 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2

 

I checked line 2 and the whole file and I can't find the SQL error.  :shrug:

 

Thanks

Link to comment
Share on other sites

<?php
     require_once "HTML/Template/IT.php";
     require 'db.inc';
     
     if (!($connection = @ mysql_connect($hostname, $username, $password)))
         die("Cannot connect");
         
     if (!(mysql_select_db($databaseName, $connection)))
        showerror();
     
     if (!$regionresult = @ mysql_query ("SELECT * FROM region LIMIT 10", 
                                          $connection))
        showerror();
        
        
     $template = NEW HTML_TEMPLATE_IT("./templates");
     $template->loadTemplatefile("example.7-4.tpl",true, true);
     
     while ($regionrow = mysql_fetch_array($regionresult));
     {
         $template->setCurrentBlock("REGION");
         $template->setVariable("REGIONNAME", $regionrow["region_name"]);
         
          //
          // I'M GUESSING THIS IS WHERE YOU'RE ERROR IS AT.
          // IT'S HELPFUL TO ECHO THE SQL STATEMENT AND LOOK FOR PROBLEMS.
          //
          // I'VE ALSO REFACTORED YOUR CODE, AS IT'S READABILITY WAS POOR IMO.
          $sql = "
              SELECT * 
              FROM winery
              WHERE region_id = {$regionrow[$region_id]}
              ";
          echo $sql; // CHECK THE OUTPUT, DOES IT LOOK RIGHT?
          $wineryresult = @mysql_query ( $sql, $connection);
          if( !$wineryresult ) {
              showerror();
          }else{
              
               while ($wineryrow = mysql_fetch_array($wineryresult))
               {
                   $template->setCurrentBlock("WINERY");
                   $template->setVariable("WINERYNAME", $wineryrow["winery_name"]);
                   $tempalte->parseCurrentBlock();
                   
               }
               $template->setCurrentBlock("REGION");
               $template->parseCurrentBlock();
          }
     }
     $template->show();
?>

Link to comment
Share on other sites

Thanks! I ran your version of the code and this is what I got…

 

SELECT * FROM winery WHERE region_id = Error 1064 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3

 

I posted a copy of the database here so you could try it yourself. It's an example database from the publisher of the book I'm reading containing no vital information.

 

http://rapidshare.com/files/310384292/winestore.sql.html

 

MD5: E16368D7CCBABCDC9FAFC9AF70BBAD57

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.