Help req. Reading data from MySQL db with PHP

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

    Help req. Reading data from MySQL db with PHP

    I have PHP code (below) which reads data from a MySQL format database. The
    problem I am having is trying to find out when the last ID entry was made.
    When the script is executed, the $gbID is supposed to be read and display
    the last entered ID number ($How_many_entr ies) - the ID number is entered /
    updated automatically in another PHP script which deals with data entry to
    the database.

    But instead of displaying the result of the query, it displays the ID
    number ($How_many_entr ies) as "Resource id #4", and looking around the
    search engines, no page makes particular sense as to what I've done wrong.

    Does anyone know how to get the $gbID number from the database and into
    $How_many_entri es so I can perform some math work on it?

    Thanks.

    Dariusz



    <?
    $DatabaseName = "Guestbook" ;
    $table_to_look_ for = "entries";

    $connection = @mysql_connect( "localhost" ) or die("<B>Could not connect to
    MySQL: </B>".mysql_error ());

    mysql_select_db ($DatabaseName) ;

    // get all tables in the database
    $result = @mysql_list_tab les ("DatabaseName" );

    // Display database entries in reverse order (remove 'DESC' to have forward
    display of results)
    $sql = "SELECT gbID, gbDate, gbIP, gbURL, gbName, gbComment FROM
    $table_to_look_ for ORDER BY gbid DESC";

    $result = @mysql_query($s ql, $connection) or die("<B>Problem reading from
    database: </B>".mysql_error ());

    while ($row = mysql_fetch_arr ay($result))
    {
    $gbID = $row['gbID'];
    $gbDate = gmstrftime('%A %d %B %Y at $T',strtotime($ row['gbDate']));
    $gbURL = $row['gbURL'];
    $gbName = $row['gbName'];
    $gbComment = nl2br($row['gbComment']);

    $display_entry .= "$gbID:<BR><B>P osted on: </B>$gbDate &nbsp;<B>IP:
    </B>Logged<BR><B> Posted by: </B>$gbName<BR><B >Personal website:
    </B>$gbURL<BR><B> Comment: </B>$gbComment<BR ><BR>";
    }

    $sql = "SELECT last_insert_id( $gbID) FROM $table_to_look_ for";
    $How_many_entri es = @mysql_query($s ql, $connection);

    // $How_many_entri es = $How_many_entri es / 5;
    echo "Entries: $How_many_entri es<BR>";

    etc....
  • Jørn-Inge Kristiansen

    #2
    Re: Help req. Reading data from MySQL db with PHP

    why not just use the mysql_num_rows( $query) function that php offers


    "Dariusz" <ng@lycaus.plus YOURSHIT.com> wrote in message
    news:xsIdb.240$ kA.86356@wards. force9.net...[color=blue]
    > I have PHP code (below) which reads data from a MySQL format database.[/color]
    The[color=blue]
    > problem I am having is trying to find out when the last ID entry was made.
    > When the script is executed, the $gbID is supposed to be read and display
    > the last entered ID number ($How_many_entr ies) - the ID number is entered[/color]
    /[color=blue]
    > updated automatically in another PHP script which deals with data entry to
    > the database.
    >
    > But instead of displaying the result of the query, it displays the ID
    > number ($How_many_entr ies) as "Resource id #4", and looking around the
    > search engines, no page makes particular sense as to what I've done wrong.
    >
    > Does anyone know how to get the $gbID number from the database and into
    > $How_many_entri es so I can perform some math work on it?
    >
    > Thanks.
    >
    > Dariusz
    >
    >
    >
    > <?
    > $DatabaseName = "Guestbook" ;
    > $table_to_look_ for = "entries";
    >
    > $connection = @mysql_connect( "localhost" ) or die("<B>Could not connect to
    > MySQL: </B>".mysql_error ());
    >
    > mysql_select_db ($DatabaseName) ;
    >
    > // get all tables in the database
    > $result = @mysql_list_tab les ("DatabaseName" );
    >
    > // Display database entries in reverse order (remove 'DESC' to have[/color]
    forward[color=blue]
    > display of results)
    > $sql = "SELECT gbID, gbDate, gbIP, gbURL, gbName, gbComment FROM
    > $table_to_look_ for ORDER BY gbid DESC";
    >
    > $result = @mysql_query($s ql, $connection) or die("<B>Problem reading from
    > database: </B>".mysql_error ());
    >
    > while ($row = mysql_fetch_arr ay($result))
    > {
    > $gbID = $row['gbID'];
    > $gbDate = gmstrftime('%A %d %B %Y at $T',strtotime($ row['gbDate']));
    > $gbURL = $row['gbURL'];
    > $gbName = $row['gbName'];
    > $gbComment = nl2br($row['gbComment']);
    >
    > $display_entry .= "$gbID:<BR><B>P osted on: </B>$gbDate &nbsp;<B>IP:
    > </B>Logged<BR><B> Posted by: </B>$gbName<BR><B >Personal website:
    > </B>$gbURL<BR><B> Comment: </B>$gbComment<BR ><BR>";
    > }
    >
    > $sql = "SELECT last_insert_id( $gbID) FROM $table_to_look_ for";
    > $How_many_entri es = @mysql_query($s ql, $connection);
    >
    > // $How_many_entri es = $How_many_entri es / 5;
    > echo "Entries: $How_many_entri es<BR>";
    >
    > etc....[/color]


    Comment

    • Andy Hassall

      #3
      Re: Help req. Reading data from MySQL db with PHP

      On Sun, 28 Sep 2003 21:32:51 GMT, ng@lycaus.plusY OURSHIT.com (Dariusz) wrote:
      [color=blue]
      >I have PHP code (below) which reads data from a MySQL format database. The
      >problem I am having is trying to find out when the last ID entry was made.[/color]

      From the 'when' above, it sounds like you want date and time?
      [color=blue]
      >When the script is executed, the $gbID is supposed to be read and display
      >the last entered ID number ($How_many_entr ies) - the ID number is entered /
      >updated automatically in another PHP script which deals with data entry to
      >the database.[/color]

      But now you say you only want the last allocated ID number?
      [color=blue]
      >But instead of displaying the result of the query, it displays the ID
      >number ($How_many_entr ies) as "Resource id #4", and looking around the
      >search engines, no page makes particular sense as to what I've done wrong.[/color]

      That would mean you're using a result set resource directly in a print
      statement, rather than fetching rows from it.
      [color=blue]
      >Does anyone know how to get the $gbID number from the database and into
      >$How_many_entr ies so I can perform some math work on it?[/color]

      Another confusion from your choice of variable name; the last allocated ID
      number may have no relation to how many entries there actually are, for various
      reasons.

      When the last entry was entered: SELECT MAX(date_field) FROM table. Relies on
      there actually being a date field in there, of course.

      The last allocated ID: This is part of the table metadata in MySQL, which you
      can get from SHOW TABLE LIKE 'name'. A less reliable method could be SELECT
      MAX(id) FROM table, but the last allocated ID could (a) have been deleted or
      (b) be well below the most recent ID, if the auto_increment number had been
      reset for that table.
      [color=blue]
      >// Display database entries in reverse order (remove 'DESC' to have forward
      >display of results)
      >$sql = "SELECT gbID, gbDate, gbIP, gbURL, gbName, gbComment FROM
      >$table_to_look _for ORDER BY gbid DESC";
      >
      >$result = @mysql_query($s ql, $connection) or die("<B>Problem reading from
      >database: </B>".mysql_error ());
      >
      >while ($row = mysql_fetch_arr ay($result))
      >{
      >$gbID = $row['gbID'];
      >$gbDate = gmstrftime('%A %d %B %Y at $T',strtotime($ row['gbDate']));
      >$gbURL = $row['gbURL'];
      >$gbName = $row['gbName'];
      >$gbComment = nl2br($row['gbComment']);
      >
      >$display_ent ry .= "$gbID:<BR><B>P osted on: </B>$gbDate &nbsp;<B>IP:
      ></B>Logged<BR><B> Posted by: </B>$gbName<BR><B >Personal website:
      ></B>$gbURL<BR><B> Comment: </B>$gbComment<BR ><BR>";
      >}
      >
      >$sql = "SELECT last_insert_id( $gbID) FROM $table_to_look_ for";[/color]

      This will not work. LAST_INSERT_ID returns the ID value that was generated in
      the previous INSERT statement involving an AUTO_INCREMENT column, for this
      connection only.
      [color=blue]
      >$How_many_entr ies = @mysql_query($s ql, $connection);
      >
      >// $How_many_entri es = $How_many_entri es / 5;
      >echo "Entries: $How_many_entri es<BR>";[/color]

      You have to fetch from the resource returned by mysql_query, as you did above.
      You can't use it directly, it's just a resource handle.

      --
      Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
      Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

      Comment

      • Dariusz

        #4
        Re: Help req. Reading data from MySQL db with PHP

        In article <bl7nkp$7uk$1@t yfon.itea.ntnu. no>, "Jørn-Inge Kristiansen" <jorninge@stud. ntnu.no> wrote:[color=blue]
        >why not just use the mysql_num_rows( $query) function that php offers[/color]

        Perfect, sorted the problem out. Thanks.

        Dariusz

        Comment

        • Dariusz

          #5
          Re: Help req. Reading data from MySQL db with PHP

          In article <9mqenv8sigt592 cu0iji07gn01ksj uldi5@4ax.com>, Andy Hassall <andy@andyh.co. uk> wrote:[color=blue]
          >On Sun, 28 Sep 2003 21:32:51 GMT, ng@lycaus.plusY OURSHIT.com (Dariusz) wrote:
          >[color=green]
          >>I have PHP code (below) which reads data from a MySQL format database. The
          >>problem I am having is trying to find out when the last ID entry was made.[/color]
          >
          > From the 'when' above, it sounds like you want date and time?[/color]

          I apologise for the slight wording mistake - wrote the posting late at
          night... I was after the last inserted ID number.
          [color=blue][color=green]
          >>But instead of displaying the result of the query, it displays the ID
          >>number ($How_many_entr ies) as "Resource id #4", and looking around the
          >>search engines, no page makes particular sense as to what I've done wrong.[/color]
          >
          > That would mean you're using a result set resource directly in a print
          >statement, rather than fetching rows from it.[/color]

          Thanks for this information, cause I looked all over and could not find a
          proper explaination of what the error meant.

          Thanks for your help, very useful.

          Dariusz

          Comment

          • Jørn-Inge Kristiansen

            #6
            Re: Help req. Reading data from MySQL db with PHP

            well, the mysql_num_rows function doesn't find the last inserted id, but how
            many posts there is, if you have deleted a post or something like that, the
            last inserted id, and the number of posts in the table will not be the same.

            or to define it more, it's how many rows/posts you get out of the query
            you're doing. (wich in this case is all the posts in the table)




            "Dariusz" <ng@lycaus.plus YOURSHIT.com> wrote in message
            news:%ATdb.369$ kA.147032@wards .force9.net...[color=blue]
            > In article <bl7nkp$7uk$1@t yfon.itea.ntnu. no>, "Jørn-Inge Kristiansen"[/color]
            <jorninge@stud. ntnu.no> wrote:[color=blue][color=green]
            > >why not just use the mysql_num_rows( $query) function that php offers[/color]
            >
            > Perfect, sorted the problem out. Thanks.
            >
            > Dariusz[/color]


            Comment

            Working...