Build Your Own DatabaseDriven Website Using PHP amp;amp; MySQL [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Build Your Own DatabaseDriven Website Using PHP amp;amp; MySQL [Electronic resources] - نسخه متنی

Kevin Yank

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید








Handling SELECT Result Sets

For most SQL queries, the mysql_query function returns either true (success) or
false (failure). For SELECT queries this just isn't enough.
You'll recall that SELECT queries are used to view stored
data in the database. In addition to indicating whether the query succeeded
or failed, PHP must also receive the results of the query. As a result, when
it processes a SELECT query, mysql_query returns
a number that identifies a result set, which contains a list of all the rows
(entries) returned from the query. False is still returned if the query fails
for any reason.

$result = @mysql_query('SELECT JokeText FROM Jokes');
if (!$result) {
die('<p>Error performing query: ' . mysql_error() .
'</p>');
}

Provided no error was encountered in processing the query, the above
code will place a result set that contains the text of all the jokes stored
in the Jokes table into the variable $result.
As there's no practical limit on the number of jokes in the database, that
result set can be pretty big.

We mentioned before that the while loop is a useful
control structure for dealing with large amounts of data. Here's an outline
of the code to process the rows in a result set one at a time:

while ( $row = mysql_fetch_array($result) ) {
// process the row...
}

The condition for the while loop probably doesn't
much resemble the conditions you're used to, so let me explain how it works.
Consider the condition as a statement all by itself:

$row = mysql_fetch_array($result);

The mysql_fetch_array function accepts a result
set as a parameter (stored in the $result variable in this
case), and returns the next row in the result set as an array (see "Getting Started with PHP" for a discussion of arrays). When there are no more rows
in the result set, mysql_fetch_array instead returns
false.

Now, the above statement assigns a value to the $row variable,
but at the same time the whole statement itself takes on that same value.
This is what lets you use the statement as a condition in the while loop.
Since a while loop will keep looping until its condition
evaluates to false, this loop will occur as many times as there are rows in
the result set, with $row taking on the value of the next
row each time the loop executes. All that's left is to figure out how to get
the values out of the $row variable each time the loop
runs.

Rows of a result set are represented as associative arrays. The indices
are named after the table columns in the result set. If $row is
a row in our result set, then $row['JokeText'] is the value
in the JokeText column of that row. So
here's what our while loop should look like if we want
to print the text of all the jokes in our database:

while ( $row = mysql_fetch_array($result) ) {
echo('<p>' . $row['JokeText'] . '</p>');
}

To summarize, here's the complete code of a PHP Web page that will connect
to our database, fetch the text of all the jokes in the database, and display
them in HTML paragraphs. The code of this example is available as jokelist.php in
the code archive.

<html>
<head>
<title> Our List of Jokes </title>
</head>
<body>
<?php
// Connect to the database server
$dbcnx = @mysql_connect('localhost', 'root','mypasswd');
if (!$dbcnx) {
die( '<p>Unable to connect to the ' .
'database server at this time.</p>' );
}
// Select the jokes database
if (! @mysql_select_db('jokes') ) {
die( '<p>Unable to locate the joke ' .
'database at this time.</p>' );
}
?>
<p> Here are all the jokes in our database: </p>
<blockquote>
<?php
// Request the text of all the jokes
$result = @mysql_query('SELECT JokeText FROM Jokes');
if (!$result) {
die('<p>Error performing query: ' . mysql_error() .
'</p>');
}
// Display the text of each joke in a paragraph
while ( $row = mysql_fetch_array($result) ) {
echo('<p>' . $row['JokeText'] . '</p>');
}
?>
</blockquote>
</body>
</html>

/ 190