Tedit kap wrote" "On 4/12/2008 1:22 PM":"
> 
> 
> I copied this from somewhere as I want to connect to mysql using php for 
> the first time but when I execute the program below there is only blank 
> screen (I already inserted a test row to my table.)
> 
> 1-In the program below what does $yourfield refer to?
> 
> 2-Isn't this supposed to display the contents of my table? It is not 
> displaying anything.
> 
> Can someone help? Thanks
> 
> <?php
> //Connect To Database
> $hostname='. ......... ....';
> $username='. ......... ....';
> $password='. ......... .......';
> $dbname='... ........' ;
> $usertable=' ......... ....';
> $yourfield = '........... ..';
> mysql_connect( $hostname, $username, $password) OR DIE ('Unable to 
> connect to database! Please try again later.');
> mysql_select_ db($dbname) ;
> $query = 'SELECT * FROM $usertable';
> $result = mysql_query( $query);
> if($result) {
> while($row = mysql_fetch_ array($result) ){
> $name = $row['$yourfield' ];
> echo 'Name: '.$name;
> }
> }
> ?>

mysql_fetch_array returns an array keyed by both field names as well as 
numerical.
For instance, if your database looked like this:
id|name|address
mysql_fetch_array would return something like this:
array(
0 => 12345,
id => 12345,
1 => John Smith,
name => John Smith,
2 => 123 Anyplace Street
address => 123 Anyplace Street
)

So, if you assign $yourfield = 'name', the line of code:
$name = $row[$yourfield] would result in an an echo of "John Smith".

FYI, you also have a bug that will keep you from this result. Variable 
expansion only works in double quotes. You would need $row["$yourfield"] 
or $row[$yourfield].

bp

Reply via email to