Re: mysql_query(): supplied argument is not a valid MySQL-Link resource
From: Andy Hassall (andy_at_andyh.co.uk)
Date: 01/15/05
- Next message: Virgil Green: "Re: huyges lans on titan"
- Previous message: aa: "mysql_query(): supplied argument is not a valid MySQL-Link resource"
- In reply to: aa: "mysql_query(): supplied argument is not a valid MySQL-Link resource"
- Next in thread: aa: "Re: mysql_query(): supplied argument is not a valid MySQL-Link resource"
- Reply: aa: "Re: mysql_query(): supplied argument is not a valid MySQL-Link resource"
- Reply: aa: "Re: mysql_query(): supplied argument is not a valid MySQL-Link resource"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 14 Jan 2005 23:45:49 +0000
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/language.variables.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 / <andy@andyh.co.uk> / <http://www.andyh.co.uk> <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
- Next message: Virgil Green: "Re: huyges lans on titan"
- Previous message: aa: "mysql_query(): supplied argument is not a valid MySQL-Link resource"
- In reply to: aa: "mysql_query(): supplied argument is not a valid MySQL-Link resource"
- Next in thread: aa: "Re: mysql_query(): supplied argument is not a valid MySQL-Link resource"
- Reply: aa: "Re: mysql_query(): supplied argument is not a valid MySQL-Link resource"
- Reply: aa: "Re: mysql_query(): supplied argument is not a valid MySQL-Link resource"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|