Solved

Won't work on public server mysql_query():

Posted on 2009-05-03
7
325 Views
Last Modified: 2013-12-12
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

0
Comment
Question by:ewan69
  • 3
  • 3
7 Comments
 
LVL 4

Expert Comment

by:gunny051499
ID: 24290535
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
 

Author Comment

by:ewan69
ID: 24293357
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
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24293420
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 4

Expert Comment

by:gunny051499
ID: 24296670
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
 

Author Comment

by:ewan69
ID: 24374628
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
 
LVL 4

Accepted Solution

by:
gunny051499 earned 500 total points
ID: 24380312
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
 

Author Closing Comment

by:ewan69
ID: 31577347
Hi Gunny, thank you very much for your help, nice to have things explained, all working now.
:)
Ewan
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now