Saturday 7 May 2011

Php Mysql_fetch Functions Script Notes




Mysql_fetch_assoc: Function reads a record from result set and it returns as an associative array. Each element key is the name of the column.
Mysql_fetch_array: This function reads a record from result set and returns as numeric array and associative array elements.
Mysql_fetch_object: This function reads a record from result set and returns as an object. This object belongs to the std base class.
Php Mysql_fetch Functions Script:
<select>
<?php
mysql_connect("localhost","root","");
$dbs=mysql_list_dbs();
while ($db=mysql_fetch_row($dbs))
{
echo "<option> $db[0]</option>";
}
?>
</select>

Script 2

<select>
<?php
mysql_connect("localhost","root","");
$dbs=mysql_list_tables("information_schema");
while ($db=mysql_fetch_row($dbs))
{
echo "<option> $db[0]</option>";
}
?>
</select>

Mysql_fectch_row and Mysql_table_name and Mysql_list_tables and Mysql_list_dbs php script




Mysql_list_dbs:  By using this function we can get the list of database available in database server.

Mysql_list_tables: By using this function we can get the list of tables available in database.

Mysql_table_name: By using this function we can get the table name of a result set.

Mysql_fectch_row: By using this function we can read a record from result set and we can locate the result set pointer at the beginning of the next record. It returns the record values as a numeric array.

How to fetch the database table values?
How to display the database on browser?


Script: save this file in htdocs as script.php and open the browser type http://localhost/script.php

<table border="12">
<th style="background-color:yellow; color:red"> Username </th>
<th style="background-color:yellow; color:red"> Password </th>
<?php
mysql_connect("localhost","root","");
mysql_select_db("mynewdb");
$data=mysql_query("select * from tbl_user");
$count=1;
while ($rec=mysql_fetch_row($data))
{
if($count%2==0)
$color="blue";
else
$color="green";
$count++;
echo "<tr style='background-color:$color'>
<td> $rec[0]</td><td><$rec[1]</td></tr>";
}
?>
</table>



Friday 6 May 2011

Connecting to the database in php Mysql




Connecting to the database in php Mysql


PHP is providing a library to connect with the Mysql database. That is “extension=php_mqsql.dll”. This library is providing number of functions.

i). Mysql_connect: This function is used to establish a connection between php application and Mysql database. We need to provide the arguments like server name, username, and password.

How to connect to the Mysql database?

How to establish a connection between mysql database and php application?

How to create a database through php script?

<?php
f($con=mysql_connect("localhost","root",""))
echo "connected to The Mysql Server";
else
echo "not Connected to The MYsql Server";
if (mysql_query("create database mynewdb", $con))
echo "DataBase Created";
else
echo "DataBase not created";
?>

Now open the http://localhost/phpmyadmin/ and check whether the data base is created or not on the name “mynewdb”.


-*-*-

ii).Mysql_error: It returns an error massage, if any error accrued at the time of application executions.

 iii). Mysql_errno: It returns the error number, if any error accrued at the time of script executions.

How to know the error reports in php application?

How to know the error number in the php script?


<?php
$con=mysql_connect("locahost","root","");
mysql_select_db("mynewdb",$con);
$sqlstt="create table tbl_user(uname varchar(100),pwd varchar(100))";
if(mysql_query($sqlstt))
echo "Table is Created";
else
echo Mysql_error();
?>

If you execute this script you will get the below output.


Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'locahost' (11004) in C:\xampp\htdocs\dinnu.php on line 2

Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in C:\xampp\htdocs\dinnu.php on line 3

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\dinnu.php on line 5

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs\dinnu.php on line 5
Access denied for user 'ODBC'@'localhost' (using password: NO)


*Because you provided the wrong password and username. 


Whenever we are passing string value to the database it should be in a single quotation.


The below script is very very important. This script is how to store the username and password values in database .


How to store username and password in database:



