php 5.2 and MSSQL (sqlsrv driver 1.0) – tipe Bytes (links)??? – PHP

  php

Q(Question):

hi!

system:
lang: php5.2.*
server: mssql express *.1399 (2005)
conn.driver: php_sqlsrv.dll (version 1.0)

i have a small table "Table01" at express server with fields:

id (int)
name (ntext)
company (bytes) – link to "Table02"

$query = sqlsrv_query($conn, ‘SELECT * FROM Table01’);
$massarray=sql_fetch_all($query);

echo $massarray[$i][‘company’]."<br>";//bytes

sqlsrv_close($conn);

//=============================================
function sql_fetch_all($query){
$i=0;
$massarray=array();
while( $row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){
$massarray[$i]=$row;
$i=$i+1;
}
return $massarray;
}

The view of $massarray[$i][‘company’] is not correct..

A(Answer):

Hi and welcome to Bytes. Please remember to wrap any code within [code][/code] tags.

Can you please give an example of what isn’t right about the return?

I can give you a tip right off the bat though, when creating a dynamic array in PHP, if you only want the array to be numbered, for example [0] = zero, [1] = one, etc. then you don’t need to specify a number in the brackets. You also don’t need to instantiate the array beforehand, simply calling $variable[] is enough to create the array. A better way to do your function would be:

function sql_fetch_all($query) {
while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
$massarray[] = $row;
}
return $massarray;
}

This will do the exact same thing as your prvious function, but with less lines.

Also understand that you can’t echo an array, it will always only print "Array( )". To print an array, use print_r($array).

Hope this helps, please let me know if you have more questions.

A(Answer):

sorry
it’s means:


for ($i=0;$i<count($massarray);$i++){
echo $massarray[$i]['company']."<br>";
}
$massarray["it's row number"]["it's SQLSRV_FETCH_ASSOC column - type is BYTE"]

A(Answer):

i cann’t correct echo an array’s colimns with type is BYTE (link to "table02")

A(Answer):

Ok, try commenting out your current function sql_fetch_all() and insert the one that I wrote in my first post, then comment out the line:

echo $massarray[$i][‘company’]."<br>";//bytes

and put this after it:

echo "<pre>";
print_r($massarray);
echo "</pre>";

It won’t be formatted how you want, but does it return the data that you want? Does it have the SQLSRV_FETCH_ASSOC column data?

A(Answer):


[0] => Array
(
[IDRRef] => "fdsg556fd3; //BYTE, not correct it's guid
[Marked] =>
[IsMetadata] =>
[ParentIDRRef] => «Яґ#lИS” //BYTE not correct
[Fld13] => DateTime Object //DATETIME
(
)
)

A(Answer):

Well immediately I see that the "Company" column doesn’t exist in that table, so that might be your problem.

Is there any reason the data needs to be in BYTE form, why not text or int? I’m not sure what you’re going for here.

LEAVE A COMMENT