PHP Doku:: Liefert ein assoziatives Array des aktuellen Datensatzes aus dem Ergebnis, das durch die Ergebniskennung bestimmt ist - function.mssql-fetch-assoc.html

Verlauf / Chronik / History: (24) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDatenbankerweiterungenAnbieterspezifische DatenbankerweiterungenMicrosoft SQL ServerMssql-Funktionenmssql_fetch_assoc

Ein Service von Reinhard Neidl - Webprogrammierung.

Mssql-Funktionen

<<mssql_fetch_array

mssql_fetch_batch>>

mssql_fetch_assoc

(PHP 4 >= 4.2.0, PHP 5, PECL odbtp >= 1.1.1)

mssql_fetch_assocLiefert ein assoziatives Array des aktuellen Datensatzes aus dem Ergebnis, das durch die Ergebniskennung bestimmt ist

Beschreibung

array mssql_fetch_assoc ( resource $result_id )

Gibt ein assoziatives Array mit dem abgerufenen Datensatz zurück und rückt den internen Datenzeiger weiter. mssql_fetch_assoc() entspricht dem Aufruf von mssql_fetch_array() mit MSSQL_ASSOC als optionalem zweitem Parameter.

Parameter-Liste

result_id

Der Bezeichner des Ergebnisses, das ausgewertet wird. Dieses Ergebnis stammt von einem Aufruf von mssql_query().

Rückgabewerte

Gibt ein assoziatives Array mit dem gelieferten Datensatz zurück oder FALSE, wenn es keinen weiteren Datensatz gibt.

Beispiele

Beispiel #1 mssql_fetch_assoc()-Beispiel

<?php
// Eine Select-Anfrage an MSSQL senden
$anfrage mssql_query('SELECT [username], [name] FROM [php].[dbo].[userlist]');

// Überprüfen, ob es Datensätze gab
if (!mssql_num_rows($anfrage)) {
    echo 
'Keine Datensätze gefunden';
} else {
    
// Eine Liste der Benutzer in folgendem Format ausgeben:
    // * name (username)

    
echo '<ul>';

    while (
$datensatz mssql_fetch_assoc($anfrage)) {
        echo 
'<li>' $datensatz['name'] . ' (' $datensatz['username'] . ')</li>';
    }

    echo 
'</ul>';
}

// Den Ergebnisspeicher freigeben
mssql_free_result($anfrage);
?>


8 BenutzerBeiträge:
- Beiträge aktualisieren...
simonh at dreameyemedia dot com
28.12.2008 11:35
With the code mentioned above and in relation to a MSSQL query.

When using the following statements  to retrieve dat "fetch_assoc" then running a split statement PHP it then into a array it will not pass the data retrieved will show correctly in a XML document created by the DOM object.

<?php
require("phpsqlajax_dbinfo.php");

// Start XML file, create parent node

$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

// Opens a connection to a DreameyeOne server
$connection=mssql_connect ("ELF\ELF", $username, $password);
if (!
$connection) {
  die(
'Not connected : ' . mssql_error());
}

// Set the active DreameyOne database
$db_selected = mssql_select_db($database, $connection);
if (!
$db_selected) {
  die (
'Can\'t use db : ' . mssql_error());
}

// Select all the rows in the markers table
$query = "SELECT msg FROM ozekimessagein ORDER BY senttime DESC";
$result = mssql_query($query);
if (!
$result) {
  die(
'Invalid query: ' . mssql_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = @mssql_fetch_assoc($result)) {
      list (
$vehicleID,$long,$lat) = split(',',$row);
// ADD TO XML DOCUMENT NODE
     
$node = $dom->createElement("marker");
     
$node = $dom->createElement("marker");
     
$newnode = $parnode->appendChild($node);
     
$newnode->setAttribute("VehicleID:",$vehicleID);
     
$newnode->setAttribute("lat",$lat);
     
$newnode->setAttribute("long",$long);
}

echo
$dom->saveXML();

?>

Merlinman
pegas1981
22.05.2007 13:41
[quote]
WARNING:

PHP 4.3.4 introduced a BUG.

if the DB has: '' (empty string)
this function will return: ' ' (string with 1 space)

see: http://bugs.php.net/bug.php?id=26315
[/quote]
the same version PHP 5.2.0

25.03.2007 7:25
regarding 'The 30 character field name limit is still an issue'

Thought about using shorter column names and using the description to give more info regarding the column.

as to the
$result = mssql_query('SELECT articles.id, issues.id FROM articles, issues WHERE articles.pub_date = issues.pub_date');

try this
$result = mssql_query('SELECT articles.id as aid, issues.id as iid FROM articles, issues WHERE articles.pub_date = issues.pub_date');
jbarker at erepublic dot com
24.01.2007 18:35
Beware when using mssql_fetch_assoc() to return data pulled in from joined tables; if the tables have the same column names, the column mentioned last will overwrite the prior one when populating the assoc.

Example:

<?
$result
= mssql_query('SELECT articles.id, issues.id FROM articles, issues WHERE articles.pub_date = issues.pub_date');
$record = mssql_fetch_assoc($result);
print_r($record);

/*
Prints:

Array
(
    [id] => 123
)

*/
?>
brandonkirsch at uses dot gmail dot domain
11.12.2006 19:43
We are running FreeTDS on a Linux Apache / PHP server and encountered the following error when calling mssql_fetch_assoc()

Fatal error: Call to undefined function mssql_fetch_assoc()

We were confused because *some* of the mssql commands worked, such as mssql_connect and mssql_fetch_array.  The trick to make mssql_fetch_assoc() work lies in configuring FreeTDS.  We had to change the statement to contain the --enable-msdblib switch:

./configure --enable-msdblib

We then recompiled PHP to include --with-mssql=/path/to/freetds and finally mssql_fetch_assoc() began working properly.
ben at thelocust dot org
3.11.2006 21:22
The 30 character field name limit is still an issue as of PHP 5.1.6, at least running under IIS.
electricsurfer.com
2.12.2003 5:38
WARNING:

PHP 4.3.4 introduced a BUG.

if the DB has: '' (empty string)
this function will return: ' ' (string with 1 space)

see: http://bugs.php.net/bug.php?id=26315
php at electricsurfer dot com
3.06.2003 20:34
WARNING: as of PHP 4.3.2

The keys in the array will only contain the 1st 30 characters of the column name if you have column names longer than that.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)