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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aa

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

    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?






  • Andy Hassall

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

    On Fri, 14 Jan 2005 23:25:14 -0000, "aa" <aa@virgin.ne t> wrote:
    [color=blue]
    >I use the following fragment of code to output datf from MySQL:
    >
    >============== =============== =============== ==========
    >$chan = mysql_connect ($db_host, $username, $password);[/color]

    You haven't checked for errors, and if this fails, you're just continuing
    without a connection. All further MySQL calls will fail.
    [color=blue]
    >mysql_select_d b ($DB_name, $chan);[/color]

    No error checking.
    [color=blue]
    >$resultid = mysql_query ("select name_ru, description_ru, retail, dealer
    >from lasershot WHERE le='1'", $chan);[/color]

    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.
    [color=blue]
    >.......
    >============== =============== =============== ==========
    >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);[/color]

    $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;
    [color=blue]
    >......
    >}
    > 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?[/color]

    Again, PHP's scoping system:

    [color=blue]
    >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?[/color]

    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.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    • aa

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

      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



      Comment

      • aa

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

        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?




        Comment

        • Paul Barfoot

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

          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:41e8fdb1$0 $22490$ed2619ec @ptn-nntp-reader03.plus.n et...[color=blue]
          >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?
          >
          >
          >
          >[/color]


          Comment

          • aa

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

            That's and idea. Thanks


            Comment

            • Norman Peelman

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

              "aa" <aa@virgin.ne t> wrote in message
              news:41e854da$0 $41092$ed2e19e4 @ptn-nntp-reader04.plus.n et...[color=blue]
              > 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[/color]
              and[color=blue]
              > 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[/color]
              resource[color=blue]
              > 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[/color]
              socket[color=blue]
              > '/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[/color]
              from[color=blue]
              > within a function?
              >
              >[/color]

              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



              Comment

              Working...