By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,852 Members | 2,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,852 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
aa
That's and idea. Thanks
Jul 17 '05 #6

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.