PHP V7.4.14, convert this database function, understanding mssql_fetch_field to sqlsrv_fetch
-
I'm trying to convert this function written in V4.7.3 to V7.4. This function is used quite often in the app. I get what it does in a general sense, but not at expert level, because I lost my expert PHP database skills back in 2005. I'm struggling to translate mssql_fetch_field. I tried sqlsrv_fetch_array and the result was 1. Then I tried sqlsrv_get_field and the result was 1. The webpage errors say:
meta= 1
Notice: Trying to get property 'name' of non-object in C:\App\Dev\PCAD\class\cls_db_tools.php on line 137
Notice: Trying to get property 'name' of non-object in C:\App\Dev\PCAD\class\cls_db_tools.php on line 141
meta= 1I don't understand the 1 that was returned. Obviously 1 doesn't contain a name, and I expected $meta to be a result. And I don't have the old dev server to go back to to see what it's suppose to return with the old code. Here's the old function
function db_Sel_Query($sqlQuery) {
$Rows = Array(); $resQuery = mssql\_query($sqlQuery) or die(); $cRows = mssql\_num\_rows($resQuery); $cFields = mssql\_num\_fields($resQuery); $rgFields = Array(); for ($i = 0; $i < $cFields; $i++) { $meta = mssql\_fetch\_field($resQuery); $rgFields\[$i\] = $meta->name; } for ($i = 0; $i < $cRows; $i++) { $row = mssql\_fetch\_row($resQuery); for ($col = 0;$col < $cFields;$col++) { $Rows\[$i\]\[$rgFields\[$col\]\] = rtrim($row\[$col\]); } } mssql\_free\_result($resQuery); return $Rows;
}
The new function I wrote
public static function dbSelectQuery($sqlQuery): array {
$Rows = array(); $rgFields = array(); $params = array(); $options = array( "Scrollable" => SQLSRV\_CURSOR\_KEYSET ); $conn = clsDbConnect::createConn(); $resQuery = sqlsrv\_query($conn, $sqlQuery, $params, $options) or die(); $rowCount = sqlsrv\_num\_rows($resQuery); $fieldCount = sqlsrv\_num\_fields($resQuery); for ($i = 0; $i < $fieldCount; $i++) { $meta = sqlsrv\_fetch\_array($resQuery, SQLSRV\_FETCH\_ASSOC, $i); // $meta = sqlsrv\_get\_field($resQuery, $i); echo "
meta= " . print_r($meta) . "
";
if ($meta->name) {
echo "meta.name= $meta->name
";
}$rgFields\[$i\] = $meta->name; } for ($i = 0; $i < $rowCount; $i++) { $row = sqlsrv\_fetch\_array($resQuery,SQLSRV\_FETCH\_NUMERIC); for ($col = 0; $col < $fieldCount;
-
I'm trying to convert this function written in V4.7.3 to V7.4. This function is used quite often in the app. I get what it does in a general sense, but not at expert level, because I lost my expert PHP database skills back in 2005. I'm struggling to translate mssql_fetch_field. I tried sqlsrv_fetch_array and the result was 1. Then I tried sqlsrv_get_field and the result was 1. The webpage errors say:
meta= 1
Notice: Trying to get property 'name' of non-object in C:\App\Dev\PCAD\class\cls_db_tools.php on line 137
Notice: Trying to get property 'name' of non-object in C:\App\Dev\PCAD\class\cls_db_tools.php on line 141
meta= 1I don't understand the 1 that was returned. Obviously 1 doesn't contain a name, and I expected $meta to be a result. And I don't have the old dev server to go back to to see what it's suppose to return with the old code. Here's the old function
function db_Sel_Query($sqlQuery) {
$Rows = Array(); $resQuery = mssql\_query($sqlQuery) or die(); $cRows = mssql\_num\_rows($resQuery); $cFields = mssql\_num\_fields($resQuery); $rgFields = Array(); for ($i = 0; $i < $cFields; $i++) { $meta = mssql\_fetch\_field($resQuery); $rgFields\[$i\] = $meta->name; } for ($i = 0; $i < $cRows; $i++) { $row = mssql\_fetch\_row($resQuery); for ($col = 0;$col < $cFields;$col++) { $Rows\[$i\]\[$rgFields\[$col\]\] = rtrim($row\[$col\]); } } mssql\_free\_result($resQuery); return $Rows;
}
The new function I wrote
public static function dbSelectQuery($sqlQuery): array {
$Rows = array(); $rgFields = array(); $params = array(); $options = array( "Scrollable" => SQLSRV\_CURSOR\_KEYSET ); $conn = clsDbConnect::createConn(); $resQuery = sqlsrv\_query($conn, $sqlQuery, $params, $options) or die(); $rowCount = sqlsrv\_num\_rows($resQuery); $fieldCount = sqlsrv\_num\_fields($resQuery); for ($i = 0; $i < $fieldCount; $i++) { $meta = sqlsrv\_fetch\_array($resQuery, SQLSRV\_FETCH\_ASSOC, $i); // $meta = sqlsrv\_get\_field($resQuery, $i); echo "
meta= " . print_r($meta) . "
";
if ($meta->name) {
echo "meta.name= $meta->name
";
}$rgFields\[$i\] = $meta->name; } for ($i = 0; $i < $rowCount; $i++) { $row = sqlsrv\_fetch\_array($resQuery,SQLSRV\_FETCH\_NUMERIC); for ($col = 0; $col < $fieldCount;
I think I figured it out. What I'm trying to get is the column names in the meta data of the table. The example use a foreach loop, so now I know how to use foreach in PHP V7+ Seems needs some work and polishing.
public static function dbSelectQuery($sqlQuery): array {
// print\_r(debug\_backtrace()); $Rows = array(); $rgFields = array(); $params = array(); $options = array( "Scrollable" => SQLSRV\_CURSOR\_KEYSET ); $conn = clsDbConnect::createConn(); $resQuery = sqlsrv\_query($conn, $sqlQuery, $params, $options) or die(" cls\_db\_tools 128: " . $sqlQuery . print\_r(sqlsrv\_errors()) ); $rowCount = sqlsrv\_num\_rows($resQuery); $fieldCount = sqlsrv\_num\_fields($resQuery); $cIndex = 0; foreach( sqlsrv\_field\_metadata( $resQuery ) as $fieldMetadata ) { $columnName = $fieldMetadata\['Name'\]; $rgFields\[$cIndex\] = $columnName; $cIndex++; } for ($i = 0; $i < $rowCount; $i++) { $row = sqlsrv\_fetch\_array($resQuery,SQLSRV\_FETCH\_NUMERIC); for ($col = 0; $col < $fieldCount; $col++) { $Rows\[$i\]\[$rgFields\[$col\]\] = rtrim($row\[$col\]); } } sqlsrv\_free\_stmt($resQuery); return $Rows;
}
If it ain't broke don't fix it Discover my world at jkirkerx.com