downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

mssql_close> <Fonctions Mssql
[edit] Last updated: Fri, 25 May 2012

view this page in

mssql_bind

(PHP 4 >= 4.0.7, PHP 5, PECL odbtp >= 1.1.1)

mssql_bindAjoute un paramètre à une procédure stockée MSSQL (locale ou distante)

Description

bool mssql_bind ( resource $stmt , string $param_name , mixed &$var , int $type [, bool $is_output = false [, bool $is_null = false [, int $maxlen = -1 ]]] )

Ajoute un paramètre à une procédure stockée MSSQL (locale ou distante).

Liste de paramètres

stmt

Une ressource de requête, obtenue avec la fonction mssql_init().

param_name

Le nom du paramètre, sous la forme d'une chaîne de caractères.

Note:

Vous devez ajouter un caractère @, comme dans la syntaxe T-SQL. Voir l'explication dans la documentation de la fonction mssql_execute().

var

La variable PHP que vous voulez lier au paramètre MSSQL. Il faut la passer par référence pour récupérer les valeurs OUTPUT et RETVAL après l'exécution de la procédure.

type

Une constante parmi : SQLTEXT, SQLVARCHAR, SQLCHAR, SQLINT1, SQLINT2, SQLINT4, SQLBIT, SQLFLT4, SQLFLT8, SQLFLTN.

is_output

Si la valeur est un paramètre OUTPUT ou non. Si c'est un paramètre OUTPUT et que vous ne mentionnez pas, il sera traité comme un paramètre d'entrée normal et aucune erreur ne sera émise.

is_null

Si le paramètre vaut NULL ou non. Le fait de passer NULL comme valeur au paramètre var n'y fera rien.

maxlen

Utilisé avec les valeurs de type CHAR et VARCHAR. Vous devez indiquer la longueur des données, donc, si ce paramètre vaut VARCHAR(50), le type doit être SQLVARCHAR et sa valeur, 50.

Valeurs de retour

Cette fonction retourne TRUE en cas de succès ou FALSE si une erreur survient.

Exemples

Exemple #1 Exemple avec mssql_bind()

<?php
// Connexion à MSSQL et sélection de la base de données
mssql_connect('KALLESPC\SQLEXPRESS''sa''phpfi');
mssql_select_db('php');

// Crée une nouvelle procédure stockée
$stmt mssql_init('NewUserRecord');

// Lie les noms de champs
mssql_bind($stmt'@username',  'Kalle',        SQLVARCHAR,     false,  false,  60);
mssql_bind($stmt'@name',      'Kalle',        SQLVARCHAR,     false,  false,  60);
mssql_bind($stmt'@age',       19,             SQLINT1,        false,  false,   3);

// Exécute 
mssql_execute($stmt);

// Libère les ressources
mssql_free_statement($stmt);
?>

Voir aussi



mssql_close> <Fonctions Mssql
[edit] Last updated: Fri, 25 May 2012
 
add a note add a note User Contributed Notes mssql_bind
asimonassi 17-Jun-2011 04:51
In order to bind DATETIME, i suggest to convert the date to double, then use SQLFLT8 instead of SQLVARCHAR.

In SQL Server Datetime is a standard double. The integer part represent the number of days since 1-1-1900 and the fractional part represent the fraction of the day (e.g: 0.5 means noon, 0.75 means 6 PM).

Using SQLVARCHAR may lead to errors depending on the local server config.

<?php
function PhpTimeToOLEDateTime($timestamp)
{
       
$a_date = getdate ($timestamp);
       
$year= $a_date['year']; //this year
       
$partial_days = ($year-1900)*365;//days elapsed since 1-1-1900
        //let's calculate how many 29 february from 1900 to first day on this year
       
$partial_days +=(int)(($year-1) / 4); //each 4 years a leap year since year 0
       
$partial_days -= (int)(($year-1) / 100); //each 100 years skip a leap
       
$partial_days += (int)(($year-1) / 400); //each 400 years add a leap
       
$partial_days -= 460; //459 leap years before 1900 + 1 for math (year 0 does not exist)
       
$partial_days += $a_date['yday'];

       
$seconds = $a_date['hours'] * 3600;
       
$seconds += $a_date['minutes'] * 60;
       
$seconds += $a_date['seconds'];

       
$d = (double) $partial_days;
       
$d +=  ((double)$seconds)/86400.0;

        return
$d;
}
?>

Sample binding

<?php
$now
= PhpTimeToOLEDateTime(time());
mssql_bind($proc, "@dateparam", $now, SQLFLT8, false);
?>
brad at foreverybody dot com 16-Jul-2010 08:39
if you are experiencing difficulties binding an empty string because it is being converted to NULL by your driver try the following:

<?php
if($var === ''){
           
//stupid hack to prevent mssql driver from converting empty strings to null. now they arent empty strings but they will get trimmed down to legth 0 ;-)
       
$var = ' ';
       
$type = SQLVARCHAR;
       
$isNull = false;
       
$maxLen = 0;
    }

