Jump to content

PHP Query Error


MattAdamson

Recommended Posts

I have this section of code which was working fine before, I cant remember the changes I made to stop it working, it should perform a query but it keeps kicking up the "mysql_query(): supplied argument is not a valid MySQL-Link resource" so Im assuming that the query being passed to my function is not correct as the error handling for my database connection kicks up nothing.

I hope this is explained enough, if I need to mention anything else please do say so. I will paste my query function below and also the piece of code passing the query to it.

Thanks very much for reading :D

[code]
function Query($query)
{
$result = mysql_query($this->query, $this->db_conn);
if($err = mysql_error($this->db_conn))
{
echo "Error on query: ". $query . "<br>" . $err ;
}
else
{
return $result;
}
}
[/code]

[code]else
{
//set login error to true
$login_error = true;
//create a query to update the last time the user failed to login
$sql = "UPDATE users SET last_failed = '$now' WHERE username = '{$mysql['username']}'";
$connect->Query($sql);
}[/code]
Link to comment
Share on other sites

I create the object here

[code]
//create a new object for page class
$connect = new CDriver();
[/code]

$this->db_conn  comes from my database class, I will include the whole class below, it includes the problem code also.

[code]class CDriver {


var $db_conn;
var $db_config;
var $db_list;
var $table_list;

function Connect()
{
$this->db_conn = mysql_connect('localhost', '*', '*') or die(mysql_error());
    mysql_select_db( havoc_site, $this->db_conn ) or die(mysql_error());
}

//Dissconnect from the database
function Dissconnect()
{
mysql_close($this->db_conn);
}

//function to execute mysql query
function Query($query)
{
$result = mysql_query($query, $this->db_conn);
if($err = mysql_error($this->db_conn))
{
echo "Error on query: ". $query . "<br>" . $err ;
}
else
{
return $result;
}
}

}[/code]

[b]edit(shoz): Login details removed[/b]
Link to comment
Share on other sites

I realize that you may not want to show your code in its entirety, but if you could post a complete script (a smaller example) with the CDriver class that also gives the same problem it'll make it easier to diagnose.

eg:
[code]
class CDriver
{
  ...
  ...

}
$connect = new CDriver();
$connect->Connect();
$sql = 'SELECT * FROM table';
$connect->Query($sql);
[/code]

If this doesn't produce the same error then something else has gone wrong and a look at the script in more detail would be needed.
Link to comment
Share on other sites

It is several pages which are fairly long as I include files but I will post as much as I can :) thanks for helping.

I hope this enough, I think Ive provided everything?

index.php
[code]

//require the drivers for connection to mysql
require('lib/driver.inc.php');
//start session before anything else
session_start();
//create a new object for page class
$connect = new CDriver();
//open connection to database and leave open for all queries to use
$connect -> Connect();
//requires authorize to check if a user has tried to login, it creates a session for them
require_once('lib/user_func.inc.php');
$user_func = new user_func();
$user_func->check_login();
[/code]

section of user_func.php, function for checking login
[code]
//create a query
$sql = "SELECT * FROM users";
$connect->Query($sql);
[/code]


driver.inc.php

[code]

class CDriver {


var $db_conn;
var $db_config;
var $db_list;
var $table_list;

function Connect()
{
$this->db_conn = mysql_connect('localhost', '*', '*') or die(mysql_error());
    mysql_select_db( havoc_site, $this->db_conn ) or die(mysql_error());
}

//function to execute mysql query
function Query($query)
{
$result = mysql_query($query, $this->db_conn);
if($err = mysql_error($this->db_conn))
{
echo "Error on query: ". $query . "<br>" . $err ;
}
else
{
return $result;
}
}
[/code]
Link to comment
Share on other sites

Sorry I missed that, it creates the object like below

[code]
//require the drivers for connection to mysql
require('lib/driver.inc.php');
//create a new object for page class
$connect = new CDriver();
[/code]

I added those two lines at the top of my script and get a fair few "Notice:" also some undefined varibles but those are not such a big deal as they are from unfinished areas of code and my original "Warnings" about the supplied argument is not a valid link.

The "Notice" that appears is as follows.

Notice: Use of undefined constant havoc_site - assumed 'havoc_site'
Link to comment
Share on other sites

[quote author=MattAdamson link=topic=109433.msg441861#msg441861 date=1159292430]
Sorry I missed that, it creates the object like below

[code]
//require the drivers for connection to mysql
require('lib/driver.inc.php');
//create a new object for page class
$connect = new CDriver();
[/code]
[/quote]
The CDriver object "$connect", that you create in the check_login function has no connection to the $connect object you create originally and called the "Connect()" method on.

If you'd like the user_func object to have access to $connect then perhaps create a constructor in user_func that will store a reference to the object in a class member variable.
[code]
class user_func
{
  var $connect;
  function user_func(&$connect)
  {
        $this->$connect =& $connect;
  }
  function check_login()
  {
        $sql = '';
        $this->connect->Query($sql);
  }
}
[/code]
The $connect var should also be available in the $GLOBALS array.

[quote author=MattAdamson link=topic=109433.msg441861#msg441861 date=1159292430]
The "Notice" that appears is as follows.

Notice: Use of undefined constant havoc_site - assumed 'havoc_site'
[/quote]

Put quotes arround havoc_site in the mysql_select_db() call
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.