Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Can't find the bug...

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Can't find the bug...

Postby geno11x11 » Wed Jan 30, 2013 6:07 pm

I have been debugging a small clip of code for hours - actually all day. Either I am missing the error, it is a PHP configuration issue, or a PHP bug. I hate to ask for help for a possibe syntax error, but I am pulling my hair out.

PNSLib.php is an include file imported into Master.php - In the interest of brevity, I am including only the section of the code containing the error - If I delete this section, the browser error is gone. I'm convinced the problem lies in the last 16 lines of PNSLib.php.

The following is (1) the output in a browser which you can view at http://mammothmicro.dyndns-web.com/PNS/Master.php, (2) Master.php which calls PNSLib.php (3) the PNSLib.php code (the include file).

Browser output: - This is the error.

Loading PNSLib.php
; "; } // end if $select = mysql_select_db($dbName,$dbConn); if (!$select) { print "problem selecting table" . mysql_error() . "
"; } // end if return $dbConn; // end connectToPNS_DB } ?>


The PNSMaster.php code:

<html>
<head>
<title>PNS Master Main Page</title>
</head>
<body>

<p>Loading PNSLib.php</p>;
<?php
include 'PNSLib.php';
?>


The PNSLib.php code:

<style type = "text/css">
body{
background-color: black;
color: white;
text-align:center
}
</style>
<?
//PNSLib.php
//holds utilities for PNS_DB database

//variables
$userName = "root";
$serverName = "localhost";
$password = "********";
$dbName = "PNS_DB";
$dbConn = "";
$mainProgram = "Master.php";

function connectToPNS_DB() {
//connects to the PNS_DB
global $serverName, $userName, $password;

$dbConn = mysql_connect($serverName, $userName, $password);
if (!$dbConn)
{
print "problem connecting to database..." . mysql_error() . "<br>";
} // end if

$select = mysql_select_db($dbName,$dbConn);
if (!$select)
{
print "problem selecting table" . mysql_error() . "<br>";
} // end if

return $dbConn;
// end connectToPNS_DB
}
?>
Last edited by geno11x11 on Thu Jan 31, 2013 6:35 pm, edited 2 times in total.
geno11x11
New php-forum User
New php-forum User
 
Posts: 4
Joined: Wed Jan 30, 2013 5:27 pm

Re: Can't find the bug...

Postby rwhite35 » Wed Jan 30, 2013 8:53 pm

Couple things to look at. Your PNS_DB looks like a constant, is it? If so, when you put it in quotes, you have assigned $dbName a string "PNS_DB". If PNS_DB is in fact a constant, I would just change the following

Code: Select all
$select = mysql_select_db(PNS_DB,$dbConn);


Next I would consider migrating to prepared statements using either mysqli or PDO. It looks like from this code that PNS only requires the connection $dbConn. You could make that connection like so

Code: Select all
     $dbConn = new mysqli(DB_HOST,DB_UNAME,DB_UPWORD,DB_NAME);
     if ($dbConn->connect_errno){
       $mes = urlencode("Failed ".$dbConn->connect_error);
          header("Location: $error.php?err=$mes");
          exit();
     }


Then a query
Code: Select all
     $result=$dbConn->query("SELECT user_name FROM users");


Then output
Code: Select all
  while ($row = $result->fetch_assoc()) {
     echo "This user name is ".$row['user_name'];
  }
$dbConn->close();


ref: http://www.php.net/manual/en/book.mysqli.php

Finally, in some setups, the script and API both have to be owned by the same process. For example, on Linux, both script and API need to be owned by apache:apache. You would change the script and API ownership with the chown command from the command line.

admin# chown apache:apache Master.php
admin# chown apache:apache PNSLib.php

Hopefully that gives you some new leads to follow.
rwhite35
New php-forum User
New php-forum User
 
Posts: 13
Joined: Thu Sep 27, 2012 10:55 am

Re: Can't find the bug...

Postby geno11x11 » Mon Feb 11, 2013 11:58 am

Problem solved!!! :D I had a hunch and copied my script from this forum page and pasted it to my editor, replaced the old files, and it worked -- I'm thinking now that hidden characters from my editor are to blame.

I examined my original code with hidden characters visible, checked it line by line, character by character, and found absolutely nothing astray. So, in this situation, how do you know if the error is program logic or a stray invisible character mucking things up?

Am I on the right track with the hidden character theory, or is there reason to believe the error cleared for a different reason?
geno11x11
New php-forum User
New php-forum User
 
Posts: 4
Joined: Wed Jan 30, 2013 5:27 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.