mssql_bind($stmt, $param_name, $var, $type, $is_output,$isNull$maxLen);
?>
alvaro at demogracia dot com 24-Nov-2009 12:12
There isn't a bind function for regular SQL queries; not even a escape function. I found this nice piece of code:

<?php
function mssql_escape($data) {
    if(
is_numeric($data))
        return
$data;
   
$unpacked = unpack('H*hex', $data);
    return
'0x' . $unpacked['hex'];
}
?>

http://stackoverflow.com/questions/574805/
Anonymous 17-Nov-2009 07:35
I found SQLVARCHAR better for datetime.

It was performing some other non-strtotime() convertions when it was set to SQLINT4
valerio dot della-porta dot com 22-Apr-2009 11:00
Use:
SQLVARCHAR for binary
SQLINT4 for datetime
SQLFLT8 for decimal
SQLVARCHAR for image
SQLFLT8 for money
SQLCHAR for nchar
SQLTEXT for ntext
SQLFLT8 for numeric
SQLVARCHAR for nvarchar
SQLFLT8 for real
SQLINT4 for smalldatetime
SQLFLT8 for smallmoney
SQLVARCHAR for sql_variant
SQLINT4 for timestamp
SQLVARCHAR for varbinary
Anonymous 25-Jan-2009 02:08
<?php
//IP Address, if instance then IP\Instance
$server = 'a.b.c.d';
$link = mssql_connect($server, 'sql_user', 'sql_user_pass');

//Select DB
$dbn = 'dbName';
mssql_select_db($dbn);

//Define Procedure
$lala = 'tstProc';
$proc = mssql_init($lala, $link);

//Define Parameters
$parm1 = 'one';
$parm2 = 'two';
$parm3 = 'three';

//Load Parameters
mssql_bind($proc, '@num', $parm1, SQLCHAR, false, false, 10);
mssql_bind($proc, '@naamen', $parm2, SQLCHAR, false, false, 10);
mssql_bind($proc, '@desci', $parm3, SQLCHAR, false, false, 10);

//Execute Procedure
mssql_execute($proc);

//Free Memory
mssql_free_statement($proc);

//...and whenever the wolf did howl, all the sheep had to do was bleat!
?>
Anonymous 08-Jun-2008 07:52
for type :

SQLCHAR     DBCHAR
SQLVARCHAR     DBCHAR
SQLTEXT     DBCHAR
SQLBINARY     DBBINARY
SQLVARBINARY     DBBINARY
SQLIMAGE     DBBINARY
SQLINT1     DBTINYINT
SQLINT2     DBSMALLINT
SQLINT4     DBINT
SQLFLT4     DBFLT4
SQLFLT8     DBFLT8
SQLBIT     DBBIT
SQLMONEY4     DBMONEY4
SQLMONEY     DBMONEY
SQLDATETIM4     DBDATETIM4
SQLDATETIME     DBDATETIME
SQLDECIMAL     DBDECIMAL
SQLNUMERIC     DBNUMERIC

source : http://msdn.microsoft.com/en-us/library/aa937008(SQL.80).aspx
daryl dot mitchell at usask dot ca 05-Dec-2007 06:56
I had the same problem but the posted solution above just produced null results.  Here's a modification that ended up working:

#THIS SUCCEEDS, USES A REFERENCE
mssql_bind($proc, '@'.$key, $sp_parms[$key], SQLVARCHAR)
or die("Unable to bind $sp_name:$key<br>".mssql_get_last_message());
fheald at buzztime dot com 06-Aug-2007 09:27
mssql_bind binds by reference, not by value, even on input parameters.  Improper binding can cause strange errors; in my case "Error converting data type varchar to int"

--SAMPLE STORED PROCEDURE
CREATE Procedure [dbo].[myproc]
(
    @one VARCHAR(10) = 'n1',
    @two VARCHAR(10) = 'n2',
    @three VARCHAR(10) = 'n3',
    @four VARCHAR(10) = 'n4',
    @five VARCHAR(10) = 'n5'
)
AS
BEGIN
SET NOCOUNT ON;

SELECT
    @one AS 'one',
    @two AS 'two',
    @three AS 'three',
    @four AS 'four',
    @five AS 'five'
END

//SAMPLE PHP CALL
$sp_name = 'mydb.dbo.myproc';
$proc = mssql_init($sp_name);
$sp_parms->one = 'one';
$sp_parms->two = 'two';
$sp_parms->three = 'three';

foreach ($sp_parms as $key=>$parm) {
    #THIS FAILS, because it's binding values!
    #mssql_bind($proc, '@'.$key, $parm, SQLVARCHAR)
    #    or die("Unable to bind $sp_name:$key<br>".mssql_get_last_message());
    #THIS SUCCEEDS, USES A REFERENCE
    mssql_bind($proc, '@'.$key, $sp_parms->$key, SQLVARCHAR)
        or die("Unable to bind $sp_name:$key<br>".mssql_get_last_message());
}

 
show source | credits | stats | sitemap | contact | advertising | mirror sites