473,981 Members | 3,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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....
Jul 17 '05 #1
5 6630
why not just use the mysql_num_rows( $query) function that php offers
"Dariusz" <ng@lycaus.plus YOURSHIT.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_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....

Jul 17 '05 #2
On Sun, 28 Sep 2003 21:32:51 GMT, ng@lycaus.plusY OURSHIT.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_entr ies) - 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_entr ies) 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_entr ies 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($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";
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_entr ies = @mysql_query($s ql, $connection);

// $How_many_entri es = $How_many_entri es / 5;
echo "Entries: $How_many_entri es<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**********@t yfon.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.plusY OURSHIT.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_entr ies) 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.plus YOURSHIT.com> wrote in message
news:%A******** *********@wards .force9.net...
In article <bl**********@t yfon.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
7110
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 disadvantages of using a MySQL blob field rather than reading the images directly from the file? How does one insert an image into a blob field? Can it be done dynamically? Thank you John
2
3033
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 when reading out the data into the PHP variables / array. It should be displaying the information out in the following way, using the PHP array: n_date, n_headline, n_summarytext, n_fulltext
2
2496
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 by the PHP gd2 Library. The problem is that im trying to translate the 'fread' structure into a MySQL blob reading, as follows:
3
3373
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 about sql database and sql server, specially this article: http://www.aspfaq.com/show.asp?id=2195 will someone help me understand: 1. with *SQL Server*, do i keep my current Access 2000 database and ASP pages?
0
1758
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 set up a cron to run mysqlcheck every hour to try to do some damage control. The biggest problem is that once the table is corrupted, it seems to be locked. Well, no clients can read from it. Once repaired, just one record is usually lost for...
0
1361
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 pay my rent but I'm having problems with some of the coding requirements to make changes, add features, fix problems, etc. The site is http://nationwidesingles.us I have gotten some assistance and it has been very valuable to have someone...
8
5498
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 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
0
5594
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
10
2981
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 display a small photo and other information to scroll in a box on the web page. The marquee's info is a table with data that I now insert manually. I would like to be able to insert the data into it from a mysql database. The marquee works by...
0
11843
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11613
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10096
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8477
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7633
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6430
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5177
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3782
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.