This is a heavily commented snippet that "connects to database and selects information and displays it" , this is for someone wanting to connect to and use a mysql database.
By : philip
<?php
// Set the various database connect information. Host is usually
// localhost. If no password, just leave blank. This information is
// given by your web host OR if your own server, you setup yourself.
// See php manual for setup instructions.
$db_host = '';
$db_user = '';
$db_pass = '';
$db_name = '';
// More then one table can be used but in ths example we use just
// one. :-) And in many scripts just one is used. And it's easiest
// to comment on.
$db_table = '';
// ---- END CONFIGURATIONS ------------------------------------------//
// Make a connect to mysql. Through this connect many databases
// and tables can be selected. $conn will be what we call this
// connection.
$conn = mysql_connect($db_host,$db_user,$db_pass);
if ($conn == true) {
// Select the database.
mysql_select_db($db_name,$conn);
// We are selecting all fields (*) from database and using the $conn
// database connection. By default $conn would be used but we're
// learning so may as well not be lazy :-) Usually you wouldn't
// select * but for the lazy and unsure, it works nicely.
//
// This is a very powerful function, one can query *any* SQL statement
// and do many things, like update/delete/insert/select data. A good
// basic SQL tutorial is : sqlcourse.com . The result of this
// query is named $result
$result = mysql_query("SELECT * from $db_table",$conn);
// While a row exists to equal the fetched object (row) of data
// continue to loop until end of fetch (at the end)
// The way data is called may be new. $row->dbfieldname . See
// the manual on mysql_fetch_object() for more info. Other functions
// can just as easily have been used, like mysql_fetch_array. That method
// one would use format as : $row['id']; , $row['name'] , etc. See manual
// for all the possible (and beautiful) tools that are available.
while($row = mysql_fetch_object($result)) {
// These will loop through entire database. $tmp keeps getting
// appended to so it's getting very large. This is what's printed
// and where one makes look pretty :-) Below the database fields
// id, name and url are being printed from the $db_table table. And,
// the .= means it appends (concatenates) to $tmp so $tmp grows larger
// and later. See : http://www.zend.com/zend/tut/using-strings.php
// Again, id,name and url are just examples ... replace according to
// your database.
$tmp .= "ID : $row->id <br>\n";
$tmp .= "Name : $row->name <br>\n";
$tmp .= "Url : $row->url <br><hr>\n";
// The while loop ends here at this brace so the above cycles through until
// all data is gathered.
}
// Else we are not connected ($conn == false) so print error.
} else {
echo 'could not connect to database : '. mysql_error();
}
// Print the pulled data.
print $tmp;
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Database Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools. FREE! Go There Now!
|
|
|
|
Effective governance for lean development isn’t about command and control. Instead, the focus is on enabling the right behaviors and practices through collaborative and supportive techniques. Hear from Scott Ambler on how it is far more effective to motivate people to do the right thing than it is to force them to do so. Learn how to form a lightweight, collaboration-based framework that reflects the realities of modern IT organizations. FREE! Go There Now!
|
|
|
|
Poor Requirements Management capabilities in an Enterprise have been linked to excessive project failures, escalating IT costs, and failure to deliver competitive advantage into the marketplace. Join Brianna M Smith from IBM Rational and learn about how successful organizations align IT and Business stakeholders through collaborative processes and tools for effective requirements management, and how an integrated approach across the IT lifecycle can provide unparalleled visibility and traceability to ensure that project teams are delivering on the business vision by "doing the right things" and "doing things right." FREE! Go There Now!
|
|
|
|
Download the IBM WebSphere Portal V6.1 beta code and learn more about the rich features and enhancements in IBM WebSphere Portal V6.1. WebSphere Portal provides a composite application or business mashup framework and the advanced tooling needed to build flexible, SOA-based solutions, and scalability to meet the needs of any size organization. FREE! Go There Now!
|
|
|
|
In this tutorial, you can learn how to install and configure the IBM Rational Asset Manager Eclipse client, explore the different views in the Asset Management perspective, learn various search techniques, work with existing assets, and submit a new asset. FREE! Go There Now!
|
|
|
|
Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Method Composer V7.2 which helps you deliver customized yet consistent process guidance to your project teams and IT organization, and includes the latest version of IBM Rational Unified Process (RUP), which has provided process guidance to teams since 1996. FREE! Go There Now!
|
|
|
|
As businesses grow increasingly dependent upon Web applications, these complex entities grow more difficult to secure. Most companies equip their Web sites with firewalls, Secure Sockets Layer (SSL), and network and host security, but the majority of attacks are on applications themselves – and these technologies cannot prevent them. This paper explains what you can do to help protect your organization, and it discusses an approach for improving your organization’s Web application security. FREE! Go There Now!
|
|
|
|
Join the IBM Watchfire team for an informative discussion on techniques and best practices to proactively manage Web application security and how to effectively build application security testing into the software development lifecycle (SDLC). In this Software Delivery Platform webcast you will learn: How to better understand potential web application security vulnerabilities, best practices and how to effectively integrate application security testing into the software development lifecycle, the importance of detecting and removing software vulnerabilities during application development. FREE! Go There Now!
|
|
|
|
In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |