Basic of MySQL

Basic of MySQL , Accessing MySQL, Basic MySQL command, SQL Scripts, Inserting Data, Querying Data,

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);

?>