Won't work on public server mysql_query():

Hi,

I have a webpage that is running fine on my local server but when i upload it to a shared public server(tried 2) I get this error message returned from my php.

<b>Warning</b>:  mysql_query(): supplied argument is not a valid MySQL-Link resource in <b>/home/********
/public_html/*********i/database.php</b> on line <b>25</b><br />

the code around the error is below, my server at home is ubuntu 9.04 lamp can post both phpinfo() outputs if needed..

Thanks, I am pulling my hair out...
22://Function to query the database.
function db_query($query, $link = 'db_link') 
23:function db_query($query, $link = 'db_link') 
24:{
 
25:global $$link;
 
26:$result = mysql_query($query, $$link) or db_error($query, mysql_errno(), mysql_error());
 
27:return $result;
 
28:}

Open in new window

ewan69Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
gunny051499Connect With a Mentor Commented:
Hi ewan69,

please put the following line right after the "global $$link" line in both functions "db_query" and "db_connect":

echo "You are trying to access link name '".$link."'<br>\n";

The double-dollar sign indicates to PHP it should use the variable named by the string contained inside $link. For example: if $link contains "database_link" then doing "$$link = 1" would assign the value "1" to the variable $database_link.

Since your db-connection seems to succeed, I assume your problem lies with $link changing from db_connect to db_query..... solving that, solves your issue. That variable is probably changed outside of database.php.

Gunny
0
 
gunny051499Commented:
Hi ewan69,

I guess the code is fine, but the login to the DB did not work (that's why you don't have a valid link ressource!). Please check:

- hostname
- username/password

if you *think* those settings are ok, you should have a look at the return codes for mysql_connect and mysql_select_db! See the sample code below.

Cheers

Gunny
<?php
 
$HOSTNAME = 'localhost';
$USERNAME = 'username';
$PASSWORD = 'password';
$DATABASE = 'database';
 
$link = mysql_connect($HOSTNAME, $USERNAME, $PASSWORD);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
 
$db_selected = mysql_select_db($DATABASE, $link);
if (!$db_selected) {
    die ('Can\'t use database : ' . mysql_error());
}
 
mysql_close($link);
 
?>

Open in new window

0
 
ewan69Author Commented:
Thanks for your help but nothing wrong with the details. I tried your code and it came up with no errors at all.
My code works on local server and also works on 1 public server but the other 2 just throw the same error. Below is the configure command part of the phpinfo() for one of the servers that doesn't work.

Thanks,

Ewan
'./configure' '--enable-bcmath' '--enable-calendar' '--enable-dbase' '--enable-exif' '--enable-fastcgi' '--enable-ftp' '--enable-gd-native-ttf' '--enable-libxml' '--enable-magic-quotes' '--enable-mbstring' '--enable-pdo=shared' '--enable-soap' '--enable-sockets' '--prefix=/usr' '--with-bz2' '--with-config-file-path=/usr/local/lib' '--with-config-file-scan-dir=/usr/local/lib/php.ini.d' '--with-curl=/opt/curlssl/' '--with-curlwrappers' '--with-freetype-dir=/usr' '--with-gd' '--with-gettext' '--with-imap=/opt/php_with_imap_client/' '--with-imap-ssl=/usr' '--with-jpeg-dir=/usr' '--with-kerberos' '--with-libxml-dir=/opt/xml2' '--with-libxml-dir=/opt/xml2/' '--with-mcrypt=/opt/libmcrypt/' '--with-mhash=/opt/mhash/' '--with-mime-magic' '--with-ming=/opt/ming/' '--with-mysql=/usr' '--with-mysql-sock=/var/lib/mysql/mysql.sock' '--with-openssl=/usr' '--with-openssl-dir=/usr' '--with-pdo-mysql=shared' '--with-pdo-sqlite=shared' '--with-png-dir=/usr' '--with-pspell' '--with-sqlite=shared' '--with-ttf' '--with-xpm-dir=/usr' '--with-xsl=/opt/xslt/' '--with-zlib' '--with-zlib-dir=/usr'

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
NerdsOfTechTechnology ScientistCommented:
Do you have a script that you include that references the connection. If so, you may want to check to see if any directories are references as you may be dealing with a different OS

instead of /home/ you may be dealing with /usr/

etc
0
 
gunny051499Commented:
Hi ewan69,

nerdsoftech could be right if you were using absolute pathnames. But I guess it would not even run on one single external server at that point.

Were you able to have a look at the "mysql_connect" function and see if the returned handle is checked for errors in your code? Putting "mysql_error()" right after it would then describe it all.

Have you access to the error log on the other 2 servers? If so, please have a look at them. They should also include PHP warnings and therefor tell you what you don't see on your webpage (maybe just doing a "show source" on the webpage itself will give you a hint as well though!).

Cheers

Gunny
0
 
ewan69Author Commented:
Hi, sorry for leaving it so long to reply...
If I change any log in details I get the first error attached to prove error report is on.

LINE 10:  $$link = mysql_connect($server, $username, $password);

Change the details back to what they should be and I get the problem error back, works on some servers but not others... The php log file on the server just shows the same errror.

LINE 25:$result = mysql_query($query, $$link) or db_error($query, mysql_errno(), mysql_error());


I have other php pages accessing the same database without problems so it must be the methods used in database.php

Thank you all for your help.

**********error with incorrect details*******
<b>Warning</b>:  mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Unknown
 MySQL server host '*********.co.uk' (1) in <b>/home/*******/public_html/taxi/database.php</b>
 on line <b>10</b><br />
Unable to connect to database server!
**********************************************
 
 
**********error on some servers but not others***********
<b>Warning</b>:  mysql_query(): supplied argument is not a valid MySQL-Link resource in <b>/home/******
/public_html/taxi/database.php</b> on line <b>25</b><br />
***********************************************************
 
 
the whole database.php file
 
<?php
 
//Make the database connection.
db_connect() or die('Unable to connect to database server!');
 
 
function db_connect($server = '**********', $username = '********', $password = '**********', $database = '***********', $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"><b>'.$errno.'-'.$error.'<br><br>'.$query.'<br><br><small><font color="#ff0000">[STOP]</font></small><br><br></b></font>');
}
 
//Function to query the database.
function db_query($query, $link = 'db_link') 
{
global $$link;
$result = mysql_query($query, $$link) or db_error($query, mysql_errno(), mysql_error());
return $result;
}
 
//Get a row from the database query
function db_fetch_array($db_query) 
{
return mysql_fetch_array($db_query, MYSQL_ASSOC);
}
 
//The the number of rows returned from the query.
function db_num_rows($db_query) 
{
return mysql_num_rows($db_query);
}
 
//Get the last auto_increment ID.
function db_insert_id() 
{
return mysql_insert_id();
}
 
 
//Add HTML character incoding to strings
function db_output($string) 
{
return htmlspecialchars($string);
}
 
//Add slashes to incoming data
function db_input($string, $link = 'db_link') 
{
global $$link;
	if (function_exists('mysql_real_escape_string')) 
	{
      	return mysql_real_escape_string($string, $$link);
    	} 
	elseif (function_exists('mysql_escape_string')) 
	{
     	return mysql_escape_string($string);
    	}
return addslashes($string);
}
 
 
?>

Open in new window

0
 
ewan69Author Commented:
Hi Gunny, thank you very much for your help, nice to have things explained, all working now.
:)
Ewan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.