jasonc Posted July 28, 2010 Share Posted July 28, 2010 I have a function that generates a random number and checks to make sure it does not exists in the database. I call this function using $ref = get_record_ref(); I get the following error. Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in line 68 EDITED.... if i place the code from the function in my script and not use it as a function the script works. why would this be? <?php db_connect() or die('Unable to connect to database server!'); function db_connect($server = 'localhost', $username = 'username', $password = 'password', $database = 'databasename', $link = 'db_link') { global $$link; $$link = mysql_connect($server, $username, $password); if ($$link) mysql_select_db($database); return $$link; } //Function to handle database errors. function db_error($query, $errno, $error) { die('<font color="#000000"><strong>' . $errno . '<br><br><br><br><small><font color="#ff0000">[sTOP]</font></small><br><br></strong><br><br><br>One of our coders have been informed of this problem and shall be working on it very shortly.<br><br>Please try again in a few minutes or so when we should have this issue fixed.<br><br>Thank you.</font>'); } //Function to query the database. function db_query($query, $link = 'db_link') { global $$link; $theip = $_SERVER['REMOTE_ADDR']; $httpuseragent = $_SERVER['HTTP_USER_AGENT']; $fromlink4 = gethostbyaddr($_SERVER['REMOTE_ADDR']); // this next line is the error line 68 $result = mysql_query($query, $$link) or db_error($query, mysql_errno(), mysql_error()); return $result; } function get_record_ref() { // /* // set new $record_ref. $record_ref = (rand(10, rand(10, 1000000000))); // check if exisits in DB. $sql = "SELECT * FROM `table` WHERE `record_ref` = '". $record_ref ."'"; $get_records_with_record_ref = db_query($sql,''); // count entries in DB for $record_ref. $count_record_ref = mysql_num_rows($get_records_with_record_ref); while ($count_record_ref > 0) { // set new $record_ref. $record_ref = (rand(10, rand(10, 1000000000))); // check if exisits in DB. $sql = "SELECT * FROM `table` WHERE `record_ref` = '". $record_ref ."'"; $get_records_with_record_ref = db_query($sql); // count entries in DB for $record_ref. $count_record_ref = mysql_num_rows($get_records_with_record_ref); } // */ return $record_ref; } ?> Quote Link to comment Share on other sites More sharing options...
lemmin Posted July 28, 2010 Share Posted July 28, 2010 I believe it has something to do with you using your global link as a variable variable. I can't really tell if you are doing that right without seeing the definition; however, when any mysql function accepts a link resource, it is optional. If you exclude it, it uses the most recently opened one. So, my advice is to simply leave it out. Unless of course you are connecting to multiple databases at the same time. Quote Link to comment Share on other sites More sharing options...
jasonc Posted July 28, 2010 Author Share Posted July 28, 2010 think i have found out why $get_records_with_record_ref = db_query($sql); // not db_query($sql,''); do not know how this got in there? will check some more to see if this is the reason why, but seems it is working ok for now. Quote Link to comment 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.