<?php
if(isset ($_POST['sub']))
{
$Uname=$_POST['txtUname'];
$Pwd=$_POST['txtPwd'];
$con=mysql_connect("localhost","root","");
mysql_select_db("mynewdb",$con);
$sqlstt="insert into tbl_user values('$Uname','$Pwd')";
if(mysql_query($sqlstt))
echo "record is Inserted";
else
echo mysql_error();
}
?>
<form method="post" action="">
Username : <input type="text" name="txtUname">
<br>
Password : <input type="text" name="txtPwd">
<br>
<input type="submit" name="sub" value="Reg">
</form>
<script>
function funcheck()
{
Uname=document.getElementByid('txtUname').value
Pwd=document.getElementByid('txtPwd').value
if($Uname.lenght==0)
{
alert("Enter Username")
return false;
}
else
if ($Pwd.lenght==0)
{
alert("Enter Password")
return false;
}
}
</script>


PHP Innodb Database Engine And Memory Key




Innodb Database Engine:

This is the transaction database engine and it supports data integrity and relationships between the tables. If the database is crashed  you can easily get back the data. It I storing in a single file and we can easily mix the innodb tables. By default innodb state is disabled. To enable the innodb state open my.cnf file that is located in file:///C:/xampp/mysql/bin/.

To change the configuration settings of innodb directives, first we have to comment (#) skip_innodb statement. And remove the comments (#) of configuration directives.

Memory (key) Engine:

By using this table we can store the information in Ram. It is 30% faster that myisam. They are accessed and used in exactly the same as regular myisam tables. However, the data stores within them are available only for the life time of Mysql server and erased when the server is crashed.

Berkeley db Engine: The Bdb table supports commit, roll back options, crash recovery multiuser. But it contains some limitations. We can’t move easily, cannot compress and table size is larger than myisam. That is the reason why we are not using this table in applications. 

Thursday 5 May 2011

Types Of Storage Engine In Mysql and Myisam Advantages and Disadvantages


Types Of Storage Engine In Mysql
A database engine is underlying software that a database management system uses to create, retrieve, update and delete data from database.  MYSQL database is supporting different types of engines to execute the SQL statements. 
Myisam: It is a record storage engine. It is based on older isam code but it has many useful executions.
Each Mysql table stores on disk in three different files. The files have name that beginning with the table name and have an extension to indicate the file type.
i).frm => file stores the table format.
ii).myd => files stores the table data.
iii).ind=> file stores the table index.
To create the table using myisam engine the SQL statement is 
“ create table <tname> (col1, col2..) engine=myisam
To see the engine supported by MySQL server the statement is
“show engine”
To see the table names along with the engines the SQL statement is 
“show table status”
In MYSQL table the data values are stored with the low bytes. It makes the data machine and operating system independent.

The Myisam format supports large table files up to 4 GB. It is very fast to fetch the records from the basic frequently. Simple to design and create, and no worries about the foreign key relationships. It is good for reading intensive(select) table.


Disadvantages of Myisam:
This table doesn’t supports the foreign key relationships. That is the reason why we can’t use this table in enterprise applications. It is very slow at the time of updating and modifying the table records. If the system crashed we cannot get back the date.

PHP MY ADMIN Options




PHP MY ADMIN

This is an environment to work with the mysql database. To open the url address is http://localhost/phpmyadmin/ .

Options of PHP My Admin:

Browse: By using this option we can fetch the records from the table.

Structure: By using this option we can change the table structure and we can add new field and new column. We can also remove the columns edit the column etc..

Insert: To insert the records in a table.

SQL: This option is providing a text box to write the sql statements.

Ex: insert users

Empty:  By using this option we can delete all records of a table.

Drop: By using this option we can delete the entire table along with the structure.

Operations: By using this option we can change the table name, data base engine etc..

Export: By using this option we can export the database objects as an SQL file.

Import: To import the cri file into another data base.

Wednesday 4 May 2011

Introduction of Cpanel FTP and MYsql




Introduction of Cpanel FTP MYsql


@Webserver is a software to run the web applications. It is used to access the request of the client and sends the response to the client.

