TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-05-2008, 07:21 PM   #1 (permalink)
The Wanderer
 
Join Date: Jun 2008
Posts: 8
Thanks: 2
gptArun is on a distinguished road
Default how to call mysql procedures in php???

Hi there ,
did you know, how to call mysql procedures in php?? searched on google then find PDO class library - but don't know how to use.
I m new to PHP, please reply ASAP.
gptArun is offline  
Reply With Quote
Old 06-05-2008, 07:33 PM   #2 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

PHP: MySQL - Manual

essentially if you just want to get back an array of items from the database

PHP Code:
$aInfo = array();
//aInfo is now an associative array with all of 'db' info
$aInfo mysql_fetch_assoc(mysql_query("SELECT * FROM `db`")); 
Personally, I hate having to write functions over and over again so I just use this wrapper I made a fair long while ago - essentially you use it like so

PHP Code:

$aDbInfo 
= array('localhost','root','password','db');
$objDb = new mysql_wrap($aDbInfo);

$objInfo $objDb->query("SELECT `username` WHERE `email`='%s'",$email);
echo 
$objInfo->fields['username'
[/php]

Though you might want to look up mysqli - or have someone speak of it, I'm not very learned with it.


The aforementioned class.
PHP Code:
<?php



define
("NO_FIELDS",'--accept-none--');



class 
mysql_wrap{

    private 
$connectionID;

    private 
$queryID;

    private 
$fieldCount;

        

        public function 
__construct($host,$user='',$pass='',$table=''){

            if(
is_array($host)){

                
$host $host['host'];

                
$user $host['user'];

                
$pass $host['pass'];

                
$table $host['table'];

            }

            

            
$this->connectionID mysql_connect($host,$user,$pass) or user_error("Connection to MySQL server could not be made");

            
mysql_select_db($table);

        }

        

        public function 
reqs(){

            

        }

        

        public function 
query(){

            
$args func_get_args();

            
$argCount func_num_args();

            if(
$argCount == 1){

                
$sql $args[0];

                
$this->queryID mysql_query($sql);

                
$this->fields = @mysql_fetch_assoc($this->queryID);

                
$this->sqlFieldCount = @mysql_num_rows($this->queryID);

                return;

            }elseif(
$argCount == 2){

                if(
$args[1] == '--accept-none--'){

                    
$this->queryID = @mysql_query($sql);

                    return;

                }

            }

            if(
is_array($args[0])){

                
$args $args[0];

                
$argCount count($args);

            }

            

            if(
$args[$argCount-1] == '--accept-none--'){

                
$input array_splice($args,0,$argCount-1);

                
$sql array_splice($input,0,1);

                
//print_r($input);

                
$input call_user_func_array('mysql_real_escape_string',$input);

                
$sql = (is_array($sql)) ? $sql : array($sql);

                
$input = (is_array($input)) ? $input : array($input);

                
$this->queryID mysql_query(call_user_func_array('sprintf',array_merge($sql,$input)));

                return new 
Record($this->queryID,$this->connectionID);

                

            }

            

            
$sql array_splice($args,0,1);

            
$arguments array_map('mysql_real_escape_string',$args);

            
$sql = (is_array($sql)) ? $sql : array($sql);

            
$arguments = (is_array($arguments)) ? $arguments : array($arguments);

            
$this->queryID mysql_query(call_user_func_array('sprintf',array_merge($sql,$arguments)));

            return new 
Record($this->queryID,$this->connectionID);

            

        }

        

        public function 
count(){

            return 
mysql_num_rows($this->queryID);

        }

        

}



class 
Record{

    private 
$count;

    private 
$curCount;

    private 
$queryID;

    private 
$connectionID;

    public 
$fields;

    public 
$EOF;

    

        public function 
__construct($queryID,$connectionID){

            
$this->queryID $queryID;

            
$this->connectionID;

            
$this->count = @mysql_num_rows($count);

            
$this->fields = @mysql_fetch_assoc($queryID);

        }

        

        public function 
movenext($len=0){

            
$this->fields = @mysql_fetch_assoc($this->queryID);

            ++
$this->curCount;

            

            if(
$len and $this->curCount >= $len){

                
$this->EOF true;

            }

            

            if(
$this->curCount >= $this->count){

                
$this->EOF true;

            }

        }

}
Enfernikus is offline  
Reply With Quote
The Following User Says Thank You to Enfernikus For This Useful Post:
gptArun (06-05-2008)
Old 06-05-2008, 07:50 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Hmmm. Did you not mean stored procedures?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-05-2008, 07:53 PM   #4 (permalink)
The Wanderer
 
Join Date: Jun 2008
Posts: 8
Thanks: 2
gptArun is on a distinguished road
Default Yes i mean Stored Procedures in mysql

Yes i mean Stored Procedures in mysql
gptArun is offline  
Reply With Quote
Old 06-05-2008, 07:57 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Isn't it something like this? I forget now. It's a while since I used stored procedures.

php Code:
$pResult = mysql_query("CALL('stored_procedure_name')");
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-05-2008, 08:05 PM   #6 (permalink)
The Wanderer
 
Join Date: Jun 2008
Posts: 8
Thanks: 2
gptArun is on a distinguished road
Default yes saw in many examples but not working

i even tried mysqli functions but not working. then i found PDO class but PDO Connection class not working.. don't know where is the problem.
Have a look to the attached zip file, including PDO connection class but not working.
Attached Files
File Type: rar PDO.rar (2.5 KB, 14 views)
gptArun is offline  
Reply With Quote
Old 06-05-2008, 08:08 PM   #7 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Using PDO you can work along the lines of:
PHP Code:
$statement $pdo->prepare('CALL sp_myprocedure(?)');
$statement->execute();
$rows $statement->fetchAll(PDO::FETCH_ASSOC); 
Salathe is offline  
Reply With Quote
Old 06-05-2008, 08:13 PM   #8 (permalink)
The Wanderer
 
Join Date: Jun 2008
Posts: 8
Thanks: 2
gptArun is on a distinguished road
Default in PDO Connection class, functions not shows any result.

Yes this simple code is working for me, but in PDO Connection class, functions not shows any result.

try {
$dbh = new PDO("mysql:host=localhost;dbname=example", "root", "root");
/*** echo a message saying we have connected ***/
echo 'Connected to database<br />';

/*** The SQL SELECT statement ***/
$sql = "CALL myprocedure(parameters)";

/*** fetch into an PDOStatement object ***/
$stmt = $dbh->query($sql);

/*** echo number of columns ***/
$result = $stmt->fetch(PDO::FETCH_NUM);

/*** loop over the object directly ***/
foreach($result as $key=>$val)
{
echo $key.' - '.$val.'<br />';
}

/*** close the database connection ***/
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}

Last edited by gptArun : 06-05-2008 at 08:15 PM. Reason: in $sql = "query or calling procedure" works
gptArun is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:54 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design