MattAdamson Posted September 25, 2006 Share Posted September 25, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/21998-php-query-error/ Share on other sites More sharing options...
wildteen88 Posted September 25, 2006 Share Posted September 25, 2006 $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] Quote Link to comment https://forums.phpfreaks.com/topic/21998-php-query-error/#findComment-98294 Share on other sites More sharing options...
MattAdamson Posted September 25, 2006 Author Share Posted September 25, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/21998-php-query-error/#findComment-98322 Share on other sites More sharing options...
wildteen88 Posted September 25, 2006 Share Posted September 25, 2006 Where is $this->db_conn comming from? Is $db_conn initiated in the class for $connect? Quote Link to comment https://forums.phpfreaks.com/topic/21998-php-query-error/#findComment-98419 Share on other sites More sharing options...
MattAdamson Posted September 25, 2006 Author Share Posted September 25, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/21998-php-query-error/#findComment-98427 Share on other sites More sharing options...
MattAdamson Posted September 26, 2006 Author Share Posted September 26, 2006 Can anyone make anymore suggestions? ??? Quote Link to comment https://forums.phpfreaks.com/topic/21998-php-query-error/#findComment-98838 Share on other sites More sharing options...
shoz Posted September 26, 2006 Share Posted September 26, 2006 Have you done a[code]<?php$connect->Connect();?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/21998-php-query-error/#findComment-98854 Share on other sites More sharing options...
MattAdamson Posted September 26, 2006 Author Share Posted September 26, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/21998-php-query-error/#findComment-99063 Share on other sites More sharing options...
shoz Posted September 26, 2006 Share Posted September 26, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/21998-php-query-error/#findComment-99071 Share on other sites More sharing options...
MattAdamson Posted September 26, 2006 Author Share Posted September 26, 2006 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 mysqlrequire('lib/driver.inc.php');//start session before anything elsesession_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 themrequire_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] Quote Link to comment https://forums.phpfreaks.com/topic/21998-php-query-error/#findComment-99086 Share on other sites More sharing options...
shoz Posted September 26, 2006 Share Posted September 26, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/21998-php-query-error/#findComment-99092 Share on other sites More sharing options...
MattAdamson Posted September 26, 2006 Author Share Posted September 26, 2006 Sorry I missed that, it creates the object like below[code]//require the drivers for connection to mysqlrequire('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' Quote Link to comment https://forums.phpfreaks.com/topic/21998-php-query-error/#findComment-99099 Share on other sites More sharing options...
shoz Posted September 26, 2006 Share Posted September 26, 2006 [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 mysqlrequire('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 Quote Link to comment https://forums.phpfreaks.com/topic/21998-php-query-error/#findComment-99114 Share on other sites More sharing options...
MattAdamson Posted September 27, 2006 Author Share Posted September 27, 2006 Thanks so much for that answer! I will get to trying it out and post back with how it goes :)Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/21998-php-query-error/#findComment-99817 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.