SELECTING DATA IN PHP

You frequently select data in a MySQL table using PHP. Selecting data through a PHP program
using a MySQL command takes four steps:
1. Make a connection to the database.
2. Create a safe query with the command.
3. Run the query.
4. Read the results.
The following code makes the connection and creates a safe query. Rather than just running the
query, use it as the right side of an assignment. This creates a mysqli_result object that you use to
read the results via methods in the result object. This example takes each row, one at a time,
and puts it into an associative array.


<?php
define(“MYSQLUSER”, “php24sql”);
define(“MYSQLPASS”, “hJQV8RTe5t”);
define(“HOSTNAME”, “localhost”);
define(“MYSQLDB”, “test”);
// Make connection to database
$connection = @new mysqli(HOSTNAME, MYSQLUSER, MYSQLPASS, MYSQLDB);
if ($connection->connect_error) {
die(‘Connect Error: ‘ . $connection->connect_error);
} else {
echo ‘Successful connection to MySQL <br />’;
// Set up the query
$query = “SELECT * FROM ‘table1‘ “
. “ WHERE ‘code‘ = 15”
. “ ORDER BY ‘description‘ ASC “
;
// Run the query
$result_obj = ‘’;
$result_obj = $connection->query($query);
// Read the results
// loop through the result object, row by row
// reading each row into an associative array
while($result = $result_obj->fetch_array(MYSQLI_ASSOC)) {
// display the array
print_r($result);
echo ‘<br />’;
}
}

// Read the results
// loop through the results, row by row
// reading each row into an associative array
while($result = $result_obj->fetch_array(MYSQLI_ASSOC)) {
// collect the array
$item[] = $result;
}
// print array when done
echo ‘<pre>’;
print_r($item);
echo ‘</pre>’;


The example prints out the array









fetch_array(MYSQLI_ASSOC): This returns an associative array. Loop through to get all the
rows. Same as fetch_assoc().
‰ fetch_array(MYSQLI_NUM): This returns a numeric array. Loop through to get all the rows.
Same as fetch_row().
‰ fetch_array(MYSQLI_BOTH): This returns both an associative array and a numeric array
with the same data. Loop through to get all the rows. This is the default if no type is
specifi ed.
‰ fetch_all(MYSQLI_ASSOC): This returns all the rows as an associative array.
‰ fetch_all(MYSQLI_NUM): This returns all the rows as a numeric array.
‰ fetch_all(MYSQLI_BOTH): This returns all the rows both as an associative array and a
numeric array with the same data.
‰ fetch_object($class_name): This returns an object of the row. Loop through to get all
the rows. If you give it a class name, it uses that class to create the object. If there is no class
name it will create a stdClass object, which is a predefi ned class.
Related Posts:
  • Advanced SQL 15.1 Exploring with SHOW The SHOW command is useful for exploring the details of databases, tables, indexes, and MySQL. It's a handy tool when you're writing new queries, modifying database structure, creating repor… Read More
  • XML Extensions in PHP 5 SimpleXML A new PHP 5-only extension that excels at parsing RSS files, REST results, and configuration data. If you know the document's format ahead of time, SimpleXML is the way to go. However, SimpleXML supports o… Read More
  • Convert CSV to PHP Every once in a while, I have a static list of values that I don't want to put into a database, but that I do want to use in my PHP application. That static data can come from a variety of sources, but often it's in a s… Read More
  • Create Dynamic Database Access Objects Some simple PHP that makes for a surprisingly robust script <?php require_once( "DB.php" ); $dsn = 'mysql://root:password@localhost/books'; $db =& DB::Connect( $dsn, array() ); if (PEAR::isError($db)) { die($db->g… Read More
  • PHP Functions A function is a named sequence of code statements that can optionally accept parameters and return a value. A function call is an expression that has a value; its value is the returned value from the function. PHP provid… Read More
  • Declaring a Class To design your program or code library in an object-oriented fashion, you'll need to define your own classes, using the class keyword. A class definition includes the class name and the properties and methods of the clas… Read More
  • PHP Web-Related Variables PHP automatically creates variables for all the data it receives in an HTTP request. This can include GET data, POST data, cookie data, and environment variables. The variables are either in PHP's global symbol table or … Read More
  • mysqli Fetch Methods If you prefer different MySQL fetch methods, they're also in mysqli. Given the same query of SELECT username FROM users, these example functions all print the same results: // Fetch numeric arrays: while ($row = mysqli_… Read More
  • Configuring the Connection Mysql <?phptry{$pdo = new PDO('mysql:host=localhost;dbname=ijdb', 'ijdbuser','mypassword');}catch (PDOException $e){$output = 'Unable to connect to the database server.';include 'output.html.php';exit();} $pdo->setAttribute(… Read More
  • MySQL Data on the Web Before we leap forward, it’s worth taking a step back for a clear picture of our ultimategoal. We have two powerful tools at our disposal: the PHP scripting languageand the MySQL database engine. It’s important to understand… Read More
  • PHP Data Types PHP provides four primitive data types: integers, floating point numbers, strings, and booleans. In addition, there are two compound data types: arrays and objects.  Integers Integers are whole numbers. The range… Read More
  • Using mysql_fetch_array fanc we accessed attributes in order using the foreach loop statement. In many cases, you'll also want to access the attributes in another way, and this is usually best achieved by using the attribute names themselves. It's muc… Read More
  • php and pdf  Documents and Pages A PDF document is made up of a number of pages. Each page contains text and/or images.Put text onto  the pages, and send the pages back to the browser when you're done.    … Read More
  • MySQL Database Using PHP <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  <title>Wines</title>  </head> <body> <pre> <?php  // (1) Open the… Read More
  • Using Cookie Authentication PHP Store authentication status in a cookie or as part of a session. When a user logs insuccessfully, put their username in a cookie. Also include a hash of the username and a secretword so a user can't just make up an authentic… Read More