Solved

PHP script error help

Posted on 2007-04-11
17
285 Views
Last Modified: 2013-12-12
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
Comment
Question by:d10u4v
  • 6
  • 5
  • 3
  • +2
17 Comments
 
LVL 7

Expert Comment

by:Zack Soderquist
ID: 18892982
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
 
LVL 14

Expert Comment

by:Aamir Saeed
ID: 18893027
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
 
LVL 5

Expert Comment

by:PatrickAdrichem
ID: 18893036
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 8

Expert Comment

by:netmunky
ID: 18893045
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
 
LVL 5

Expert Comment

by:PatrickAdrichem
ID: 18893052
on i_m_aamir

mysql_query($sql);

Mostly works, but its always better to define the connecting you are using.
0
 

Author Comment

by:d10u4v
ID: 18893081
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
 
LVL 14

Expert Comment

by:Aamir Saeed
ID: 18893124
you must have problem with your

include_once "dbconnect.inc.php";

because I have tried your script and its working fine for me.
0
 
LVL 7

Expert Comment

by:Zack Soderquist
ID: 18893130
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
 
LVL 7

Expert Comment

by:Zack Soderquist
ID: 18893142
include_once "dbconnect.inc.php";

include_once("dbconnect.inc.php");
0
 
LVL 14

Expert Comment

by:Aamir Saeed
ID: 18893192
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
 
LVL 5

Expert Comment

by:PatrickAdrichem
ID: 18893202
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
 
LVL 5

Expert Comment

by:PatrickAdrichem
ID: 18893219
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
 

Author Comment

by:d10u4v
ID: 18893343
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
 
LVL 7

Expert Comment

by:Zack Soderquist
ID: 18893361
You should start this in a new thread d10u4v
0
 
LVL 5

Accepted Solution

by:
PatrickAdrichem earned 500 total points
ID: 18893480
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
 
LVL 7

Expert Comment

by:Zack Soderquist
ID: 18893740
Is this question then resolved?
0
 
LVL 7

Expert Comment

by:Zack Soderquist
ID: 18969513
Has this been resolved yet d10u4v??
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to stop user from going back to a previous page in PHP 3 32
Presenting past 6 months bug 7 29
Php recording post 4 43
comma true 6 33
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question