mysql link error problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
skooter
Forum Newbie
Posts: 20
Joined: Sun Jul 01, 2007 5:29 am

mysql link error problem

Post by skooter »

Everah | Please use proper bbCode tags when posting code in the forums. For convenience you can use [code] to highlight code, [php] or [syntax="php"] to highlight PHP or [{lang}] to highlight just about any programming language {lang} (like javascript or vbscript).

Okay, so I've set up a site to access a (simple) MySQL database of parts, but the devsite is telling me the following error:

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\Users\skooter\Dev\Web\trailerParts\scripts\PartsScript.php on line 35

SETUP: WAMP setup, with PHP v5.2.5.

Line 35 (and its surrounds) read:

Code: Select all

<?php
function select($choice) {
        
        /* objects for connection use */
        $conn = new DBConnect();
        $conf = new DBConfig();
        
        /* select necessary values */
        $command = "SELECT * FROM " . $conf->getTableName() . " WHERE category=\"$choice\"";
        $this->resource = mysql_query($command, $conn->getResource());
        
        /* check if returned ok */
        $this->isFalse();
        
        /* closing connection */
        $conn->_destruct();
        
        /* return the resource */
        return $this->resource;
    }
?>
$conn is an instance of DBConnect which connects to the database. getResource (getter method) seems to be returning nothing (i *occassionally* get a 1 if I echo it). DBConnect reads:

Code: Select all

<?php
class DBConnect {
 
    /* connection resource */
    private $res;
        
    function _construct() {
        require("./DBConfig.php");
        $dbc = new DCConfig();
        $this->res = mysql_connect($dbc->getHost(), $dbc->getUser(), $dbc->getPassword());
        checkRes();
        if (!(mysql_select_db($dbc->getTableName(), $this->res))) {
            die("failed to select database: " . error());
        }
    }
    
    /* returns the connection to the currently connected MySQL resource */
    function getResource() {
        return $this->res;
    }
    
    /* checks if the resource variable has been properly set as a MySQL resource */
    function checkRes() {
        if (!$this->res) {
            die("connection failed: " . error());
        }
    }
    
    function error() {
        return (mysql_errno() . " </br> " . mysql_error());
    }
    
    function _destruct() {
        include("DBClose.php");
        $close = new DBClose();
        $close->close($this->res);
    }
}   // end TPConnect
?>
can anyone see any problems with this?? I figure there may be something wrong with the class/method set up concerning class variables....

Everah | Please use proper bbCode tags when posting code in the forums. For convenience you can use [code] to highlight code, [php] or [syntax="php"] to highlight PHP or [{lang}] to highlight just about any programming language {lang} (like javascript or vbscript).
Last edited by RobertGonzalez on Fri Feb 08, 2008 4:41 pm, edited 1 time in total.
Reason: Added PHP bbcode tags to highlight code on the post
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: mysql link error problem

Post by Kieran Huggins »

try:

Code: Select all

$this->res = mysql_connect($dbc->getHost(), $dbc->getUser(), $dbc->getPassword()) or die(MySQL_error());
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: mysql link error problem

Post by Zoxive »

Kieran Huggins wrote:try:

Code: Select all

$this->res = mysql_connect($dbc->getHost(), $dbc->getUser(), $dbc->getPassword()) or die(MySQL_error());
He has it check to see if its empty in the function checkRes();, but I don't think he is calling it right.

Try

Code: Select all

$this->checkRes();
Also, it seems like you should be getting Errors, try turning on error reporting, and fixing them.
(A lot of places are not calling $this->func());
skooter
Forum Newbie
Posts: 20
Joined: Sun Jul 01, 2007 5:29 am

Re: mysql link error problem

Post by skooter »

ok...i've popped in the
$this->res = mysql_connect($dbc->getHost(), $dbc->getUser(), $dbc->getPassword()) or die(mysql_error());

line and there isn't a problem. also popped in the $this->checkRes()...