Every web server contains the root directory. We have to place our application in the web server root directory. In root directory a file with name index is the first executable file, otherwise we can maintain the startup files with name default Home etc. We can upload the files in web server in two ways. One is FTP and another one is Cpanel.

Cpanel:

By using the cpanel we can controlee the web server, like changing passwords, create mail accounts, upload the files, manage the database etc..

*htaccess: It is a configuration setting file, which is available in web server, used to change the configuration settings of the server. We can also create php.ini file to overwrite the default settings of web server.

FTP:
FTP is a state full protocol to upload the multiple files to the web server. We can upload the files through browsers or through third party tools like file zilla, cute ftp etc..

MYSQL:
 Mysql is a database language. Implemented by MySQL AB Corporation in 1995. It is an open source relational data base management system.

PHP is providing MySQL library with number of predefined functions to insert the records, fetch the records and create new objects in data base server.

We can interact with the mysql data base server in different environments using command prompts, using PHP My admin frontend programming language.

Mail Function In PHP




Mail Function In PHP

By using this function we can send the mails from php application to destination email id. The arguments are “to address, subject, body, and collection of headers”.

*To send mails from one location to another location we have to use the “SMTP SERVER”.

*The default port number of SMTP server is 25. The fourth argument, We can place the multiple headers.

*The fourth argument, we can place the multiple headers. The separated between one header and another header is “\r\n”. 

The mail related headers are nothing but cc, bcc, from etc.


<?php
if(mail("abc@gmail.com",hi","hello",From:DanielPHP"))
echo "mail has been sent";
else
echo "not";
?>

By using the above script we can’t send mails from our system. Because system doesn’t contains mail server. By default every web server contains mail server. So just try this script in any web server. 

Tuesday 3 May 2011

Complete string functions in php





str_repeat: This function is used to repeat a string in specific number of times.

str_repeat script:
<?php
$str="welcome to dan php site";
echo str_repeat($str,2);
?>
str_replace: To replace the occurrence of the search string with replacement string.

str_replace Script
<?php
$str="welcome to dan php site";
echo str_replace("php","Java",$str);
?>
str_split: Converts a string as array. Same as implode.

str_word_count: This function returns the information about the words used in a string.

*If the mode is 0 it returns the total number of words. If the mode is 1 it returns the all words as array elements.

Each element value is one woed and element keys are 0 1 2 3 etc.

If the mode is 2 , the element keys are the index number of the words.

str_word_count script

<?php
$str="welcome to dan php site";
print_r(str_word_count(str,2));
?>
strstr: This function is used to return the characters from a string from specified position. It is case sensitive.

strstr script
<?php
$str="welcome to dan php site";
echo strstr($str,'p');
?>
stristr: It is same as “strstr”, but it is case insensitive.

strchr : It will find out the last occurred characters from a string , based on the input characters. And it returns the remaining string from that location.

strcmp: It compares two string values. It calculates the acsii value.

*It returns 0, if the both strings are same. If first string is greater than second string it displays the positive value. Otherwise it displays the negative value.

strchr script

<?php
$str="welcome to dan php site";
$str1="Welcome to dan php site";
echo strcmp($str,$str1);
?>

strcasechp: It is same as strcmp, But it is case insensitive.

strip_tags: By using this function we can strip the HTML tags from a string.

strip_tags script
<?php
$str="hello <input type='button' value ='click'>";
echo strip_tags($str);
?>

strpos: By using this function we can get the position of a character in a string.

stripos: It is same as strops. But it is case insensitive.

stripos script
<?php
$str="hello";
echo strpos($str,'e');
?>

strtolower: It converts the all characters into lower case characters.

strtoupper: It converts the all characters to uppercase characters.

substr: It returns the sub string of a string.

substr script:

<?php
$str="Welcome To My Site";
echo substr($str,2,3);
?>

string functions in php has been completed today. Tomorrow we are going to discourse an interesting topic. C u tomorrow guys . b bye