SQL Query - Results from another PHP Page ?

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

    SQL Query - Results from another PHP Page ?

    Can I store this PHP code in another page, then refernece it on
    multiple pages using Require or Include ???

    //results from sql query
    for ($i=0; $i<$number; $i++) {
    $ID = mysql_result($r esult,$i,"ID");
    $CompanyName = mysql_result($r esult,$i,"1");
    $ContactName = mysql_result($r esult,$i,"2");
    $Phone = mysql_result($r esult,$i,"3");
    $HomePhone = mysql_result($r esult,$i,"4");
    $MobilePhone = mysql_result($r esult,$i,"5");
    $AltPhone = mysql_result($r esult,$i,"6");
    $SecondContact = mysql_result($r esult,$i,"7");
    $SecondPhone = mysql_result($r esult,$i,"8");
    $ThirdContact = mysql_result($r esult,$i,"9");
    $ThirdPhone = mysql_result($r esult,$i,"10");
    $Notes = mysql_result($r esult,$i,"11")


    This will save me having to have all that on the pages its needed if I
    can be used from another page !



  • Gary Petersen

    #2
    Re: SQL Query - Results from another PHP Page ?

    On Tue, 15 Jul 2003 08:05:27 -0500, Frank wrote:
    [color=blue]
    > Can I store this PHP code in another page, then refernece it on multiple
    > pages using Require or Include ???
    >
    > //results from sql query
    > for ($i=0; $i<$number; $i++) {
    > $ID = mysql_result($r esult,$i,"ID");
    > $CompanyName = mysql_result($r esult,$i,"1"); $ContactName =
    > mysql_result($r esult,$i,"2"); $Phone = mysql_result($r esult,$i,"3");
    > $HomePhone = mysql_result($r esult,$i,"4"); $MobilePhone =
    > mysql_result($r esult,$i,"5"); $AltPhone =
    > mysql_result($r esult,$i,"6"); $SecondContact =
    > mysql_result($r esult,$i,"7"); $SecondPhone =
    > mysql_result($r esult,$i,"8"); $ThirdContact =
    > mysql_result($r esult,$i,"9"); $ThirdPhone =
    > mysql_result($r esult,$i,"10"); $Notes = mysql_result($r esult,$i,"11")
    >
    >
    > This will save me having to have all that on the pages its needed if I
    > can be used from another page ![/color]

    Why not just use mysql_fetch_arr ay() and access
    the fields by name? Try this:

    $myrow = mysql_fetch_arr ay ($result, MYSQL_BOTH);

    Now all of the fields will be in the $myrow array.
    You can access the values by number or name. So this
    code will print the company name.

    echo $myrow['1'];

    HTH

    Comment

    • Zac Hester

      #3
      Re: SQL Query - Results from another PHP Page ?

      <Frank @ MyPlace.Com (Frank)> wrote in message
      news:3f13f975.4 48075508@news.b tclick.com...[color=blue]
      > Can I store this PHP code in another page, then refernece it on
      > multiple pages using Require or Include ???
      >
      > //results from sql query
      > for ($i=0; $i<$number; $i++) {
      > $ID = mysql_result($r esult,$i,"ID");
      > $CompanyName = mysql_result($r esult,$i,"1");
      > $ContactName = mysql_result($r esult,$i,"2");
      > $Phone = mysql_result($r esult,$i,"3");
      > $HomePhone = mysql_result($r esult,$i,"4");
      > $MobilePhone = mysql_result($r esult,$i,"5");
      > $AltPhone = mysql_result($r esult,$i,"6");
      > $SecondContact = mysql_result($r esult,$i,"7");
      > $SecondPhone = mysql_result($r esult,$i,"8");
      > $ThirdContact = mysql_result($r esult,$i,"9");
      > $ThirdPhone = mysql_result($r esult,$i,"10");
      > $Notes = mysql_result($r esult,$i,"11")
      >
      >
      > This will save me having to have all that on the pages its needed if I
      > can be used from another page !
      >
      >
      >[/color]

      Not only can you, but you should. Code duplication should be avoided at all
      costs. It will save you so much time later if you consolidate repeated code
      into a single file. Just think of include() as a way of inserting an entire
      file in-line with your current file.

      Also, read the PHP manual about the mysql_result() function. It says:

      "When working on large result sets, you should consider using one of the
      functions that fetch an entire row (specified below). As these functions
      return the contents of multiple cells in one function call, they're MUCH
      quicker than mysql_result(). "

      "Recommende d high-performance alternatives: mysql_fetch_row (),
      mysql_fetch_arr ay(), and mysql_fetch_obj ect()."

      HTH,
      Zac


      Comment

      • Frank

        #4
        Re: SQL Query - Results from another PHP Page ?


        Tony, I'm new to this, can you point me in the right direction ??

        Thanks !

        [color=blue]
        >I would strongly recommend that you use a function which returns the
        >result set as an associative array so that you can access field values
        >using the field name rather than its sequence number in the array. Not
        >only does it make it easier to code, it also gets around the problem
        >that can be caused if the fields get returned in a different sequence.
        >
        >That's just my opinion.
        >
        >Tony Marston
        >http://www.tonymarston.net/[/color]

        Comment

        • Tony Marston

          #5
          Re: SQL Query - Results from another PHP Page ?

          Take a look in the PHP documentation for the MySQL functions at


          http://www.php.net/manual/en/functio...etch-array.php - Fetch a
          result row as an associative array, a numeric array, or both.

          http://www.php.net/manual/en/functio...etch-assoc.php - Fetch a
          result row as an associative array.

          http://www.php.net/manual/en/functio...tch-object.php - Fetch a
          result row as an object.

          Tony Marston


          Frank @ MyPlace.Com (Frank) wrote in message news:<3f151d35. 522763944@news. btclick.com>...[color=blue]
          > Tony, I'm new to this, can you point me in the right direction ??
          >
          > Thanks !
          >
          >[color=green]
          > >I would strongly recommend that you use a function which returns the
          > >result set as an associative array so that you can access field values
          > >using the field name rather than its sequence number in the array. Not
          > >only does it make it easier to code, it also gets around the problem
          > >that can be caused if the fields get returned in a different sequence.
          > >
          > >That's just my opinion.
          > >
          > >Tony Marston
          > >http://www.tonymarston.net/[/color][/color]

          Comment

          Working...