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? 6 4634
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
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
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?
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?
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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";
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
/**
*...
|
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?...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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,...
| |