I am working on migrating our data from using inline SQL to using Stored Procedures. I am hitting a snag when running my code and getting the errors shown below:
Warning: sqlsrv_execute() expects parameter 1 to be resource, bool given in ajax_functions1.live.php on line 285
Warning: sqlsrv_fetch_array() expects parameter 1 to be resource, bool given in ajax_functions1.live.php on line 296
Below is an excerpt of the code:
function db_add_user($email, $provider, $subuid,$data){
include 'config.php';
$email = $data['preferred_username'];
$params = array(
array(&$myparams['email'], SQLSRV_PARAM_IN),
array(&$myparams['provider'], SQLSRV_PARAM_IN)
);
$sql = "{EXEC [dbo].[getSubUniqueID_HPCreate] @email = ?, @provider = ?}";
$stmt = sqlsrv_prepare($conn, $sql, $params);
sqlsrv_execute($stmt);
$uid=null;
if($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) > 0) {
$uid = $row[0]['sub_unique_id'];
}else{
$params = array(
array(&$myparams['email'], SQLSRV_PARAM_IN),
array(&$myparams['provider'], SQLSRV_PARAM_IN),
array(&$myparams['subuid'], SQLSRV_PARAM_IN)
);
$sql = "{EXEC [dbo].[insertSubUniqueID_HPCreate] @email = ?, @provider = ?, @subuid = ?}";
$stmt = sqlsrv_prepare($conn, $sql, $params);
sqlsrv_execute($stmt);
}
return $uid;
}
Here is the SP dbo.getSubUniqueID_HPCreate
USE [homepages_config]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[getSubUniqueID_HPCreate]
@email nvarchar(256),
@provider nvarchar(64)
AS
BEGIN
SET NOCOUNT ON;
SELECT sub_unique_id FROM homepages_config.dbo.users_providers
WHERE email = @email AND provider_id = @provider;
END
Here is the SP dbo.insertSubUniqueID_HPCreate
USE [homepages_config]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[insertSubUniqueID_HPCreate]
@email nvarchar(256),
@provider nvarchar(256),
@subuid nvarchar(1024)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO homepages_config.dbo.users_providers ([email], [provider_id], [sub_unique_id])
VALUES (LOWER(@email), @provider, @subuid)
END
Any help would be greatly appreciated.
First, the stored procedures are correct. One issue is the different declaration for the @provider
parameter ( @provider nvarchar(64)
and @provider nvarchar(256)
), but it's probably a typing error.
So, I think that you should make some changes in your script:
{call sp_name (?, ...)}
syntax. This is a possible reason for the Warning: sqlsrv_execute() expects parameter 1 to be resource, bool given in ajax_functions1.live.php on line ... error, because function sqlsrv_prepare()
is not executed correctly and you need to check the result from this execution. Note, that you may use sqlsrv_query()
, because this function does both statement preparation and statement execution, and can be used to execute parameterized queries.db_add_user()
function.sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)
is an associative array, so $row[0]['sub_unique_id']
is an error. The following script (based on your code) is a possible solution to your problem:
<?php
function db_add_user($email, $provider, $subuid){
include 'config.php';
$params = array(
array($email, SQLSRV_PARAM_IN),
array($provider, SQLSRV_PARAM_IN)
);
$uid = null;
$sql = "{call [dbo].[getSubUniqueID_HPCreate]( ?, ? )}"
$stmt = sqlsrv_query($conn, $sql, $params);
if ($stmt === false) {
echo print_r(sqlsrv_errors(), true);
return null;
}
if ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$uid = $row['sub_unique_id'];
} else {
$params = array(
array($email, SQLSRV_PARAM_IN),
array($provider, SQLSRV_PARAM_IN),
array($subid SQLSRV_PARAM_IN)
);
$sql = "{call [dbo].[insertSubUniqueID_HPCreate]( ?, ?, ? )}"
$stmt = sqlsrv_query($conn, $sql, $params);
if ($stmt === false) {
echo print_r(sqlsrv_errors(), true);
return null;
}
}
return $uid;
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.