• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 304
  • Last Modified:

PHP script error help

Hi,

Can anyone hwlp me out.  I have this PHP script which isn't working - that is no results are returned.

<?php
include_once "dbconnect.inc.php";

// turn error reporting on
error_reporting(E_ALL ^ E_NOTICE);

$sql = 'SELECT * FROM `tblTagDetails` LIMIT 0, 30';
$resultID = mysql_query($sql, $linkID);

echo '<b>Output from tblTagDetails</b><br>';
   
while ($query_data = mysql_fetch_array($resultID))
{
   echo "TagNumber: {$query_data['TagNumber']}<br>";
   echo "CustomerCode: {$query_data['CustomerCode']}<br>";
   echo "Weight : {$query_data['Weight']}<br>";
   echo "ShreddingDate: {$query_data['ShreddingDate']}<br>";
   echo "ShredConfirmationCode : {$query_data['ShredConfirmationCode']}<br><br>";
}

echo "<hr>";

$sql = 'SELECT * FROM `tblShreddingDetails` LIMIT 0, 30';
$resultID = mysql_query($sql, $linkID);

echo '<b>Output from tblShreddingDetails</b><br>';
   
while ($query_data = mysql_fetch_array($resultID))
{
   echo "ShredConfirmationCode: {$query_data['ShredConfirmationCode']}<br>";
   echo "CertificateLink: {$query_data['CertificateLink']}<br>";
}
?>
0
d10u4v
Asked:
d10u4v
  • 6
  • 5
  • 3
  • +2
1 Solution
 
Zack SoderquistCommented:
I don't notice anything obvious in your code .. have you tried running your queries through something like phpMyAdmin?

Try running 'SELECT * FROM `tblTagDetails` LIMIT 0, 30' directly against the database and see what the database is spitting out at you
0
 
Aamir SaeedCommented:
include_once "dbconnect.inc.php";

// turn error reporting on
error_reporting(E_ALL ^ E_NOTICE);

$sql = 'SELECT * FROM `tblTagDetails` LIMIT 0, 30';
$resultID = mysql_query($sql);

echo '<b>Output from tblTagDetails</b><br>';
   
while ($query_data = mysql_fetch_array($resultID))
{
   echo "TagNumber: {$query_data['TagNumber']}<br>";
   echo "CustomerCode: {$query_data['CustomerCode']}<br>";
   echo "Weight : {$query_data['Weight']}<br>";
   echo "ShreddingDate: {$query_data['ShreddingDate']}<br>";
   echo "ShredConfirmationCode : {$query_data['ShredConfirmationCode']}<br><br>";
}

echo "<hr>";

$sql = 'SELECT * FROM `tblShreddingDetails` LIMIT 0, 30';
$resultID = mysql_query($sql, $linkID);

echo '<b>Output from tblShreddingDetails</b><br>';
   
while ($query_data = mysql_fetch_array($resultID))
{
   echo "ShredConfirmationCode: {$query_data['ShredConfirmationCode']}<br>";
   echo "CertificateLink: {$query_data['CertificateLink']}<br>";
}
0
 
PatrickAdrichemCommented:
First check if "dbconnect.inc.php"; is located in the same folder.
Mostly i get white screens without any errors if it cannot find an include file.
if its wrong move file, or repoint it to right directory.

if that doesn't fix it.

Try changing it to this

<?php
include_once "dbconnect.inc.php";

// turn error reporting on
error_reporting(E_ALL ^ E_NOTICE);

$sql = 'SELECT * FROM `tblTagDetails` LIMIT 0, 30';
$resultID = mysql_query($sql, $linkID);
if (!$resultID) {
      echo('error<br>'.mysql_errno().'<br>'.mysql_error().'<br>'.$sql);
      die;
}
echo '<b>Output from tblTagDetails</b><br>';
   
while ($query_data = mysql_fetch_array($resultID))
{
   echo "TagNumber: {".$query_data['TagNumber']."}<br>";
   echo "CustomerCode: {".$query_data['CustomerCode']."}<br>";
   echo "Weight : {".$query_data['Weight']."}<br>";
   echo "ShreddingDate: {".$query_data['ShreddingDate']."}<br>";
   echo "ShredConfirmationCode : {".$query_data['ShredConfirmationCode']."}<br><br>";
}

echo "<hr>";

$sql = 'SELECT * FROM `tblShreddingDetails` LIMIT 0, 30';
$resultID = mysql_query($sql, $linkID);
if (!$resultID) {
      echo('error<br>'.mysql_errno().'<br>'.mysql_error().'<br>'.$sql);
      die;
}

echo '<b>Output from tblShreddingDetails</b><br>';
   
