I'm using the sqlsrv extension for php and have a problem with fetching the rows from a simple query.
My query is a select which should return 133228 rows but when trying to display the rows I get only 15.
I've searched for an answer but couldn't find a solution and this is my first time using this extension. I've found an answer to a previous question about the same problem but in that case the problem was double calling of sqlsrv_fetch_array, which I don't have for sure.
Here is my query:
$sql = 'select * from ViewProduct';
$params = array();
$options = array('Scrollable' => SQLSRV_CURSOR_KEYSET);
$stmt = sqlsrv_query($dbRemote, $sql, $params, $options);
$count = sqlsrv_num_rows($stmt);
if ($count === false)
echo "Error in retrieveing row count.";
else
echo $count;
//$rows = array();
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
print_r($row);
echo "<br>";
//$rows[] = $row;
}
As I said the above query returns 15 when should be 133228 rows.
What am I missing?
Thank you in advance.
i have same problem,
this is my solution for this
$data = [];
$result_num = false;
// get rows num if have any
$result_count_res = sqlsrv_query(
$conn,
$query,
[],
[ "Scrollable" => SQLSRV_CURSOR_KEYSET ]
);
if( $result_count_res === false) {
echo "Row count false";
}else{
// set row num
$result_num = sqlsrv_num_rows( $result_count_res );
}
if($result_num){
// run another query to retrive results
$result_res = sqlsrv_query(
$conn,
$query,
[],
[ "Scrollable" => SQLSRV_CURSOR_FORWARD ]
);
if( $result_res === false) {
// get errors from query
die( print_r( sqlsrv_errors(), true) );
}else{
// run through "for" loop to retrive all results
for($i = 0; $i < $result_num; $i++){
$data[] = sqlsrv_fetch_array( $result_res, SQLSRV_FETCH_ASSOC);
}
}
sqlsrv_free_stmt( $result_res );
}else{
echo "Row count false";
}
echo '<pre>';
var_dump($data);
echo '</pre>';
Related
I'm running a query to get the contents for a web slider.
$serverName = "livedata";
$connectionInfo = array( "Database"=>"DB", "UID"=>"User", "PWD"=>"PWD" );
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
$sql = "SELECT Sliders.DisplayFrom, Sliders.DisplayUntil, Sliders.Sort, Sliders.Image, Sliders.Link, Sliders.Target FROM Sliders WHERE (((Sliders.DisplayFrom)<GetDate()) AND ((Sliders.DisplayUntil)>getdate())) ORDER BY Sliders.sort;";
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
$result = sqlsrv_query($conn, $sql);
$maxx = 0;
while($row = sqlsrv_fetch_array($result)) {
$maxx++;
}
while($row = sqlsrv_fetch_array($result)) {
echo “<br>” . $row[‘Image’];
}
The second loop does not output any results. why? Is it because $row is already at the end? If so, how do I move first like in ASP without having to create $result2 and $row2, 3, 4, 5, ...
Would it be better to use for loops like in vb.net?
for ($i = 0; $i <= $maxx; $i++){
}
But then How do I specify the output if I have no $row?
and if I only wanted to output the first row like I do in vb.net, how would I write the following in PHP?
dsqueryResults.tables(0).rows(0).item("Image");
would I still use a while loop and have a variable inside the loop to hold the row number and only output if row = 0?
$WhichRow = 0;
while($row2 = sqlsrv_fetch_array($result2)) {
if ($whichRow == 0){
echo “<br>” . $row2[‘Image’];
}
$WhichRow++;
}
Use the $max++ counter inside the while loop if you really want to use that for other purposes.
If you just want a count then run a count query on MySQL or do an array count of returned result.
If you just want to print 1 row use
LIMIT 0,1
at the end of your query as there's no point in fetching all rows and waste resources.
Based on additional comments you can try this to get your current row number.
$result = sqlsrv_query($conn, $sql);
$rownumber = 0;
while($row = sqlsrv_fetch_array($result)) {
$rownumber = $rownumber + 1;//Your current row number.
echo “<br>” . $row[‘Image’];
}
I pull data from the mssql database. I want to assign the data I have captured as an array. So I want to assign more than one table value to the result. Currently, it only assigns 1 value. When I make $ results [], I can't print. I converted it to an array so I can assign more than one data, but it doesn't work.
<?php
...
$conn = sqlsrv_connect( $serverName, $connectionInfo );
$sql = "...";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $row = sqlsrv_fetch_array($stmt) ) {
$destekCevap = $row['destekcevap_..'];
$destekCevapFoto = $row['destekcevap_...'];
$results = Array("destekcevap_.." => $destekCevap, "destekCevapFoto" => $destekCevapFoto);
}
echo json_encode($results);
sqlsrv_free_stmt($stmt);
?>
Initialize the array above the while-loop and assign new values in the loop like this:
$results = array();
while( $row = sqlsrv_fetch_array($stmt) ) {
$destekCevap = $row['destekcevap_..'];
$destekCevapFoto = $row['destekcevap_...'];
$results[] = "your values";
}
Cheers,
Niklas
My json code doesn't display anything, I have already tried many codes but nothing has helped.
include('connect.php');
$sql = "SELECT * FROM items";
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt === false)
{
echo "Error in query preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) //this loop is working
{
echo $row['item_id'].", ".$row['item_name'].", ".$row['Barcode']."<br>";
}
$json = array();
do {
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$json[] = $row;
}
} while ( sqlsrv_next_result($stmt) );
echo json_encode($json); //empty?!
sqlsrv_free_stmt( $stmt);
There are numerous likely issues with this:
1) Have you checked your query actually returns rows?
2) You're looping your data twice (two while( $row = sqlsrv_fetch_array... loops) which is not useful or efficient.
3) the do...while ( sqlsrv_next_result($stmt) ); clause should be unnecessary as well, as fetch_array will know when it's got to the end of the data, and you've only got one resultset, so you don't need to move between them
4) you're echoing raw data as well as JSON, so if you make an ajax call to this script it'll fail because the response will partly contain non-JSON data
I think this will be sufficient to get you some sensible data:
include('connect.php');
$sql = "SELECT * FROM items";
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt === false)
{
echo "Error in query preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
$json = array();
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{
$json[] = $row;
}
echo json_encode($json);
If THIS works:
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) //this loop is working
{
echo $row['item_id'].", ".$row['item_name'].", ".$row['Barcode']."<br>";
}
the rest must work too.
As ADyson says:
if( $stmt === false)
{
echo "Error in query preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
$json = array();
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{
$json[] = $row;
}
echo json_encode($json);
for double check add your echo in this code, like this:
if( $stmt === false)
{
echo "Error in query preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
$json = array();
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{
echo $row['item_id'].", ".$row['item_name'].", ".$row['Barcode']."<br>";
$json[] = $row;
}
echo json_encode($json);
If this code works, accept the ADyson answer
here is a way to solve the issue .Hoping your query is well written.
$dataFinal= array();//final variable that will contain total array for json
for($k=0;$k<count(variable_that_contents_the_resutl_array_query);$k++){
$ligne = array("item_id"=> $row['item_id'],
"item_name"=>$row['item_name'],"Barcode"=> $row['Barcode']);
array_push($dataFinal, $ligne);//add line in array datafinal
}//end for loop
$result = array($dataFinal);
$response = new Response(json_encode($dataFinal));
$response->headers->set('Content-Type', 'application/json');
return $response;
shortly I'm facing an issue using while loop... I don't know what is going on, So, I thought that I should ask for help, after a lot of searches, I know that mysql_fetch_array returns a row in every time, so we should use while if we need to get all rows from the query.
and after trying to avoid while and use foreach I found that simply we can't use foreach instead of while or we can use it as another loop after while.
anyways, my problem is: I created a stored procedure in mysql database, and it working fine throw my php code using ajax, but when I tried to put it in a loop based on IDs of some data, the while loop gave me the first row only after calculations.
so please if anyone can see what I can't see in the code, don't hesitate.
thanks..
$startdate = $_POST['startdate'];
$enddate = $_POST['enddate'];
$thefinalres="";
$loop = mysql_query("SELECT * FROM mrh_chains order by id");
if ($loop){
while ($row = mysql_fetch_array($loop))
{
$theid = $row['id'];
$thename = $row['name'];
//CALL PROCEDURE
$result = mysql_query("CALL calccommission($theid,'$startdate','$enddate')");
if ($result){
$row2 = mysql_fetch_array($result);
if ($row2[0]!=""){
$thecommission = $row2[0];
} else {
$thecommission = "Try to change dates, There are no data on these dates";
}
$thefinalres .= $thename . " commission: ". $thecommission . "<br/>";
}
}
$value = array('msg' => 'true' );
$value["res"] = $thefinalres;
} else {
$value = array('msg' => 'false' );
}
after a looooooooooooooooot of search, I found solution with mysqli
$mysqli = new mysqli( "HOST", "USR", "PWD", "DBNAME" );
$ivalue=1;
$res = $mysqli->multi_query( "CALL myproc($ivalue,#x);SELECT #x" );
if( $res ) {
$results = 0;
do {
if ($result = $mysqli->store_result()) {
printf( "<b>Result #%u</b>:<br/>", ++$results );
while( $row = $result->fetch_row() ) {
foreach( $row as $cell ) echo $cell, " ";
}
$result->close();
if( $mysqli->more_results() ) echo "<br/>";
}
} while( $mysqli->next_result() );
}
$mysqli->close();
I found answer in this article: https://forums.mysql.com/read.php?52,198596,198717
SELECT * FROM dbo.Warehouse returns result, but when I change to SELECT * FROM dbo.Accessories, it neither shows any result nor echo 'Rows not found'. Both Warehouse and Accessories tables have rows. Here is my script.
<?php
require('db_connect.php');
$query = 'SELECT * FROM dbo.Warehouse';
$params = array();
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$stmt = sqlsrv_query($conn, $query, $params, $options);
if(!$stmt){
die(print_r(sqlsrv_errors(),true));
}
$c = 0;
$result[] = array();
if(sqlsrv_num_rows($stmt) != 0){
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
$row = array($c => $row);
$result[$c] = $row;
$c++;
}
} else {
echo("Rows not found");
}
echo json_encode($result);
?>
Please help me, thanks in advance. :(