no change
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: mysql link error problem

Post by Zoxive »

skooter wrote:ok...i've popped in the
$this->res = mysql_connect($dbc->getHost(), $dbc->getUser(), $dbc->getPassword()) or die(mysql_error());

line and there isn't a problem. also popped in the $this->checkRes()...

no change
Turn on Error Reporting As I Suggested.

There are many other spots you are calling error();, then it looks like you want $this->error();

Code: Select all

// Top of File
ini_set('display_errors',true);
error_reporting(E_ALL);
skooter
Forum Newbie
Posts: 20
Joined: Sun Jul 01, 2007 5:29 am

Re: mysql link error problem

Post by skooter »

E_ALL is set already in the php.ini file... thats the one comin up
skooter
Forum Newbie
Posts: 20
Joined: Sun Jul 01, 2007 5:29 am

Re: mysql link error problem

Post by skooter »

Boy this is getting to me....

I've run DBConnect as a standalone page, with MySQL connected...ports checked...Apache running fine. The MySQL resource link is still set to FALSE :banghead:

Oddly enuff there is no mysql_error() being recorded anywhere and the apache error log is not recording any errors (E_ALL is still set in php.ini). this is freakin me right out! :banghead:
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: mysql link error problem

Post by Zoxive »

What is the display_errors setting as I suggested?

error_reporting means nothing if display_errors if off.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: mysql link error problem

Post by RobertGonzalez »

The constructor for the class you posted is called with _construct instead of __construct() (note the TWO underscores).
skooter
Forum Newbie
Posts: 20
Joined: Sun Jul 01, 2007 5:29 am

Re: mysql link error problem

Post by skooter »

kk. display_errors and error reporting are both set as you asked, and no error is being reported, neither on-screen nor to the apache error log.

it just read SELECT FAIL: (my error code) with no report from mysql_error().

double underscores - thanks didnt notice that!
skooter
Forum Newbie
Posts: 20
Joined: Sun Jul 01, 2007 5:29 am

Re: mysql link error problem

Post by skooter »

ok, something else i've just noticed... when the constructor is called in a separate html file, the die() messages are NOT given. Is this because they are in a constructor or because the constructor is running fine (ie making the connection) ??

If the latter, does that mean the problem has something to do with assigning a resource as a class variable?
skooter
Forum Newbie
Posts: 20
Joined: Sun Jul 01, 2007 5:29 am

Re: mysql link error problem

Post by skooter »

sorry for the spam but...

I put an echo in the constructor (after the call to checkRes()) which gets outputted - so this means the die() is not being called in checkRes() correct?? So this means the resource is being allocated fine, then being lost somewhere between construction and the call to the getter, right?? :o
skooter
Forum Newbie
Posts: 20
Joined: Sun Jul 01, 2007 5:29 am

Re: mysql link error problem

Post by skooter »

BOOM!!! Problem solvered!!

fyi: the problem lay in the not calling the constructor properly, so that the link wasn't being created.

After this was solved, the second problem lay in the fact that if (!$checkRes()) fails since checkRes() doesn't return a boolean - it either dies or continues....l
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: mysql link error problem

Post by RobertGonzalez »

So are you all squared away now?
skooter
Forum Newbie
Posts: 20
Joined: Sun Jul 01, 2007 5:29 am

Re: mysql link error problem

Post by skooter »

Weeeelllll, the link error is solvered! but here's one to try out :)

I figure this might be an Apache problem, or an address referencing problem but the error log reads:

[Tue Feb 12 10:46:08 2008] [error] [client 127.0.0.1] File does not exist: C:/Users/skooter/Dev/Web/Web

which is fair as there is no Dev/Web/Web!!!! Dev/Web is the DocRoot set in the httpd config file. Checking the access file, I've noticed that all attempts to access said directory come from $_GET given to a BasketScript.php file.... any thoughts as to where to start???
Post Reply