Re: fetch items from a row [message #182452 is a reply to message #181287] |
Mon, 05 August 2013 00:19 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma:
|
Senior Member |
|
|
On Fri, 03 May 2013 15:55:58 -0400, richard wrote:
> How would I change the '42' value to a string value based upon the value
> retrieved from using $_GET('number')?
$_GET isn't a function, it's an associative array.
If you're trying to read the "number" parameter of a get request, you use:
$_GET['number'];
or:
$_GET["number"];
The type of brackets is very important, although in this specific case,
the type of quote marks is less important than it is at other times.
> <?php $result = mysql_query("SELECT id,email FROM people WHERE id =
> '42'"); if (!$result) {
> echo 'Could not run query: ' . mysql_error();
> exit;
> }
> $row = mysql_fetch_row($result);
>
> echo $row[0]; // 42 echo $row[1]; // the email value ?>
If you only want the email, only request the email.
If you want to output the email address for a specific id number that
comes from a get request, which I think is what you mean:
<?php
$num = 0;
$if isset( $_GET["number"] ) $num = intval( $_GET["number"] );
$sql = "SELECT email FROM people WHERE id = '{$num}'";
$result = mysql_query( sql );
if ( !$result ) {
echo "mysql query \"{$sql}\" failed with: " . mysql_error() . "\n";
} else {
$rows = mysql_num_rows( $result );
if ( $rows != 1 ) {
echo "Unexpected result from sql query \"{$sql}\", {$rows} rows
returned when 1 row expected!\n";
} else {
$row = mysql_fetch_row($result);
if ( $row ) {
echo "Email address is: {$result[0]}\n";
} else {
echo "No email address found for that ID number\n";
}
}
}
--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
|
|
|