while ($query_data = mysql_fetch_array($resultID))
{
   echo "ShredConfirmationCode: {".$query_data['ShredConfirmationCode']."}<br>";
   echo "CertificateLink: {".$query_data['CertificateLink']."}<br>";
}
?>

and see what the error gives.

also post your "dbconnect.inc.php"; please (without password etc ofcourse just empty them) might be an error in that.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
netmunkyCommented:
try also set_ini('display_errors','1') to make sure errors are printed out

$linkID is not required unless you have multiple mysql_connect/mysql_pconnect

you might also try if( ! $resultID ) { print mysql_error(); }
0
 
PatrickAdrichemCommented:
on i_m_aamir

mysql_query($sql);

Mostly works, but its always better to define the connecting you are using.
0
 
d10u4vAuthor Commented:
I don't exactly get a black screen, i get the headings

Output details from ??? etc

but with no data, but data is present.

My dbconnect is:

<?php

$myServer = localhost;  
$myUser = '??';  //enter your info
$myPass = '??';  //enter your info
$myDB = '??';   //enter your info

$s = @mysql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");

$d = @mysql_select_db($myDB, $s)
or die("Couldn't open database $myDB");

?>
0
 
Aamir SaeedCommented:
you must have problem with your

include_once "dbconnect.inc.php";

because I have tried your script and its working fine for me.
0
 
Zack SoderquistCommented:
here's a connection class that will do all of your connections, and error handling .. it may give you more insight into your issue.. I would recommend copying and pasting the code into notepad or a text editor for better readability

<?
#ITEM                  VER            DESCRIPTION                                          DATE
#-----------------------------------------------------------------------
#File Name:                        class.mysql.asp
#
#Author:                        zsoderquist
#
#Created:                        04.05.2007
#Last Modified:                  
#
#Description:                  MySQL Connection Class
#                                    
#                                    This allows for you to connect
#                                    to the database server, select
#                                    the databse, and run your query
#                                    in one step using the query funtion.
#                                    It includes error handling at all stages.
#                                    You can iterate through the records
#                                    using NextRecord()
#                                    numRows() gives you the number of rows
#                                    from a SELECT query
#                                    affectedRows() gives you the affected
#                                    rows from an INSERT, DELETE or UPDATE
#                                    resetRecord() resets the returned records
#                                    back to the first record
#                                    
#Revisions:            1.0            Original Release
#                                    
#-----------------------------------------------------------------------
class clsMySQL
{
      var $strHost        = "";      //Host
      var $strUser        = "";      //Username
      var $strPassword = "";      //Password
      var $strDatabase = "";      //Database
      
      var $LinkId             = 0;
      var $ResourceId  = 0;
      var $strQuery;
      var $record        = array();
      var $intRow        = 0;
      
      var $intErrno        = 0;
      var $strError;
      
      #'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      # Summary:            Error Handling (private)
      # Parameters:      Error Message (string)
      # Return:            None
      #
      function prvFailed($strMessage)
      {
            $this->intErrno = mysql_errno();
            $this->strError = mysql_error();
            echo "<strong>Database Error:</strong> ".$strMessage."<br /> \n";
            echo "<strong>MySQL Error:</strong> ".$this->strError." (".$this->intErrno.")<br /> \n";            
          die();
      }
      
      #'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      # Summary:            Connect to Database Server and select database
      #                        (private)
      # Parameters:      None
      # Return:            None
      #
      function prvConnect()
      {
            @$this->LinkId = mysql_connect($this->strHost, $this->strUser, $this->strPassword);

            if(!$this->LinkId)
                  $this->prvFailed("Connection Failed");
                              
            if(!mysql_select_db($this->strDatabase, $this->LinkId))
                  $this->prvFailed("Can not use database. (".$this->strDatabase.")");
      }
      
      #'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      # Summary:            Run SQL Query
      # Parameters:      SQL(string)
      # Return:            Resource ID (resource id)
      #
      function query($strQuery)
      {
            if(!$this->LinkId)
                  $this->prvConnect();

            $this->ResourceId = mysql_query($strQuery, $this->LinkId);
            $this->intRow = 0;
            if(!$this->ResourceId)
                  $this->prvFailed("Invalid SQL: ".$strQuery);
            
            return $this->ResourceId;
      }

      #'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      # Summary:            Iterate to next record
      # Parameters:      None
      # Return:            Next Record(array)
      #
      function nextRecord()
      {
            $this->record = mysql_fetch_array($this->ResourceId, MYSQL_BOTH);
            $this->intRow += 1;
            
            $status = is_array($this->record);
            if(!$status)
            {
                  @mysql_free_result($this->ResourceId);
                  $this->ResourceId = 0;
            }
            return $status;            
      }

      #'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      # Summary:            Return Number of Rows from last query
      # Parameters:      None
      # Return:            Number Rows(integer)
      #
      function numRows()
      {
            return @mysql_num_rows($this->ResourceId);
      }      

