11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 30 • USING <strong>PHP</strong> WITH MYSQLwww.it-ebooks.info• MYSQLI_BOTH: Returns the row as both an associative and a numerically indexedarray. Therefore, each field could be referred to in terms of its index offset and itsfield name. This is the default.For example, suppose you only want to retrieve a result set using associative indices:$query = 'SELECT sku, name FROM products ORDER BY name';$result = $mysqli->query($query);while ($row = $result->fetch_array(MYSQLI_ASSOC)){$name = $row['name'];$sku = $row['sku'];echo "Product: $name ($sku) ";}If you wanted to retrieve a result set solely by numerical indices, you would make the followingmodifications to the example:$query = 'SELECT sku, name, price FROM products ORDER BY name';$result = $mysqli->query($query);while ($row = $result->fetch_array(MYSQLI_NUM)){$sku = $row[0];$name = $row[1];$price = $row[2];printf("(%s) %s: %d ", $sku, $name, $price);}Assuming the same data is involved, the output of both of the preceding examples is identical tothat provided for the example in the query() introduction.Determining the Rows Selected and Rows AffectedYou’ll often want to be able to determine the number of rows returned by a SELECT query or the numberof rows affected by an INSERT, UPDATE, or DELETE query. Two methods, introduced in this section, areavailable for doing just this.Determining the Number of Rows ReturnedThe num_rows() method is useful when you want to learn how many rows have been returned from aSELECT query statement. Its prototype follows:class mysqli_result {int num_rows}For example:598

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!