Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

PHP Tutorial : PHP & MySQL

By Exforsys | on January 16, 2007 |
PHP Tutorial

PHP Tutorial : PHP & MySQL

In this PHP Tutorial you will learn about PHP and MySQL – Connecting to MySQL, Closing a connection, Selecting a database, Executing a query, Inserting data and Retrieving data.

Connecting to MySQL:

To connect to MySQL database server, use the function mysql_connect().

mysql_connect() takes three string arguments, the hostname, the username, and the password.

mysql_connect() returns an integer represents the connection index if the connection is successful, or returns false if the connection fails.

Example:

<?php
$con = mysql_connect(“localhost”, “john”, “smith”);
echo $con;
?>

Closing a connection:

To close a connection to MySQL, use the function mysql_close().
mysql_close() takes one argument, which is the connection index.

Example:

<?php
$con = mysql_connect(“localhost”, “john”, “smith”);
echo $con;
mysql_close($con);
?>

Selecting a database:

Now you are connected to the MySQL server, to start using queries, you have first to select a database, because MySQL server can contain many databases.

To select a database, use the function mysql_select_db().

mysql_select_db() takes two arguments, a string represents the database name, and the connection index.

Example:

<?php
$con = mysql_connect(“localhost”, “john”, “smith”);
mysql_select_db(“MyDB”, $con);
mysql_close($con);
?>

Executing a query:

To execute a SQL query, use the function mysql_query().
mysql_query() takes two arguments, a string represents the SQL statement, and the connection index.
mysql_query() returns a true or false.

Example:

<?php
$con = mysql_connect(“localhost”, “john”, “smith”);
mysql_select_db(“MyDB”, $con);
$query = “SELECT * FROM Products”;
$result = mysql_query($query, $con);
mysql_close($con);
?>

Inserting data:

Example:

<?php
$con = mysql_connect(“localhost”, “john”, “smith”);
mysql_select_db(“MyDB”, $con);
$query = “INSERT INTO Names VALUES(‘John’, ‘Smith’)”;
if(mysql_query($query, $con)) {
echo “Record added”;
}
else {
echo “Something went wrong”;
}
mysql_close($con);
?>

Retrieving data:

Data is retrieved using the SELECT statement, the statement returns data rows.
To get the number of the retrieved rows, use the function mysql_num_rows().
mysql_num_rows() takes one argument, which is the query result index.
mysql_num_rows() returns an integer, which is the number of retrieved rows.

Example:

<?php
$con = mysql_connect(“localhost”, “john”, “smith”);
mysql_select_db(“MyDB”, $con);
$query = “SELECT * FROM Products”;
$result = mysql_query($query, $con);
$num = $mysql_num_rows($result);
echo “The number of rows = $num”;
mysql_close($con);
?>

You can use the number of rows to make a loop and display all the result rows.
But a better solution is to make a while loop using the function mysql_fetch_array().
mysql_fetch_array() takes one argument, which is the result index.
mysql_fetch_array() returns an array of the next fetched row, or false if no more rows.

Example:

<?php
$con = mysql_connect(“localhost”, “john”, “smith”);
mysql_select_db(“MyDB”, $con);
$query = “SELECT first_name, last_name FROM Products”;
$result = mysql_query($query, $con);
while($dataArray = mysql_fetch_array($result) {
$firstName = $dataArray[‘first_name’];
$lastName = $dataArray[‘last_name’];
echo “$firstName $lastName<br />\n”;
}
mysql_close($con);
?>

« « How To Build Customer Loyalty With CRM
Online Student Loan Consolidation » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Free Training

RSSSubscribe 403 Followers
  • Popular
  • Recent
  • How to Use Cookies in PHP

    February 18, 2009 - 0 Comment
  • PHP Tutorials – Variables

    September 22, 2006 - 0 Comment
  • PHP Tutorial – Introduction

    April 29, 2006 - 0 Comment
  • PHP Tutorials – Operators (Part I)

    September 18, 2006 - 0 Comment
  • PHP Tutorial – Installation

    May 17, 2006 - 0 Comment
  • PHP Tutorials – Operators

    September 22, 2006 - 0 Comment
  • PHP Tutorial – Syntax

    May 17, 2006 - 0 Comment
  • PHP Tutorials – Conditional Statements

    September 25, 2006 - 0 Comment
  • PHP Tutorials – Data Types

    September 18, 2006 - 0 Comment
  • PHP Tutorials – Loops

    September 30, 2006 - 0 Comment
  • PHP Strings

    March 18, 2009 - 0 Comment
  • How to Use Cookies in PHP

    February 18, 2009 - 0 Comment
  • PHP and MySQL User Registration

    October 31, 2008 - 0 Comment
  • WAMP Server

    October 26, 2008 - 0 Comment
  • PHP Tutorials – Forms

    November 22, 2006 - 0 Comment
  • PHP Tutorials – File manipulation (Part-2)

    November 22, 2006 - 0 Comment
  • PHP Tutorials – File manipulation (Part 1)

    November 15, 2006 - 0 Comment
  • PHP Tutorials – Arrays (Part 2)

    November 15, 2006 - 0 Comment
  • PHP Tutorials : Arrays

    November 5, 2006 - 0 Comment
  • PHp Tutorials : Regular expressions

    November 2, 2006 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • PHP Strings
  • How to Use Cookies in PHP
  • PHP and MySQL User Registration
  • WAMP Server
  • PHP Tutorials – Forms

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2022. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.Accept Reject Read More
Privacy & Cookies Policy
Necessary Always Enabled