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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying Data from Table

Status
Not open for further replies.

neon0107

Programmer
May 14, 2001
11
US
Hello, I am a newbie with PHP and this is just my 2nd little script. Hope someone can help. I get no results to appear when this page loads. If I access my db through phpmyadmin and run the same query, I get the correct results. Any ideas? Thanks in advance<?php

$connect= mysql_connect(&quot;localhost&quot;,&quot;sungate_imcleod&quot;,&quot;bigjake&quot;) or die(&quot;Could not connect&quot;);

$db =mysql_select_db(&quot;sungate_news&quot;,$connect) or die(&quot;Could not select database&quot;);



$query=&quot;SELECT ID,Title FROM newsitem ORDER BY ID DESC&quot;;

$result=mysql_query($query,$connect);

while($r=mysql_fetch_array($result));
{
$ID=$r[&quot;ID&quot;];
$Title=$r[&quot;Title&quot;];

echo&quot;$ID $Title&quot;;
}

?>


 
It's because the array elements you are referencing don't exist.

mysql_fetch_array() by default returns a numerical array, not an associative array. You can either issue &quot;mysql_fetch_array($result, MYSQL_ASSOC)&quot; or use &quot;mystl_fetch_assoc($result)&quot;. ______________________________________________________________________
TANSTAAFL!
 
Thanks sleipnir214

I had previously tried the numeric array and got nothing, so I was trying anything just to see if I could get values to display. I tried changing it to mysql_fetch_assoc($result) and still no results.

Neon
 
Try using print_r($r) inside your while loop to see what's in $r. ______________________________________________________________________
TANSTAAFL!
 
This confused me for a bit because use mysql_fetch_array() and reference the associative array frequently.

I went and had a look at the function reference and aparently the default behavior of mysql_fetch_array() is to fetch both the numeric and associative, unless specified otherwise.

[smurf]
01101000011000010110010001110011
 
Thanks to hads and sleipnir214 for taking a look at my post. I figured out the problem. Typical for a newbie like me, it was syntax. I have a ; after the While line. It all worked after that. I did change it to a mysql_fetch_row method after I got it to work.

Neon
 
neon01017,

been a while since I have looked at PHP, but do you wish to pull all ID's &Title from database?

Try removing line

$connect= mysql_connect(&quot;localhost&quot;,&quot;sungate_imcleod&quot;,&quot;bigjake&quot;) or die(&quot;Could not connect&quot;);

& replace with

mysql_connect (&quot;localhost&quot;, &quot;your_username&quot;, &quot;your_password&quot;);


mysql_select_db (sungate_news);


$result = mysql_query(&quot;SELECT ID,Title FROM newsitem ORDER BY ID DESC&quot;; ) ;


while ($rows = mysql_fetch_row($result))

{

echo (&quot;<tr><td>$rows[ID]</td><td>$rows[Title]</td><td><a href = 'could_be_a_link.php'>$rows[OTHER_FIELD_NAME_PERHAPS]</a></td></tr>&quot;);
}
?>

Hope this may help
ELW
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top