Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loading an array

Status
Not open for further replies.

thumbelina

Programmer
Jun 4, 2001
58
CA
<?php $crap = mysql_connect(&quot;localhost&quot;,&quot;username&quot;,&quot;password&quot;);
mysql_select_db(&quot;database&quot;,$crap);
$query = &quot;SELECT coloum FROM table&quot;;
$result = mysql_query($query, $crap);
while($row = mysql_fetch_array($result)){
print($row[&quot;name&quot;]);
}?>
ok this is what the book said to do it should take the info out of the table put it in an array and just print out the results but it doesn't seem to work. my problem i think is that i don't know what the mean but [&quot;name&quot;] in the last line. anythoughts?
 
ok never mind i got it to work. it was the &quot;name&quot; part that was the problem. i put in the coloum name from my table, in case you were wondering.
 
Thumbelina,

Just thought I would throw my 2 cents in here.

The following can be changed for streamlining and more efficiency

from:

<?php
$crap = mysql_connect(&quot;localhost&quot;,&quot;username&quot;,&quot;password&quot;);
@mysql_select_db(&quot;database&quot;,$crap);

$query = &quot;SELECT coloum FROM table&quot;;
$result = mysql_query($query, $crap);

while($row = mysql_fetch_array($result)){
print($row[&quot;name&quot;]);
}
?>

to:

<?php
$crap = mysql_pconnect(&quot;localhost&quot;,&quot;username&quot;,&quot;password&quot;);
@mysql_select_db(&quot;database&quot;,$crap) or die(mysql_error());

$query = &quot;SELECT coloum FROM table&quot;;
$result = mysql_query($query) or die(mysql_error().&quot;<br>&quot;.$query);

while($row = mysql_fetch_row($result)){
echo $row[0];
}
?>

Here is what I did:

** I used @ when selecting database to suppress errors so I can use my own.
** I added 'or die' statements to spit out the mysql error and anything else I want to display for debug/notification purposes (you can use custom functions here to send you an email, etc).
** I changed mysql_fetch_array to mysql_fetch_row since you are only retrieving data from one column. Since you already know the name of the column and it is the only one you are retrieving, you can do this without having to remember which column is which in the array.

I changed print($row[&quot;name&quot;]); to echo $row[0];
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top