PHP DEN

1 ODBC With PHP



ODBC With PHP

ODBC (open database connectivity) is an application programming interface(API) that allows you to connect to a data source like msaccess , oracle etc…
ODBC is vendor dependent that means for each db there will be a specific ODBC driver.
ODBC driver is a .dll(dynamic link library) file. If the php application want to work with oracle db, we need to create a DSN for oracle.
DSN stands for Data Source Name, is the reference of the db to any front end application.

Create an ODBC/DSN for Oracle :

Start -> settings -> control panal -> administrative tools -> data sources
Click on “add” button.
Select a driver for which you want to setup a datasource.
Select Microsoft ODBC for oracle or oracle for Ora ***[MSORCL32.dll/SQORA32.dll] click on finish.
Data source name :  oracledsn
Username : scott
Ok----ok

Create an ODBC/DSN for Oracle,ODBC With PHP,odbc, odbc drivers, php with odbc.
[Read More...]


0 Procedures In Mysql



Procedures In Mysql

when you pass an sql query using mysql_query() function each the qyery gets compiled and after that it gets executed even if the query is same. This reduces the performance your application.
To overcome this ,we can use a procedure or a function in mysql.
Procedure will not a return a value back to the caller (but, using out parameters we are able to send multiple values to caller) where as a function returns a single a value back to the caller.
Procedure as associated with mainly two type of parameters.
1.      IN –pass by value
2.      OUT- pass by reference.
Steps to create a procedure :
1.      Start mysql db server.
2.      Open mysql prompt
C:\xampp\mysql\bin>mysql –u root –p
3.      Change the delimiter
Mysql> delimiter//
4.      Select a DB
Mysql>use employee //
Mysql> create a procedure myproc(OUT param1 INT)
BEGIN
Select count(*) into param1 from emp;
End//
Mysql>delimiter;
To invoke a procedure
Mysql> call myproc(@p);
Mysql> select @p;
Php  program to invoke the above procedure?
<?php
 $con = mysql_connect(“localhost”,”root”,””);
Mysql_select_db(“employee”);
$res = mysql_query(“call myproc(@s)”);
If($res)
{
$rs=mysql_query(“select @s”);
//var_dump($rs);
$row = mysql_fetch_row($rs);
Echo “number of records=”.$row[0];
}
Else
Echo “procedure not found”;
Mysql_close($con);
?>
Inserting images in mysql:
Mysql has a BLOB(binary large objects) datatype which can be used to store binary data . blobs are typically images ,audio or other multimedia blob objects. Mysql has 4 blobs types.
a.       TINYBLOB
b.      BLOB
c.       MEDIUMBLOB
d.      LONGBLOB
All these types differ only in their sizes.
1.      Create a table named “test_image” in mysql “employee” database having 3 columns show below.
Mysql> create table test_image(id int(10) auto_increment primary key, name varchar(20)m image blob);
Program to insert an image:
<?php
 $con = mysql_connect(‘localhost’,’root’,’’);
Mysql_select_db(“employee”);
$data = file_get_contents(“images/back1.jpg”);
$data = addslashes($data);
$res = mysql_query(“insert into test_image(name,image) values(‘back1.jpg’,’$data’)”);
If($res)
Echo “record inserted”;
Else
Echo mysql_error();
Mysql_close();
?>
Displaying images stored in mysql?
<?php
$con = mysql_connect('localhost','root','');
mysql_select_db("emp");
$rs = mysql_query("select * from test_image where id=1");
if(mysql_num_rows($rs)>0)
{
    $row = mysql_fetch_row($rs);
    $n = file_put_contents('Photo0104.jpg', $row[2]);
    header("content_type:image/jpeg");
    echo $row[2];
}
else
    echo "no matching record found";
mysql_close($con);
?>
OutPut:
Image displayed.

procedures in mysql, stored procedures in mysql, mysql variables in stored procedures, stored procedures in mysql examples., 
[Read More...]


0 My Sql : Joins



Joins

TABLE1.CREATE TABLE EMP(EMPNO INT(10) PRIMARY KEY, ENAME VARCHAR(20), SAL FLOAT(10,2),DEPTNO INT(2));
TABLE2. CREATE TABLE DEPT(DEPTNO INT(2) PRIMARY KEY,DNAME VARCHAR(20), LOC VARCHAR(20));
TABLE1: INSERT INTO EMP(EMPNO,ENAME,SAL,DEPTNO) VALUES(101,’CHANDU’,123.45,10),(102,’KARANAM’,124.34,20),(103,’ASWINI’,145.23,10);
TABLE2:INSERT INTO DEPT(DEPTNO,DNAME,LOC) VALUES(10,’TECHNICAL’,’HYD’),(20,’ACCOUNTS’,’CHANNAI’);

