473,750 Members | 2,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_a nsi_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_a nsi_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 4672
On Fri, 14 Jan 2005 23:25:14 -0000, "aa" <aa@virgin.ne t> 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_d b ($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_d b ($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_a nsi_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_a nsi_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.andyhsoftwa re.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_na me;
$db_host = "humbug";
function write_table()
{
$username = "myusername ";
......... $password, $DB_name,$DB_na me 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_a nsi_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($ch annel)
{
$resultid = mysql_query ("select name_ru, description_ru, retail, dealer
from lasershot WHERE le='1'", $channel);
.......
}
write_table($ch an);

See if that helps.

--
Paul Barfoot

"aa" <aa@virgin.ne t> wrote in message
news:41******** *************** @ptn-nntp-reader03.plus.n et...
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.ne t> wrote in message
news:41******** *************** @ptn-nntp-reader04.plus.n et...
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_a nsi_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_a nsi_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
28470
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
7836
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 fields Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/themepar/public_html/changepass.php on line 34 Sorry You failed to enter the correct old password I was wondering if anyone could help me, I have...
2
16932
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 not a valid MySQL result resource in /home/jplane/certcent/phpweb/quiz/index.php on line 21 PHP CODE:
1
2709
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 line 113 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/mamadele/public_html/BESTPLAYS/search.php on line 127 My code is below. Any help would be much appreciated. ...
11
3602
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 when there is no result "empty table" how can i do a quick fix to say No Results... row 88: if ($myrow = mysql_fetch_array($result)) { do { if ($rowcolor == 1) {
2
2198
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 /home/asi50080/public_html/onlineadv/category.php on line 143 Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/asi50080/public_html/onlineadv/category.php on line 155
0
5622
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 Markus' first installment: 1: Headers Already Sent.
9
4530
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 /** * Database.php * * The Database class is meant to simplify the task of accessing
1
1934
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? Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/xxxx/public_html/propview.php on line 78 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in...
0
9577
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9396
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9339
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6804
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4713
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.