PHP 7.2.21
Database is on MS SQL, and the database connection is working
I have found and read sqlsrv_num_rows Not Returning Any Value. I have updated the $options based on the solution, but my code still does not return any results.
If I run the query in SQL, I get 8 records, so I know the data is there.
$sql = "SELECT * FROM Question WHERE Category = 'HD';";
$params = array();
$options = array( "Scrollable" => 'keyset' );
$stmt = sqlsrv_query( $conn, $sql, $params, $options );
if( $stmt === false ) { reportSQLError($sql, $params); }
$maxHDQ = sqlsrv_num_rows($stmt);
The code itself is ok and should work. What are the values for $stmt and $maxHDQ?
It is suddenly working
I just removed 'HD' from the query and added it to $params and this seems to have resolved everything.
I am not sure why it is working this way as the final query sent to SQL comes out the same either way I run it, however it does work.
$sql = "SELECT * FROM Question WHERE Category = ?;";
$params = array('HD');
$options = array( 'Scrollable' => 'buffered' );
$stmt = sqlsrv_query( $conn, $sql, $params, $options );
if( $stmt === false ) { reportSQLError($sql, $params); }
$maxHDQ = sqlsrv_num_rows($stmt);
Related
I'm using the sqlsrv to fetch data from a sql server database. This database is in a diferent server of PHP (outside local network).
I using a query with lots of UNION. If I run the query on Management Studio it takes around 4-5 seconds to fetch all the data... But if I use the PHP and sqlsrv it takes around 57 seconds!
This is the code I'm using:
$connectionInfo = array("Database"=>$db, "UID"=>$u, "PWD"=>$p, "CharacterSet"=>"UTF-8");
$conn = sqlsrv_connect($serverName, $connectionInfo);
$errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
$tsql = $query;
$stmt = sqlsrv_query($conn, $tsql, array(), array( "Scrollable" => 'static' ));
$json = array();
do {
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$json[] = $row;
}
} while ( sqlsrv_next_result($stmt) );
echo json_encode($json);
Any idea of what I'm doing wrong?
Try this code:
$this->stmt = sqlsrv_query($this->conn, $sql, null, array( "Scrollable" => SQLSRV_CURSOR_FORWARD ));
SQLSRV_CURSOR_STATIC Lets you access rows in any order but will not reflect changes in the database. static is the abbreviated form of SQLSRV_CURSOR_STATIC.
Please take a look at: https://learn.microsoft.com/en-us/sql/connect/php/cursor-types-sqlsrv-driver
I have this function which I use to get a value from a query. I might be doing something wrong with the execution, or the syntax. When I try to run the query with the data in it, it's fine, but this one returns 0 items.
public function get_modified_event ($type, $id, $employee_id)
{
global $dbh;
$sql =<<<SQL
SELECT outlook_id
FROM dba.events
WHERE spine_item_type = :type
AND spine_id = :id
AND employee_id = :employee_id
SQL;
$stmt = $dbh->prepare( $sql );
$stmt->execute( array(
'id' => $id,
'type' => $type,
'employee_id' => $employee_id,
) );
$rows = $stmt->fetchAll( \PDO::FETCH_ASSOC );
return $rows['outlook_id'];
}
You could try reworking a portion of the code so that it uses this kind of a format:
$stmt = $dbh->prepare( $sql );
if ($stmt->execute( /* your array goes here */ )) {
$rows = $stmt->fetchAll( PDO::FETCH_ASSOC );
if ($rows !== FALSE){
print_r($rows);
}
else
if ( $stmt->rowCount() === 0) {
echo "The info provided is unknown to the database";
}
}
Hopefully you'll get some meaningful results.
I am trying to execute a sql server stored procedure from php. Stored procedure returns a output value. I want that value in php to display in webpage.
Everything works fine except the output value returns 0 in php.. but the row is getting inserted perfectly. the output value shows as 0. annoyed where I have made the mistake. Below is my php code
require('db.php');
$billno = "REF0001";
$retsno = 0.0;
$sp_name = "{call testinsert( ?, ? )}";
$params = array(
array($billno, SQLSRV_PARAM_IN),
array($retsno, SQLSRV_PARAM_OUT)
);
/* Execute the query. */
$stmt3 = sqlsrv_query( $conn, $sp_name, $params);
if( $stmt3 === false )
{
echo "Error in executing statement 3.\n";
die( print_r( sqlsrv_errors(), true));
}
echo "Inserted Retuern Sno for ".$billno." is ". $retsno. ".";
and my stored procedure is
create procedure testinsert(#bill_no VARCHAR(20),#RetSno INT OUTPUT)
as
begin
insert into testable values(#bill_no,GETDATE())
set #RetSno = ##IDENTITY
return
end
Maybe you missed scrollable. https://msdn.microsoft.com/en-us/library/hh487160(v=sql.105).aspx
$stmt3 = sqlsrv_query( $conn, $sp_name, $params, array( "Scrollable" => 'static'));
Before you use your echo, add the following line to read the next line:
sqlsrv_fetch($stmt3);
Then you can select the first field of the response, which should be your parameter, with sqlsrv_get_field():
$id = sqlsrv_get_field(0);
I had the same case, and solved it using:
sqlsrv_next_result($stmt);
at the end of the instruction. Here I put part of my code in case it helps you.
$totalPaginas = 0;
$result = array();
$query = "EXEC DBO.SPS_EJECUTAR_CONSULTA #ReporteID = ?, #FilasPorPagina = ?, #NroPagina = ?, #TotalPaginas = ?";
$params = array(
array(&$reporteid, SQLSRV_PARAM_IN),
array(&$filasPorPagina, SQLSRV_PARAM_IN),
array(&$nroPagina, SQLSRV_PARAM_IN),
array(&$totalPaginas, SQLSRV_PARAM_OUT)
);
$stmt = sqlsrv_prepare($this->db, $query, $params);
if( !$stmt ) {
// show errors
}
$result = sqlsrv_execute($stmt);
if( !$result ) {
// show errors
}
$data = array();
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$data[] = $row;
}
sqlsrv_next_result($stmt);
var_dum($data);
echo('Total Paginas: '.$totalPaginas);
Officially tells you how to do it here:
https://learn.microsoft.com/en-us/sql/connect/php/how-to-retrieve-input-and-output-parameters-using-the-sqlsrv-driver
I am using adapter in zend framework 2. My code is for mysql update query which is not working, can any one suggest me. Array result is ok but its not display in query. Just shows update tablename set.
I have tried all suggestions of SO and also from Google, but I am failed to solve that problem.
Code is here:
public function updatebhkdetail($bhkupdate)
{
$WHERE = 'project_id='.$bhkupdate['project_id'];
$sql = new Sql($this->adapter);
$update1bhk = $sql->update('tablename', array($bhkupdate), $WHERE);
$statementUpdate = $sql->getSqlStringForSqlObject($update1bhk);
$sectorName = $this->adapter->query($statementUpdate,
\Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
}
I'm not to familiar with getSqlStringForSqlObject. But this should work:
$sql = new Sql( $this->adapter );
$update = $sql->update();
$update->table( <yourTableName> );
$update->set( $keyValues );
$update->where( array( 'project_id' => $bhkupdate['project_id'] ) );
$statement = $sql->prepareStatementForSqlObject( $update );
$results = $statement->execute();
I'm having a really annoying problem with a simple query.
So:
INSERT INTO [SapuV2].[dbo].[ALERGIA] ([DESCRIPCION]) OUTPUT INSERTED.PK_ALERGIA VALUES ('test')
PK_ALERGIA is a primary identity key, and i need the new id created.
PHP code:
$sql = "INSERT INTO ALERGIA (DESCRIPCION) OUTPUT INSERTED.PK_ALERGIA VALUES(?)";
$id = 0;
$params = array(
array(&$nombre, SQLSRV_PARAM_IN),
array(&$id, SQLSRV_PARAM_OUT, SQLSRV_PHPTYPE_INT)
);
$stmt = sqlsrv_prepare($this->Conn, $sql, $params);
if( sqlsrv_execute($stmt) === false){
die( print_r( sqlsrv_errors(), true) );
}else{
var_dump($id);
}
I'm able to save records on my BD, but $id is always 0 ...
Thanks ;)
Please try this:
INSERT INTO ALERGIA (DESCRIPCION)select #var=scope_identity()
I finally did it, but i'm not proud ...
Using same SQL Query, but change the sqlsrv_execute to a sqlsrv_fetch_array
$sql = "INSERT INTO ALERGIA ([DESCRIPCION]) OUTPUT INSERTED.PK_ALERGIA VALUES (?)";
$params = array(
array(&$nombre, SQLSRV_PARAM_IN)
);
$rows = sqlsrv_query($this->Conn, $sql, $params);
if($rows === false){
die(var_dump(sqlsrv_errors()));
}else{
while($row = sqlsrv_fetch_array($rows, SQLSRV_FETCH_ASSOC)){
var_dump($row);
}
}