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

From: Andy Hassall (andy_at_andyh.co.uk)
Date: 01/15/05


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


Relevant Pages

  • Re: struts failed connection to remote mySQL
    ... > Then you don't have a listener on that port which is why your ... > connection attempt fails. ... MySQL Connector/J uses TCP connections only. ...
    (comp.lang.java.programmer)
  • Re: VFP8 & MySQL
    ... > VFP functions like NVL to a MySQL one, definitely it made the switch very ... > About your connection problem, I have mine setup as default for 100 ... As for the server, we left it ... The server crashed and corrupted the database. ...
    (microsoft.public.fox.programmer.exchange)
  • Re: Does apache stop a script mid execution ?
    ... If the user calls a time consuming script and then stops or refreshes ... How does it relate to e.g. a script performing a large mysql query? ... 2.b) how the connection is set up. ... the server stops the script execution. ...
    (comp.lang.php)
  • Re: The network path was not found
    ... I fail to open a second pc on my small network. ... Right click Local Area Connection. ... If it fails either way there's an IP or Name resolution problem. ... Pull the power from the router. ...
    (microsoft.public.windowsxp.help_and_support)
  • MySQL on Win 98
    ... I'm just getting into MySQL and I'm trying to learn about it at home. ... and created a DSN connection for it. ... User: root ... ODBC Data Source Administrator and get a "success" result. ...
    (microsoft.public.fox.programmer.exchange)