      function affectedRows()
      {
            return @mysql_affected_rows();
      }

      #'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      # Summary:            Reset to First Record
      # Parameters:      None
      # Return:            None
      #
      function resetRecord()
      {
            if($this->ResourceId)
                  mysql_data_seek($this->ResourceId, 0);
      }
}

# SAMPLE IMPLEMENTATION
#
#
# $mysql = new clsMySQL();
# $result = $mysql->query("SELECT * FROM `test`");
#
# echo "Number Rows: ".$mysql->numRows()."<br />";
# while($mysql->nextRecord())
# {      
#        echo $mysql->record['id']."<br />";
# }
?>
0
 
Zack SoderquistCommented:
include_once "dbconnect.inc.php";

include_once("dbconnect.inc.php");
0
 
Aamir SaeedCommented:
I think this is the problem with your connection source, you are taking into $s and passing $linkID

$s = @mysql_connect($myServer, $myUser, $myPass)

$resultID = mysql_query($sql, $linkID);
0
 
PatrickAdrichemCommented:
In your case you give $s as your Database connection

while in your query you tell it to use $linkID which is not an active connection

Try changing it to this:
$myServer = localhost;  
$myUser = '??';  //enter your info
$myPass = '??';  //enter your info
$myDB = '??';   //enter your info

$linkID  =      @mysql_pconnect($myServer, $myUser, $myPass) or die ("Connection to the SQL Server Failed<br><br>".mysql_error());
              @mysql_select_db($myDB, $linkID ) or die ("Connection to the Database Failed");
0
 
PatrickAdrichemCommented:
Oh that class is even better BTW. but that means a bigger change to your script.
Classes make your code cleaner, and less chance in getting errors because you write it once and use it a million times
0
 
d10u4vAuthor Commented:
Thanks guys,  I have got the debug working.  Now comes the next bit.

I have this script which is trying to connect to the same database:

<?php
$sql = "SELECT TagNumber, CustomerCode, Weight, ShreddingDate, ShredConfirmationCode, CertificateLink
        FROM tblTagDetails AS a, tblShreddingDetails AS b
        WHERE a.ShredConfirmationCode = b.ShredConfirmationCode
          AND TagNumber = '$TagNumber'
          AND CustomerCode = '$CustomerCode'";
$resultID = mysql_query($sql, $linkID);

// record(s) found
if (mysql_num_rows($resultID) > 0)
{
   $query_data = mysql_fetch_object($resultID);
   
   echo "Tag Number: $query_data->TagNumber<br />";
   echo "Customer Code: $query_data->CustomerCode<br />";
   echo "Weight: $query_data->Weight<br />";
   echo "Shredding Date: $query_data->ShreddingDate<br />";
   echo "Shred Confirmation Code: $query_data->ShredConfirmationCode<br />";
   echo "Certificate Link: $query_data->CertificateLink<br />";
}
// no record found
else
{
   echo "No record found";
}
?>

I can populate the query with the fields entered from the previous page [CustomerCode & TagNumber] but get no results returned.

The top of the page has:

<?php
include_once 'dbconnect.inc.php';
$TagNumber = $_POST['TagNumber'];
$CustomerCode = $_POST['CustomerCode'];
?>

Hope you can still help me out?

Anthony
0
 
Zack SoderquistCommented:
You should start this in a new thread d10u4v
0
 
PatrickAdrichemCommented:
adjust query to:

SELECT a.TagNumber, a.CustomerCode, a.Weight, a.ShreddingDate, a.ShredConfirmationCode, b.CertificateLink
        FROM tblTagDetails AS a,
        INNER JOIN tblShreddingDetails AS b
        ON a.ShredConfirmationCode = b.ShredConfirmationCode
          AND a.TagNumber = '$TagNumber'
          AND a.CustomerCode = '$CustomerCode'

(not sure which field is in a or b. adjust accordingly)

don't forget the error handling to see if you have query errors

$resultID = mysql_query($sql, $linkID);
if (!$resultID) {
      echo('error<br>'.mysql_errno().'<br>'.mysql_error().'<br>'.$sql);
      die;
}

The error you should recieve in current script is something like
duplicate entry found: ShredConfirmationCode dunno which to use a.ShredConfirmationCode or b.ShredConfirmationCode

If your as object does not longer work add

 a.TagNumber as Tagnumber

you only have to add the a.  and b. part to the double fields (that reside in both tables)
the Inner Join instead of the where is just cleaner and faster SQL.
0
 
Zack SoderquistCommented:
Is this question then resolved?
0
 
Zack SoderquistCommented:
Has this been resolved yet d10u4v??
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 6
  • 5
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now