Jump to content


Photo

PHP Query Error


  • Please log in to reply
13 replies to this topic

#1 MattAdamson

MattAdamson
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 25 September 2006 - 04:11 PM

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

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;
		}	
	}

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);
				}


#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 25 September 2006 - 04:13 PM

$this->query should be $query on this line:
$result = mysql_query($this->query, $this->db_conn);


So it should be this:
$result = mysql_query($query, $this->db_conn);


#3 MattAdamson

MattAdamson
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 25 September 2006 - 04:38 PM

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?

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 25 September 2006 - 06:37 PM

Where is $this->db_conn comming from? Is $db_conn initiated in the class for $connect?

#5 MattAdamson

MattAdamson
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 25 September 2006 - 06:42 PM

I create the object here

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

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

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;
		}	
	}

}

edit(shoz): Login details removed

#6 MattAdamson

MattAdamson
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 26 September 2006 - 11:07 AM

Can anyone make anymore suggestions?  ???

#7 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 26 September 2006 - 12:22 PM

Have you done a
<?php
$connect->Connect();
?>


#8 MattAdamson

MattAdamson
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 26 September 2006 - 04:31 PM

I have indeed, this is on my main page which calls the rest of the class's and functions.

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

Thanks again but anything else?

#9 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 26 September 2006 - 04:53 PM

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:
class CDriver
{
   ...
   ...

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

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.

#10 MattAdamson

MattAdamson
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 26 September 2006 - 05:15 PM

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

//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();

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


driver.inc.php


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;
		}	
	}


#11 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 26 September 2006 - 05:26 PM

How does check_login() get access to the global $connect object?

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


#12 MattAdamson

MattAdamson
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 26 September 2006 - 05:40 PM

Sorry I missed that, it creates the object like below

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

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'

#13 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 26 September 2006 - 06:03 PM

Sorry I missed that, it creates the object like below

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

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.
class user_func
{
   var $connect;
   function user_func(&$connect)
   {
        $this->$connect =& $connect;
   }
   function check_login()
   {
        $sql = '';
        $this->connect->Query($sql);
   }
}
The $connect var should also be available in the $GLOBALS array.

The "Notice" that appears is as follows.

Notice: Use of undefined constant havoc_site - assumed 'havoc_site'


Put quotes arround havoc_site in the mysql_select_db() call

#14 MattAdamson

MattAdamson
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 27 September 2006 - 07:56 PM

Thanks so much for that answer! I will get to trying it out and post back with how it goes :)

Thanks again!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users