I am trying to communicate to a MSSQL Server database with PHP from a woocommerce website (to fetch data like products, categories etc.). But I get no results, here is my code:
$connectionInfo = array( "UID"=>$uid,
"PWD"=>$pwd,
"Database"=>$databaseName);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$tsql = "SELECT * FROM eshopItemsTable";
$stmt = sqlsrv_query($conn, $tsql);
//uncomment to get some results : $table = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
while( $table = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) ) {
print_r($table);
}
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
with this code it prints nothing. But if I uncomment the line above while
then I get some results but I dont get all of them (there are ~2000 items in the db but I get something like 10 items without the first one – which is obvious because I already consume the first row). What is the proper way to get all the results?
4
Answers
Try this way:
If I were you I would use the tools I already have – in case of WordPress it’s
wpdb
. Normally when you usewpdb
you’re fetching from the WordPress databse but in our case we connect to a different database before using its methods:This handles the timeout stuff too – normally. Try out by logging out the results and tell me if it works. If yes, I think you can continue working with this.
Please check with this code, where you can print errors step by step.
Seems something wrong with the while.
The while is ok, but seems like ends before that he had to.
I’ve looking for some solutions and this man had the same problem:
SQLSRV doesn't fetch all rows
First of all gets the num of results:
And then use a for instead of a while.
Consider also, a time out. Because you are attacking to another server…