SELECT E.EMPNO , E.ENAME,E.ESAL,E.DEPTNO,D.DNAME,D.LOC FROM EMP E , DEPT D WHERE E.DEPTNO = D.DEPTNO;

[Read More...]


0 My Sql Functions With Examples



My Sql Functions With Examples

Program to get the details of all the columns of “emp” table?
<?php
echo "<h1>";
$con = mysql_connect('localhost','root','');
mysql_select_db("emp");
$res = mysql_query("select * from emp1");
$i =0;
while($i<  mysql_num_fields($res))
{
    echo "<u> information for columns".($i+1).":</u><br>";
    $meta = mysql_fetch_field($res,$i);
    echo "<pre>
        Blob            :   $meta->blob
        Max_length      :   $meta->max_length
        Name            :   $meta->name
        Not-Null        :   $meta->not_null
        Numeric         :   $meta->numeric
        Primary-key     :   $meta->primary_key
        Table           :   $meta->table
        Type            :   $meta->type
        Default         :   $meta->def
        Unique-key      :   $meta->unique_key
        Unsigned        :   $meta->unsigned
        Zerofill        :   $meta->zerofill
            </pre>";
        $i++;       
}

mysql_close();

?>
OutPut:
information for columns1:

        Blob            :   0
        Max_length      :   3
        Name            :   empno
        Not-Null        :   1
        Numeric         :   1
        Primary-key     :   1
        Table           :   emp1
        Type            :   int
        Default         :  
        Unique-key      :   0
        Unsigned        :   0
        Zerofill        :   0
           
information for columns2:

        Blob            :   0
        Max_length      :   12
        Name            :   ename
        Not-Null        :   0
        Numeric         :   0
        Primary-key     :   0
        Table           :   emp1
        Type            :   string
        Default         :  
        Unique-key      :   0
        Unsigned        :   0
        Zerofill        :   0
           
information for columns3:

        Blob            :   0
        Max_length      :   7
        Name            :   sal
        Not-Null        :   0
        Numeric         :   1
        Primary-key     :   0
        Table           :   emp1
        Type            :   real
        Default         :  
        Unique-key      :   0
        Unsigned        :   0
        Zerofill        :   0
7.mysql_fetch_lengths(mywql result): get the length of each ouput in a result.
<?php
echo "<h1>";
$con = mysql_connect('localhost','root','');
mysql_select_db('emp');
$res = mysql_query("select * from emp1 where empno=101");
$record = mysql_fetch_assoc($res);
print_r($record);
echo "<br><br>";
$length = mysql_fetch_lengths($res);
print_r($length);

?>
OutPut:
Array ( [empno] => 101 [ename] => penna [sal] => 123.45 )

Array ( [0] => 3 [1] => 5 [2] => 6 )
8.mysql_field_len(mysql result , int $field_offset) : returns the length of the specified field.
9.mysql_field_name(mysql result , int $field_offset) : get the name of the specified field in a result.
10.mysql_field_type(mysql result,int $field_offset) : get the type of the specified field in a result.
<?php
echo "<h1>";
$con = mysql_connect('localhost','root','');
mysql_select_db('emp');
$rs = mysql_query("select * from emp1");
$i=0;
while($i<  mysql_num_fields($rs))
{
    echo mysql_field_name($rs, $i)."--".mysql_field_table($rs, $i)."--".mysql_field_len($rs, $i)."<br>";
    $i++;
}
?>
OutPut:
empno--emp1--10
ename--emp1--20
sal--emp1—10
1.      Mysql_free_result(mysql result) :  free mysql result memory.
2.      Mysql_get_client_info()
3.      Mysql_get_host_info()
4.      Mysql_get_proto_info()
5.      Mysql_get_server_info()
6.      Mysql_info()
7.      Mysql_num_fields(mysql result):  get number of fields in result resource.
8.      Mysql_num_rows(mysql result): get number of rows/records in result resource.
9.      Mysql_ping($conn) : ping a server connection or reconnect if there is no connection.

[Read More...]


 
Return to top of page Copyright © 2010 | PHP DEN Converted into Blogger Template by HackTutors