473,221 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

mysql_query(): supplied argument is not a valid MySQL-Link resource

aa
I use the following fragment of code to output datf from MySQL:

================================================== ====
$chan = mysql_connect ($db_host, $username, $password);
mysql_select_db ($DB_name, $chan);

$resultid = mysql_query ("select name_ru, description_ru, retail, dealer
from lasershot WHERE le='1'", $chan);
........
================================================== ====
This was working fine.

Then I needed to repeat this code (except for the first two lines) several
times, every time for a different value of le

I placed everything starting from lime 3 inside {}, made is a function and
called this function like that:

$chan = mysql_connect ($db_host, $username, $password);
mysql_select_db ($DB_name, $chan);
function write_table()
{
$resultid = mysql_query ("select name_ru, description_ru, retail, dealer
from lasershot WHERE le='1'", $chan);
.......
}
write_table();

Now I am getting this error:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource
in /files/home2/andrei/lasershot/pricelist_sql_ansi_split.php on line 41
(line 41 is the former line 3:
$resultid = mysql_query ("select name_ru, description_ru, retail, dealer
from lasershot WHERE le='1'", $chan);

Why does the argument stopped being valid?

When I moved the first two lines inside the function, the line

$chan = mysql_connect ($db_host, $username, $password);

started generating error:
Warning: mysql_connect(): Can't connect to local MySQL server through socket
'/tmp/mysql.sock' (2) in
/files/home2/andrei/lasershot/pricelist_sql_ansi_split.php on line 40

Does this mean that mysql_query() and mysql_connect() cannot be called from
within a function?


Jul 17 '05 #1
6 4619
On Fri, 14 Jan 2005 23:25:14 -0000, "aa" <aa@virgin.net> wrote:
I use the following fragment of code to output datf from MySQL:

================================================= =====
$chan = mysql_connect ($db_host, $username, $password);
You haven't checked for errors, and if this fails, you're just continuing
without a connection. All further MySQL calls will fail.
mysql_select_db ($DB_name, $chan);
No error checking.
$resultid = mysql_query ("select name_ru, description_ru, retail, dealer
from lasershot WHERE le='1'", $chan);
No error checking.

For every call to mysql_*, check the return for 'false'. If it's false,
there's an error, mysql_error() tells you what's up, and you generally have to
bail out of the script there since your connect/select database/query have
failed.
.......
================================================= =====
This was working fine.

Then I needed to repeat this code (except for the first two lines) several
times, every time for a different value of le

I placed everything starting from lime 3 inside {}, made is a function and
called this function like that:

$chan = mysql_connect ($db_host, $username, $password);
mysql_select_db ($DB_name, $chan);
function write_table()
{
$resultid = mysql_query ("select name_ru, description_ru, retail, dealer
from lasershot WHERE le='1'", $chan);
$chan isn't in scope here. PHP has a somewhat unusal scoping system. Rather
than global variables always being visible, when you're inside a function you
must bring them into scope using a 'global' statement.

Precede the function call with:

global $chan;
......
}
write_table();

Now I am getting this error:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource
in /files/home2/andrei/lasershot/pricelist_sql_ansi_split.php on line 41
(line 41 is the former line 3:
$resultid = mysql_query ("select name_ru, description_ru, retail, dealer
from lasershot WHERE le='1'", $chan);

Why does the argument stopped being valid?
Again, PHP's scoping system:
http://uk2.php.net/manual/en/languag...bles.scope.php
When I moved the first two lines inside the function, the line

$chan = mysql_connect ($db_host, $username, $password);

started generating error:
Warning: mysql_connect(): Can't connect to local MySQL server through socket
'/tmp/mysql.sock' (2) in
/files/home2/andrei/lasershot/pricelist_sql_ansi_split.php on line 40

Does this mean that mysql_query() and mysql_connect() cannot be called from
within a function?


No - once you moved it inside the function, none of $db_host, $username or
$password had values, so it would be taking the defaults (connect to hardcoded
socket name using null username and password), which typically won't work.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2
aa
Thanks, Andy, it makes a lot of sense. Even the requirement to expressly
declare a variable as a global one.
However it did not sort my problem out. I played with one variable $db_host:
================================================== ====
global $chan, $db_host, $username, $password, $DB_name,$DB_name;
$db_host = "humbug";
function write_table()
{
$username = "myusername";
......... $password, $DB_name,$DB_name are assigned values here
$chan = mysql_connect ($db_host, $username, $password);
if ($chan==false)
mysql_error();
......
}
===================================
produces the same error as before:
Warning: mysql_connect(): Can't connect to local MySQL server through socket
'/tmp/mysql.sock' (2) in
/files/home2/andrei/lasershot/pricelist_sql_ansi_split.php on line 42

However if I move
$db_host = "humbug";
inside the function it works fine. Which means that the global variable are
not seen inside a function, or I declare them wrongly

Jul 17 '05 #3
aa
I looked up "PHP and MySQL Web Developement" by Luke Welling and Laura
Thomson.
In the para "Scope of variables" it reads (the book is translated into
Russian and I translate is back into English):
"Variables declared as global in a scenario are seen throughout the
scenario, but not seen from within functions. By default all the variables
declared in a scenario outside functions are global."

It looks like if query MySQL from a function, I will have to put all the
database related variables inside the function. And therefore to open and
close a connection to the database very time I run a query.
In my case I am building a page by sending different queries to the
database.
Opening and closing the connection for every query looks like an unnecessary
overhead.

Can I get round it?


Jul 17 '05 #4
Hi aa

When I had my first attempt at using functions in a PHP script I had similar
problems. I got round it by passing all variables outside the function into
it in the function call.

function write_table()
{
$resultid = mysql_query ("select name_ru, description_ru, retail, dealer
from lasershot WHERE le='1'", $chan);
.......
}
write_table();

becomes

function write_table($channel)
{
$resultid = mysql_query ("select name_ru, description_ru, retail, dealer
from lasershot WHERE le='1'", $channel);
.......
}
write_table($chan);

See if that helps.

--
Paul Barfoot

"aa" <aa@virgin.net> wrote in message
news:41***********************@ptn-nntp-reader03.plus.net...
I looked up "PHP and MySQL Web Developement" by Luke Welling and Laura
Thomson.
In the para "Scope of variables" it reads (the book is translated into
Russian and I translate is back into English):
"Variables declared as global in a scenario are seen throughout the
scenario, but not seen from within functions. By default all the variables
declared in a scenario outside functions are global."

It looks like if query MySQL from a function, I will have to put all the
database related variables inside the function. And therefore to open and
close a connection to the database very time I run a query.
In my case I am building a page by sending different queries to the
database.
Opening and closing the connection for every query looks like an
unnecessary
overhead.

Can I get round it?

Jul 17 '05 #5
aa
That's and idea. Thanks
Jul 17 '05 #6
"aa" <aa@virgin.net> wrote in message
news:41***********************@ptn-nntp-reader04.plus.net...
I use the following fragment of code to output datf from MySQL:

================================================== ====
$chan = mysql_connect ($db_host, $username, $password);
mysql_select_db ($DB_name, $chan);

$resultid = mysql_query ("select name_ru, description_ru, retail, dealer
from lasershot WHERE le='1'", $chan);
.......
================================================== ====
This was working fine.

Then I needed to repeat this code (except for the first two lines) several
times, every time for a different value of le

I placed everything starting from lime 3 inside {}, made is a function and called this function like that:

$chan = mysql_connect ($db_host, $username, $password);
mysql_select_db ($DB_name, $chan);
function write_table()
{
$resultid = mysql_query ("select name_ru, description_ru, retail, dealer
from lasershot WHERE le='1'", $chan);
......
}
write_table();

Now I am getting this error:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /files/home2/andrei/lasershot/pricelist_sql_ansi_split.php on line 41
(line 41 is the former line 3:
$resultid = mysql_query ("select name_ru, description_ru, retail, dealer
from lasershot WHERE le='1'", $chan);

Why does the argument stopped being valid?

When I moved the first two lines inside the function, the line

$chan = mysql_connect ($db_host, $username, $password);

started generating error:
Warning: mysql_connect(): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in
/files/home2/andrei/lasershot/pricelist_sql_ansi_split.php on line 40

Does this mean that mysql_query() and mysql_connect() cannot be called from within a function?


You need to:

function write_table()
{
global $chan; // this makes $chan visible to the function.
$resultid = mysql_query ("select name_ru, description_ru, retail, dealer
from lasershot WHERE le='1'", $chan);
......
}
Norm
---
FREE Avatar hosting at www.easyavatar.com

Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Burton Figg | last post by:
Before I get in trouble, I have searched extensively for this one, in the PHP Docs, online etc. I have a simple page: <?php $un="jim"; $pw="jim"; $db="localhost";
4
by: Ryanlawrence1 | last post by:
Heya, I get these 2 errors: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/themepar/public_html/changepass.php on line 20 You have not entered all the...
2
by: techjohnny | last post by:
Error: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/jplane/certcent/phpweb/quiz/index.php on line 20 Warning: mysql_num_rows(): supplied argument is...
1
by: lsmamadele | last post by:
I am getting the following error messages in my search: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/mamadele/public_html/BESTPLAYS/search.php on...
11
by: Breana | last post by:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/breana/public_html/category.php on line 88 ------------------------------------------- It does this...
2
by: perhapscwk | last post by:
When I run my site from localhost, no error, but when I move it to webhosting, it show below error, why? Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in...
0
Atli
by: Atli | last post by:
What to discuss: What is a "MySQL resource". What causes the error. How to fix it. Common Newbie Pitfalls This article is the second installment in a series of (hopefully) many, following...
9
by: Cxsey | last post by:
I get the following error code using this code: "Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource" It's on line 55 in this php script: <?php /** *...
1
by: kmacc | last post by:
Hi, I'm getting this error on a page after changing server host, the error did not happen on my old host. I'm thinking it is to do with a new MySQL version and more strict coding. Can anyone help?...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.