PHP & MySQL

PHP & MySQL, PHP Tutorial, MySQL Tutorial, PHP and MySQL - From Beginning to Professional, Free PHP and MySQL books

Index - MySQL basic

1. Access MySQL
2. Creating MySQL database
3. Creating MySQL table
4. Insert Query in MySQL
5. Selecting query in MySQL
6. Using mysql_fetch_array()
7. Using DATE_FORMAT in MySQL
8. Using UPDATE in MySQL

Change existing rows in MySQL using UPDATE

How to Change existing rows in MySQL using UPDATE with the help of PHP??

 <?

mysql_connect(‘localhost’);

mysql_select_db(‘foo’);

$result = mysql_query(

"update users set email = ‘babycarl@lerdorf.com’

where id = ‘carl’");

if($result) {

echo mysql_affected_rows();

} else {

echo mysql_error();

}

?>

Using DATE_FORMAT in MySQL

What is DATE_FORMAT ?? How to use DATE_FORMAT in MySQL using PHP???

 <?

mysql_connect(‘localhost’);

mysql_select_db(‘computereducationworld’);

$result = mysql_query(

"select id, email,

date_format(ts,’W M D, Y %r’) as d

from users order by ts");

if($result) {

while($row = mysql_fetch_assoc($result)) {

echo "$row[id] - $row[email] - $row[d]

\n";

}

} else {

echo mysql_error();

}

?>

Select data using mysql_fetch_array()

how to Select data from database table using mysql_fetch_array() with PHP ?? answer goes here:

<?

$result = mysql_query("select * from users order by id");

if(!$result) echo mysql_error();

else {

while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {

echo "$row[id] - $row[Password] - $row[Name] -

$row[email] - $row[ts]

\n";

}

}

?>

Selecting query in MySQL

Selecting query in MySQL using PHP

<?

mysql_connect(‘localhost’);

mysql_select_db(‘foo’);

$result = mysql_query("select * from users");

if(!$result) echo mysql_error();

else {

while($row = mysql_fetch_row($result)) {

echo "$row[0] - $row[1] - $row[2] - $row[3] - $row[4]

\n";

}

}

?>  

Insert Query in MySQL

Inserting query in MySQL

<?php

function add_user($id, $pass, $name, $email) {

$result=mysql_query("insert into users values

(’$id’,ENCRYPT(’$pass’),’$name’,’$email’,NULL)");

if($result) {

echo "Row inserted

";

} else {

echo mysql_error()."

";

}

}

 

?>  

Create table using MySQL

how to create a table using mySQL in PHP??  Answer goes here:

 <?

mysql_select_db(’foo’);

$result = mysql_query(”CREATE TABLE users (

id varchar(16) binary NOT NULL default ”,

Password varchar(16) NOT NULL default ”,

Name varchar(64) default NULL,

email varchar(64) default NULL,

ts timestamp(14) NOT NULL,

PRIMARY KEY (id)

)”);

if($result) {

echo “Table created”;

} else {

echo mysql_error();

}

?>

Creating a MySQL database

Creating a MySQL database using PHP:

<?

mysql_connect(‘localhost’);

if(mysql_query("CREATE DATABASE foo")) {

echo "Database foo created";

} else {

echo mysql_error();

}

?>  

How to access MySQL with PHP

Accessing MySQL with PHP

 <?

mysql_connect(’db.domain.com:33306′,’rasmus’,‘foobar’);

mysql_connect(’localhost:/tmp/mysql.sock’);

mysql_connect(’localhost’,’rasmus’,‘foobar’,

true,MYSQL_CLIENT_SSL|MYSQL_CLIENT_COMPRESS);

?>

File-Inclusion Statements, include(), require() Statement

File-Inclusion Statements
 
You can insert the content of a file into a PHP file before the server executes it, with the include() or require() function.
These two functions are used to create functions, headers, footers, or elements that can be reused on multiple pages.
 
include() Statement
 
The include() function takes all the text in a specified file and copies [...]

Looping Statements, while, do…while, for, foreach, continue, break and goto Statements

Looping Statements
Loops have as purpose to repeat a statement a certain number of times or while a condition is fulfilled.
 
The while Statement
 
The while statement specifies a condition that must be met before execution of its embedded code is terminated. Its syntax is the following:
 
                                    while (expression) {
                                                                statements
                                                          }
 
In the following example, $count is initialized [...]

Control Structures, Conditional, if, else, elseif, switch Statement

Control Structures
Control structures determine the flow of code within an application, defining execution characteristics such as whether and how many times a particular code statement will execute, as well as when a code block will relinquish execution control.
 
Conditional Statements
Conditional statements make it possible for your computer program to respond accordingly to a wide variety of [...]

String Interpolation, Double Quotes, Single Quotes, Heredoc

String Interpolation
To offer developers the maximum flexibility when working with string values, PHP offers a means for both literal and figurative interpretation. For example, consider the following string:
 
                      The $boy jumped over the wall.\n
 
You might assume that $boy  is a variable and that \n is a newline character, and therefore both should be interpreted accordingly.
 
Double [...]

Chapter 3..PHP Basics (Part-5)

String Interpolation
To offer developers the maximum flexibility when working with string values, PHP offers a means for both literal and figurative interpretation. For example, consider the following string:
The $animal jumped over the wall.\n
You might assume that $animal is a variable and that \n is a newline character, and therefore both should be interpreted accordingly. However, [...]

Chapter 3..PHP Basics (Part-4)

Constants
A constant is a value that cannot be modified throughout the execution of a program. Constants are particularly useful when working with values that definitely will not require modification, such as pi (3.141592) or the number of feet in a mile (5,280). Once a constant has been defined, it cannot be changed (or redefined) at [...]

Chapter 3..PHP Basics (Part-3)

Variables
Although variables have been used in numerous examples in this chapter, the concept has yet to be formally introduced. This section does so, starting with a definition. Simply put, a variable is a symbol that can store different values at different times. For example, suppose you create a Web-based calculator capable of performing mathematical tasks. [...]