Free Newsletters:
DatabaseJournal  
DBANews
Database Journal
Search Database Journal:
 
MS SQL Oracle DB2 Access MySQL PostgreSQL PHP SQL Etc SQL Scripts & Samples Links Database Forum DBA Videos
internet.com

» Database Journal Home
» Database News
» DBA Videos
» Database Articles
» Database Tutorials
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» DBA Jobs
» Sitemap

News Via RSS Feed



follow us on Twitter

Marketplace Partners
Be a Marketplace Partner




internet.commerce
Be a Commerce Partner


















2009: The Year Microsoft 'Gets' Users?

Apple's Jobs: Condition Won't Hinder CEO Duties

LG, Netflix Plan TVs With Streaming Net Video

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Database Journal | DBA Support | SQLCourse | SQLCourse2 | Swynk







Senior Infrastructure Engineer (PA)
Next Step Systems
US-PA-Philadelphia

Justtechjobs.com Post A Job | Post A Resume

Featured Database Articles

MySQL

September 24, 2002

Using a MySQL database with PHP

By Ian Gilfillan

Returning information about results

So far you have no way of knowing how many records were affected or returned (the information that MySQL gives you at the bottom of the query). There are two functions that provide this - one for queries that affected data but did not return any (INSERT's etc), and the other for queries that returned data (SELECT's etc). The mysql_affected_rows() function returns the number of rows affected by the last query.

int mysql_affected_rows ( [resource link_identifier])

This can be useful to see if you actually did perform an update, for example. Let's modify our script as follows:

<?php
$username = "pee_wee";
$password = "let_me_in";
$hostname = "localhost";	
$dbh = mysql_connect($hostname, $username, $password) 
	or die("Unable to connect to MySQL");
$selected = mysql_select_db("first_test",$dbh) 
	or die("Could not select first_test");
if (mysql_query("UPDATE people SET surname='Arendse' WHERE id=6")) {
  print "Successfully updated data";
}
else {
	  print "Failed to update data";
}
mysql_close($dbh);
?>

If you run the script, it will seem to succeed. But no data will have been changed, because there are no records with an id of 6. So the UPDATE succeeded, but affected no records. Actually Hazel Burger has married and changed her name to Hazel Arendse, and we wanted to reflect this. We can get a more meaningful result, and have picked up our problem, if we'd used mysql_affected_rows(). Change the script as follows:

<?php
$username = "pee_wee";
$password = "let_me_in";
$hostname = "localhost";	
$dbh = mysql_connect($hostname, $username, $password) 
	or die("Unable to connect to MySQL");
$selected = mysql_select_db("first_test",$dbh) 
	or die("Could not select first_test");
if (mysql_query("UPDATE people SET surname='Arendse' WHERE id=6")) {
  $rows_affected = mysql_affected_rows($dbh);
  print "Successfully updated $rows_affected rows";
}
else {
	  print "Failed to update data";
}
mysql_close($dbh);
?>

Note that mysql_affected_rows takes the database handle as the argument, not the result from the query. It will only return data for the most recent query. If you ran other UPDATE's and DELETE's, you would lose the affected rows data unless you caught it before any other queries.

You can return similar information about data returned with the mysql_num_rows() function. It returns the number of rows returned from the last query. Unlike mysql_affected_rows(), it takes the result resource as an argument, not the database handle. Note that SELECT queries are not the only kind of query that returns data. SHOW, DESCRIBE and EXPLAIN do as well. Examine the following changes to the script:

<?php
$username = "pee_wee";
$password = "let_me_in";
$hostname = "localhost";	
$dbh = mysql_connect($hostname, $username, $password) 
	or die("Unable to connect to MySQL");
$selected = mysql_select_db("first_test",$dbh) 
	or die("Could not select first_test");
$result = mysql_query("SHOW DATABASES");
while ($row = mysql_fetch_array($result,MYSQL_NUM)) {
  print "Database:".$row[0]."<br>";
}
mysql_close($dbh);
?>

Hopefully this article has got you started and shown you that the basics of running a database-driven site are not as difficult as you may have thought. Many dynamic sites are not much more complex than this. Of course there's much more than this, and future articles will explore the topic more thoroughly. In the mean time, you can investgate some of the other resources on this topic.

Go to page: Prev  1  2  3  

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

MySQL Archives







Latest Forum Threads
MySQL Forum
Topic By Replies Updated
Need helping in writing query sizzlingbrains 1 January 2nd, 01:22 PM
Database Query Problem?? anix 0 December 30th, 03:48 PM
Changing Date Format in mysql ashokvissu 1 December 27th, 10:19 AM
Database Overwriting Another spare 0 December 9th, 09:45 AM








internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers