Trouble passing mysql table name to php function and using it!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kennthompson@gmail.com

    Trouble passing mysql table name to php function and using it!

    Trouble passing mysql table name in php. If I use an existing table
    name already defined everything works fine as the following script
    illustrates.

    <?php
    function fms_get_info()
    {
    $result = mysql_query("se lect * from $tableInfo") ;
    for ($i = 0; $i < mysql_num_rows( $result); $i++)
    {
    /* do something */
    }



    }


    /* Main */
    fms_get_info();

    But it won't work if I pass a variable table name to the function.


    <?php
    function fms_get_info($t ableName)
    {
    $result = mysql_query("se lect * from $tableName") ;
    for ($i = 0; $i < mysql_num_rows( $result); $i++)
    {
    /* do something */
    }



    }


    /* Main */
    fms_get_info($t ableInfo);

    I need to use the same function to gather information from multiple
    tables at will without creating a different function for each
    possible
    mysql database table by name. I thought this would be easy, but I
    have failed at several tries.

  • Jerry Stuckle

    #2
    Re: Trouble passing mysql table name to php function and using it!

    kennthompson@gm ail.com wrote:
    Trouble passing mysql table name in php. If I use an existing table
    name already defined everything works fine as the following script
    illustrates.
    >
    <?php
    function fms_get_info()
    {
    $result = mysql_query("se lect * from $tableInfo") ;
    for ($i = 0; $i < mysql_num_rows( $result); $i++)
    {
    /* do something */
    }
    >
    >
    >
    }
    >
    >
    /* Main */
    fms_get_info();
    >
    But it won't work if I pass a variable table name to the function.
    >
    >
    <?php
    function fms_get_info($t ableName)
    {
    $result = mysql_query("se lect * from $tableName") ;
    for ($i = 0; $i < mysql_num_rows( $result); $i++)
    {
    /* do something */
    }
    >
    >
    >
    }
    >
    >
    /* Main */
    fms_get_info($t ableInfo);
    >
    I need to use the same function to gather information from multiple
    tables at will without creating a different function for each
    possible
    mysql database table by name. I thought this would be easy, but I
    have failed at several tries.
    >
    This should work fine. What do you get back as an error message? How
    are you calling the function?

    What happens if you do the following:

    function fms_get_info($t ableName)
    {
    $sql = "select * from $tableName";
    echo $sql . "<br>\n";
    $result = mysql_query($sq l) ;
    for ($i = 0; $i < mysql_num_rows( $result); $i++)
    {
    /* do something */
    }

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • kennthompson@gmail.com

      #3
      Re: Trouble passing mysql table name to php function and using it!

      On Feb 27, 10:27 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
      kennthomp...@gm ail.com wrote:
      Trouble passing mysql table name in php. If I use an existing table
      name already defined everything works fine as the following script
      illustrates.
      >
      <?php
      function fms_get_info()
      {
      $result = mysql_query("se lect * from $tableInfo") ;
      for ($i = 0; $i < mysql_num_rows( $result); $i++)
      {
      /* do something */
      }
      >
      }
      >
      /* Main */
      fms_get_info();
      >
      But it won't work if I pass a variable table name to the function.
      >
      <?php
      function fms_get_info($t ableName)
      {
      $result = mysql_query("se lect * from $tableName") ;
      for ($i = 0; $i < mysql_num_rows( $result); $i++)
      {
      /* do something */
      }
      >
      }
      >
      /* Main */
      fms_get_info($t ableInfo);
      >
      I need to use the same function to gather information from multiple
      tables at will without creating a different function for each
      possible
      mysql database table by name. I thought this would be easy, but I
      have failed at several tries.
      >
      This should work fine. What do you get back as an error message? How
      are you calling the function?
      >
      What happens if you do the following:
      >
      function fms_get_info($t ableName)
      {
      $sql = "select * from $tableName";
      echo $sql . "<br>\n";
      $result = mysql_query($sq l) ;
      for ($i = 0; $i < mysql_num_rows( $result); $i++)
      {
      /* do something */
      }
      >
      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstuck...@attgl obal.net
      =============== ===- Hide quoted text -
      >
      - Show quoted text -


      No. That's exactly the way I was trying to do it. MySQL doesn't accept
      a table name passed into a function this way. There must be some
      hidden mysql code that I'm unaware of.


      Comment

      • Rik

        #4
        Re: Trouble passing mysql table name to php function and using it!

        <kennthompson@g mail.comwrote:
        Jerry Stuckle <jstuck...@attg lobal.netwrote:
        >kennthomp...@g mail.com wrote:
        Trouble passing mysql table name in php. If I use an existing table
        name already defined everything works fine as the following script
        illustrates.
        But it won't work if I pass a variable table name to the function.
        >>
        <?php
        function fms_get_info($t ableName)
        {
        $result = mysql_query("se lect * from $tableName") ;
        for ($i = 0; $i < mysql_num_rows( $result); $i++)
        {
        /* do something */
        }
        >>
        }
        >This should work fine. What do you get back as an error message? How
        >are you calling the function?
        >>
        >What happens if you do the following:
        >>
        >function fms_get_info($t ableName)
        >{
        > $sql = "select * from $tableName";
        > }
        >
        No. That's exactly the way I was trying to do it. MySQL doesn't accept
        a table name passed into a function this way. There must be some
        hidden mysql code that I'm unaware of.
        Jerry did ask for you to echo mysql_error...

        But here you go:
        function fms_get_info($t ableName){
        $qry = "SELECT * FROM `{$tableName}`" ;
        $res = mysql_query($qr y) or die($qry.' failed, mysql
        sais:'.mysql_er ror());
        }

        --
        Rik Wasmus

        Comment

        • Tim Streater

          #5
          Re: Trouble passing mysql table name to php function and using it!

          In article <op.toe7s3uqqnv 3q9@misant>, Rik <luiheidsgoeroe @hotmail.com>
          wrote:
          <kennthompson@g mail.comwrote:
          Jerry Stuckle <jstuck...@attg lobal.netwrote:
          kennthomp...@gm ail.com wrote:
          Trouble passing mysql table name in php. If I use an existing table
          name already defined everything works fine as the following script
          illustrates.
          But it won't work if I pass a variable table name to the function.
          >
          <?php
          function fms get info($tableName )
          {
          $result = mysql query("select * from $tableName") ;
          for ($i = 0; $i < mysql num rows($result); $i++)
          {
          /* do something */
          }
          >
          }
          This should work fine. What do you get back as an error message? How
          are you calling the function?
          >
          What happens if you do the following:
          >
          function fms get info($tableName )
          {
          $sql = "select * from $tableName";
          }
          No. That's exactly the way I was trying to do it. MySQL doesn't accept
          a table name passed into a function this way. There must be some
          hidden mysql code that I'm unaware of.
          >
          Jerry did ask for you to echo mysql error...
          >
          But here you go:
          function fms get info($tableName ){
          $qry = "SELECT * FROM `{$tableName}`" ;
          $res = mysql query($qry) or die($qry.' failed, mysql
          sais:'.mysql error());
          }
          Why does one need the back ticks and { ?? I would have expected one
          could construct a query with:

          $tab = "thistable" ;
          $query = "select * from " . $tab;
          $res = mysql_query ($query);

          or thereabouts. What does the extra stuff do?

          Thanks,

          Comment

          • Michael Fesser

            #6
            Re: Trouble passing mysql table name to php function and using it!

            ..oO(kennthomps on@gmail.com)
            >On Feb 27, 10:27 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
            >>
            >What happens if you do the following:
            >>
            >function fms_get_info($t ableName)
            >{
            > $sql = "select * from $tableName";
            >[...]
            >
            >No. That's exactly the way I was trying to do it.
            Then it would work, assuming that $tableName contains a proper table
            name.
            >MySQL doesn't accept
            >a table name passed into a function this way.
            It doesn't matter whether the table name is hard-wired or stored in a
            variable. The final query string as seen by MySQL will be the same.
            >There must be some
            >hidden mysql code that I'm unaware of.
            Nope. Just make use of the error reporting features that PHP/MySQL
            offer.

            Micha

            Comment

            • kennthompson@gmail.com

              #7
              Re: Trouble passing mysql table name to php function and using it!

              On Feb 27, 2:46 pm, Michael Fesser <neti...@gmx.de wrote:
              .oO(kennthomp.. .@gmail.com)
              >
              On Feb 27, 10:27 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
              >
              What happens if you do the following:
              >
              function fms_get_info($t ableName)
              {
              $sql = "select * from $tableName";
              [...]
              >
              No. That's exactly the way I was trying to do it.
              >
              Then it would work, assuming that $tableName contains a proper table
              name.
              >
              MySQL doesn't accept
              a table name passed into a function this way.
              >
              It doesn't matter whether the table name is hard-wired or stored in a
              variable. The final query string as seen by MySQL will be the same.
              >
              There must be some
              hidden mysql code that I'm unaware of.
              >
              Nope. Just make use of the error reporting features that PHP/MySQL
              offer.
              >
              Micha
              I understand what you are saying, but I have written the code using
              the same variable name that is first defined as the table name within
              the function and everything works fine, but when I change it to a
              variable name it does not work. I makes no sense to me. I'm just
              telling you the facts. Here is an extract of the code: (By the way,
              the working model only worked if I declared the outside table name
              global. The following version does not include this statement. I've
              tried dozens of variations. None seem to work passing the mysql table
              name to the function.


              function fms_get_table($ tableName, $label, $name, $var)
              {
              if (is_array($var) )
              {
              $var = $var[$name];
              }
              fms_open_table_ row();
              fms_open_table_ col();
              echo "$label:";
              fms_close_table _col();
              fms_close_table _row();
              echo "<SELECT size=1 name=project>\n ";
              $result2 = fms_mysql_query ("SELECT * FROM '{$tableName}'" );
              if ($result2)
              {
              $row2 = mysql_fetch_arr ay($result2);
              if (strlen($row2["projectNam e"]) 0)
              {
              echo "<OPTION VALUE=" . $row2["keyid"] .
              ">" .
              $row2["projectNam e"] . "\n";
              }
              }
              else
              {
              echo "<option&nbsp;\ n";
              }
              $result2 = fms_mysql_query ("select * from '{$tableName}' order
              by
              keyid asc");
              for ($i = 0; $i < mysql_num_rows( $result2); $i++)
              {
              $row2 = mysql_fetch_arr ay($result2);
              if (strlen($row2["projectNam e"]) 0)
              {
              echo "<OPTION VALUE=" . $row2["keyid"] .
              ">" .
              $row2["projectNam e"] . "\n";
              }
              }
              echo "</SELECT>\n";
              fms_close_table _col();
              fms_close_table _row();


              }


              /* Calling script: an edit form */

              $query = "select * from $tableFields where keyid = '$keyid'";
              $result = mysql_query($qu ery);
              if ($result)
              {
              $row = mysql_fetch_arr ay($result);


              }


              fms_break(1);
              fms_open_center ();
              fms_open_form(" edit", "updateField.ph p", "post");
              fms_open_table( 0, "#eeeeee", "", 2, 2);
              fms_hidden("key id", $row);

              fms_get_table($ tableTarget, "Project", "project", $row);


              fms_get_value(" Label", "label", 60, $row);
              fms_get_value(" Field", "field", 60, $row);
              fms_get_value(" Type", "type", 60, $row);
              fms_get_value(" Parameters", "parameters ", 60, $row);
              fms_get_value(" Attributes", "attributes ", 60, $row);
              fms_get_value(" Extra", "extra", 60, $row);
              fms_get_value(" FMS", "fms", 60, $row);
              fms_get_value(" Cols", "cols", 10, $row);
              fms_get_value(" Rows", "rows", 10, $row);
              fms_close_table ();
              fms_break(1);
              fms_submit("UPD ATE");
              fms_submit("DEL ETE");
              fms_submit("DUP LICATE");
              fms_close_form( );
              fms_close_cente r();


              /* mysql connection */
              $host = "localhost" ;
              $user = "";
              $password = "";
              $database = "";
              $tableTarget = "php_generator_ targets";
              $tableFields = "php_generator_ data_fields";
              $link = mysql_pconnect( $host, $user, $password );
              if(!$link)
              {
              echo "Did not connect.";


              }


              else
              {
              mysql_select_db ($database);
              if(mysql_errno( ))
              {
              echo mysql_errno() . ":" . mysql_error();
              exit;
              }


              }


              /* Main */
              A shell gets the mysql connection, and manipulates the display. The
              edit form tried to extract information from another table. It works
              fine if I write a piece of code for each call, but I want the same
              function to work for a number of database tables. But when I tried
              passing the table name -- to my surprise it did not work. I've tried
              numerous variations without success.


              Comment

              • Rik

                #8
                Re: Trouble passing mysql table name to php function and using it!

                Tim Streater <timstreater@wa itrose.comwrote :
                >But here you go:
                >function fms get info($tableName ){
                > $qry = "SELECT * FROM `{$tableName}`" ;
                > $res = mysql query($qry) or die($qry.' failed, mysql
                >sais:'.mysql error());
                >}
                >
                Why does one need the back ticks and { ?? I would have expected one
                could construct a query with:
                Backticks, to make sure it even works when a table has a reserved
                name('order' or something comes to mind). The accolades are just for my
                benefit: I've taken to it to always use them in double quoted strings, it
                doesn't cost anything extra, and you're always sure you haven't forgotten
                when using array values or object properties.
                --
                Rik Wasmus

                Comment

                • Rik

                  #9
                  Re: Trouble passing mysql table name to php function and using it!

                  On Tue, 27 Feb 2007 23:47:17 +0100, <kennthompson@g mail.comwrote:
                  $result2 = fms_mysql_query ("SELECT * FROM '{$tableName}'" );
                  backticks (`) are _not_ single quotes(')...

                  Use "SELECT * FROM `{$tableName}`"

                  --
                  Rik Wasmus

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: Trouble passing mysql table name to php function and using it!

                    kennthompson@gm ail.com wrote:
                    On Feb 27, 10:27 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                    >kennthomp...@g mail.com wrote:
                    >>Trouble passing mysql table name in php. If I use an existing table
                    >>name already defined everything works fine as the following script
                    >>illustrates .
                    >><?php
                    >>function fms_get_info()
                    >>{
                    >> $result = mysql_query("se lect * from $tableInfo") ;
                    >> for ($i = 0; $i < mysql_num_rows( $result); $i++)
                    >> {
                    >> /* do something */
                    >> }
                    >>}
                    >>/* Main */
                    >>fms_get_info( );
                    >>But it won't work if I pass a variable table name to the function.
                    >><?php
                    >>function fms_get_info($t ableName)
                    >>{
                    >> $result = mysql_query("se lect * from $tableName") ;
                    >> for ($i = 0; $i < mysql_num_rows( $result); $i++)
                    >> {
                    >> /* do something */
                    >> }
                    >>}
                    >>/* Main */
                    >>fms_get_info( $tableInfo);
                    >>I need to use the same function to gather information from multiple
                    >>tables at will without creating a different function for each
                    >>possible
                    >>mysql database table by name. I thought this would be easy, but I
                    >>have failed at several tries.
                    >This should work fine. What do you get back as an error message? How
                    >are you calling the function?
                    >>
                    >What happens if you do the following:
                    >>
                    >function fms_get_info($t ableName)
                    >{
                    > $sql = "select * from $tableName";
                    > echo $sql . "<br>\n";
                    > $result = mysql_query($sq l) ;
                    > for ($i = 0; $i < mysql_num_rows( $result); $i++)
                    > {
                    > /* do something */
                    > }
                    >>
                    >--
                    >============== ====
                    >Remove the "x" from my email address
                    >Jerry Stuckle
                    >JDS Computer Training Corp.
                    >jstuck...@attg lobal.net
                    >============== ====- Hide quoted text -
                    >>
                    >- Show quoted text -
                    >
                    >
                    >
                    No. That's exactly the way I was trying to do it. MySQL doesn't accept
                    a table name passed into a function this way. There must be some
                    hidden mysql code that I'm unaware of.
                    >
                    >
                    It works. Did you do as I asked? Echo the sql first, then the error
                    returned by mysql.

                    It's not PHP nor MySQL that's your problem.


                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===

                    Comment

                    • Tim Streater

                      #11
                      Re: Trouble passing mysql table name to php function and using it!

                      In article <op.tofqiap6qnv 3q9@misant>, Rik <luiheidsgoeroe @hotmail.com>
                      wrote:
                      Tim Streater <timstreater@wa itrose.comwrote :
                      But here you go:
                      function fms get info($tableName ){
                      $qry = "SELECT * FROM `{$tableName}`" ;
                      $res = mysql query($qry) or die($qry.' failed, mysql
                      sais:'.mysql error());
                      }
                      Why does one need the back ticks and { ?? I would have expected one
                      could construct a query with:
                      >
                      Backticks, to make sure it even works when a table has a reserved
                      name('order' or something comes to mind). The accolades are just for my
                      benefit: I've taken to it to always use them in double quoted strings, it
                      doesn't cost anything extra, and you're always sure you haven't forgotten
                      when using array values or object properties.
                      Ah, thanks, that's clear. In fact I don't usually have a variable
                      tablename when constructing a query so I haven't been hit by that.

                      But a question to the OP might be - what debugging have you done?
                      Whenever I get an oddity of this nature I usually spend some time
                      carefully debugging before posting about it.

                      -- tim

                      Comment

                      • Rik

                        #12
                        Re: Trouble passing mysql table name to php function and using it!

                        Jerry Stuckle <jstucklex@attg lobal.netwrote:
                        It's not PHP nor MySQL that's your problem.
                        Tssssk :P.
                        --
                        Rik Wasmus

                        Comment

                        Working...