Page 1 of 1

mysql link error problem

Posted: Wed Feb 06, 2008 6:42 am
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).

Re: mysql link error problem

Posted: Wed Feb 06, 2008 6:55 am
by Kieran Huggins
try:

Code: Select all

$this->res = mysql_connect($dbc->getHost(), $dbc->getUser(), $dbc->getPassword()) or die(MySQL_error());

Re: mysql link error problem

Posted: Wed Feb 06, 2008 9:14 am
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());

Re: mysql link error problem

Posted: Wed Feb 06, 2008 1:19 pm
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

Re: mysql link error problem

Posted: Wed Feb 06, 2008 1:22 pm
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);

Re: mysql link error problem

Posted: Wed Feb 06, 2008 1:48 pm
by skooter
E_ALL is set already in the php.ini file... thats the one comin up

Re: mysql link error problem

Posted: Thu Feb 07, 2008 11:54 pm
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:

Re: mysql link error problem

Posted: Fri Feb 08, 2008 8:10 am
by Zoxive
What is the display_errors setting as I suggested?

error_reporting means nothing if display_errors if off.

Re: mysql link error problem

Posted: Fri Feb 08, 2008 4:42 pm
by RobertGonzalez
The constructor for the class you posted is called with _construct instead of __construct() (note the TWO underscores).

Re: mysql link error problem

Posted: Fri Feb 08, 2008 8:40 pm
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!

Re: mysql link error problem

Posted: Fri Feb 08, 2008 8:52 pm
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?

Re: mysql link error problem

Posted: Fri Feb 08, 2008 9:10 pm
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

Re: mysql link error problem

Posted: Fri Feb 08, 2008 9:34 pm
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

Re: mysql link error problem

Posted: Mon Feb 11, 2008 10:47 am
by RobertGonzalez
So are you all squared away now?

Re: mysql link error problem

Posted: Mon Feb 11, 2008 7:52 pm
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???