473,241 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

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_entries) - 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_entries) 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_entries 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_tables ("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($sql, $connection) or die("<B>Problem reading from
database: </B>".mysql_error());

while ($row = mysql_fetch_array($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>Posted 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_entries = @mysql_query($sql, $connection);

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

etc....
Jul 17 '05 #1
5 6578
why not just use the mysql_num_rows($query) function that php offers
"Dariusz" <ng@lycaus.plusYOURSHIT.com> wrote in message
news:xs****************@wards.force9.net...
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_entries) - 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_entries) 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_entries 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_tables ("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($sql, $connection) or die("<B>Problem reading from
database: </B>".mysql_error());

while ($row = mysql_fetch_array($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>Posted 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_entries = @mysql_query($sql, $connection);

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

etc....

Jul 17 '05 #2
On Sun, 28 Sep 2003 21:32:51 GMT, ng@lycaus.plusYOURSHIT.com (Dariusz) wrote:
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.
From the 'when' above, it sounds like you want date and time?
When the script is executed, the $gbID is supposed to be read and display
the last entered ID number ($How_many_entries) - the ID number is entered /
updated automatically in another PHP script which deals with data entry to
the database.
But now you say you only want the last allocated ID number?
But instead of displaying the result of the query, it displays the ID
number ($How_many_entries) as "Resource id #4", and looking around the
search engines, no page makes particular sense as to what I've done wrong.
That would mean you're using a result set resource directly in a print
statement, rather than fetching rows from it.
Does anyone know how to get the $gbID number from the database and into
$How_many_entries so I can perform some math work on it?
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.
// 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($sql, $connection) or die("<B>Problem reading from
database: </B>".mysql_error());

while ($row = mysql_fetch_array($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>Posted 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";
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.
$How_many_entries = @mysql_query($sql, $connection);

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


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 (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #3
In article <bl**********@tyfon.itea.ntnu.no>, "Jørn-Inge Kristiansen" <jo******@stud.ntnu.no> wrote:
why not just use the mysql_num_rows($query) function that php offers


Perfect, sorted the problem out. Thanks.

Dariusz
Jul 17 '05 #4
In article <9m********************************@4ax.com>, Andy Hassall <an**@andyh.co.uk> wrote:
On Sun, 28 Sep 2003 21:32:51 GMT, ng@lycaus.plusYOURSHIT.com (Dariusz) wrote:
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.


From the 'when' above, it sounds like you want date and time?


I apologise for the slight wording mistake - wrote the posting late at
night... I was after the last inserted ID number.
But instead of displaying the result of the query, it displays the ID
number ($How_many_entries) as "Resource id #4", and looking around the
search engines, no page makes particular sense as to what I've done wrong.


That would mean you're using a result set resource directly in a print
statement, rather than fetching rows from it.


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
Jul 17 '05 #5
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.plusYOURSHIT.com> wrote in message
news:%A*****************@wards.force9.net...
In article <bl**********@tyfon.itea.ntnu.no>, "Jørn-Inge Kristiansen"

<jo******@stud.ntnu.no> wrote:
why not just use the mysql_num_rows($query) function that php offers


Perfect, sorted the problem out. Thanks.

Dariusz

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: John | last post by:
I have over 5000 thumbnail pictures of size 5kb each. I would like to able to load all 5000 pictures and view 50 per page using mysql_data_seek(). I would like to know what are the advantages and...
2
by: Dariusz | last post by:
Below is part of a code I have for a database. While the database table is created correctly (if it doesn't exist), and data is input correctly into the database when executed, I have a problem...
2
by: RootShell | last post by:
Dear All Im having some dificulty here: I found a great PHP code by Catalin Mihaila that reads a SRC (Sinclair Spectrum $SCREEN Image Format) and tranforms it into PNG format to show onscreen...
3
by: cooldv | last post by:
i am running a website on Windows 2000 server with ASP 3 webpages and Access 2000 database. (with a hosting company) traffic is slow at this time but expect to grow. lately i have been reading...
0
by: Richard Gabriel | last post by:
Hi everyone, Since we upgraded to MySQL 4.0.13 from 3.23, we have been getting table corruption often. It happens about twice per week (with about 500 queries per second average). I have even...
0
by: Creativy, writing and more | last post by:
Hi all, I'm hoping to find some additional help with a site I'm maintaining for my landlord. I'm trying to grow a web hosting and web design business. I was to have this site as a job that would...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
10
by: CDFTim | last post by:
O.K. that was a long Title... Can you help / show me how I would......... I am going to long windedly try to paint this picture. Backround: I have an html page that has a marquee function in it to...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.