Jump to content

Archived

This topic is now archived and is closed to further replies.

MattAdamson

PHP Query Error

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]

Share this post


Link to post
Share on other sites
$this->query should be $query on this line:
[code=php:0]$result = mysql_query($this->query, $this->db_conn);[/code]


So it should be this:
[code=php:0]$result = mysql_query($query, $this->db_conn);[/code]

Share this post


Link to post
Share on other sites
Hey thanks for the suggestion! :) Ive changed this back as it was something I changed to see if I could get the code working, either way I still get the same error though  :(

Any other suggestions please?

Share this post


Link to post
Share on other sites
Where is $this->db_conn comming from? Is $db_conn initiated in the class for $connect?

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
I have indeed, this is on my main page which calls the rest of the class's and functions.

[code]//open connection to database and leave open for all queries to use
$connect -> Connect();[/code]

Thanks again but anything else?

Share this post


Link to post
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.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
How does check_login() get access to the global $connect object?

Also put the following at the top of the script
[code]
error_reporting(E_ALL);
ini_set("display_errors", "1");
[/code]

Share this post


Link to post
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'

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.