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

Struggling!!!

Status
Not open for further replies.

Geee

Programmer
Joined
Apr 23, 2001
Messages
253
Location
GB
I'm really new at this after trying to move from ASP, can anyone help me. I'm reading from the database but keep getting stuck in an infinte loop or displaying nothing at all. I suspect the line:

while($rs = mysql_fetch_assoc($result)){

is to blame but don't know enough to be sure, the full code is:

<?php
// whatever your database is here, im assuming its an access db or something??
$db = mysql_connect(&quot;localhost&quot;, &quot;plaz1_plaz&quot;, &quot;mouse25&quot;);
// select database
mysql_select_db(&quot;plaz1_forums&quot;,$db);
// set SQL string
$sql = &quot;SELECT DISTINCT (phpbb_posts.topic_id) AS topics FROM phpbb_posts INNER JOIN phpbb_posts_text ON phpbb_posts.post_id = phpbb_posts_text.post_id WHERE (((phpbb_posts.forum_id)=3))ORDER BY phpbb_posts.topic_id DESC LIMIT 7&quot;;
// put the recordset into a variable.
$result = mysql_query($sql ,$db) or die (mysql_error());
$rs = mysql_fetch_assoc($result);
// start output of records
while($rs = mysql_fetch_assoc($result)){
?>
<b></b>
<table width=&quot;550&quot; border=&quot;1&quot; cellspacing=&quot;2&quot; cellpadding=&quot;4&quot; bgcolor=&quot;#333333&quot; bordercolor=&quot;#333333&quot; align=&quot;center&quot;>
<tr>

<td bgcolor=&quot;#222233&quot; height=&quot;10&quot; bordercolor=&quot;#000000&quot;>
<p><b>
<?php
// construct 2nd SQL query
$sql2 = &quot;SELECT phpbb_users.username, phpbb_posts.post_time, phpbb_posts_text.post_subject, phpbb_posts_text.post_text, phpbb_posts.topic_id &quot;;
$sql2 = $sql2 . &quot;FROM (phpbb_posts_text INNER JOIN phpbb_posts ON phpbb_posts_text.post_id = phpbb_posts.post_id) INNER JOIN phpbb_users ON phpbb_posts.poster_id = phpbb_users.user_id &quot;;
$sql2 = $sql2 . &quot;WHERE (((phpbb_posts.topic_id)=&quot; . $rs[&quot;topics&quot;] . &quot;))&quot; ;
// put result into a variable
$result2 = mysql_query($sql2 ,$db) or die (mysql_error());
$rs2 = mysql_fetch_assoc($result2);
// print field from result set
echo $rs2[&quot;post_subject&quot;];
?>
</b></p>
</td>
</tr>
<tr>

<td bgcolor=&quot;#666666&quot; height=&quot;18&quot; bordercolor=&quot;#000000&quot;>
<p align='right'>
<?php
// think this should show your date in 01-Jan-2003 format?
echo &quot;Added on &quot; . date(&quot;d-M-Y&quot;, $rs2[&quot;post_time&quot;]) . &quot; by &quot; . $rs2[&quot;username&quot;];
?></p>
<p align='justify'>
<?php
// this bit might not work, not sure how the VBCrLF should be handled
echo str_replace(&quot;VBCrLF&quot;, &quot;<BR>&quot;, $rs2[&quot;post_text&quot;]);
?>
</p>
<?php
// put topic id into a variable? is this needed?
$topicid = $rs2[&quot;topic_id&quot;];
// set third SQL statement
$sql3 = &quot;Select COUNT(post_id) as comments from phpbb_posts where topic_id =&quot; . $rs[&quot;topics&quot;];
// put result of third query into variable
$result3 = mysql_query($sql3 ,$db) or die (mysql_error());
$rs3 = mysql_fetch_assoc($result3);
// print link to page (doesnt have an end </a> tag btw)
echo &quot;<p align='right'><b><a href=' . $topicid . &quot;' target='_parent'>&quot; . intval($rs3[&quot;comments&quot;]-1) . &quot; comments...&quot;;
?>
</td>
</tr>
</table>
<br>


<?php
// end of 1st record set output loop
}
?>




-GTM Consult, Home of USITE-
-=
 
i have never used ur method of reading records this is what i do:

$rs=mysql_query($sql)
for($i=0;$i<mysql_num_rows($rs);$i++)
{
$row=msql_fetch_row($rs);
echo $row[0]; //0 is for firts column in the query and so on...
}



Known is handfull, Unknown is worldfull
 
Thanks for the quick response, but I still cant fit that in anywhere and get it to work. Any chance you could show me how to modify my code?

-Geeeeeeeeeeeeeeeeeeeeeeee-
-=
 
the problem is here
Code:
$rs = mysql_fetch_assoc($result);
// start output of records
while($rs = mysql_fetch_assoc($result)){

replace it with only while statment
ie
Code:
while($rs = mysql_fetch_assoc($result)){
....



--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
MySQL queries and record retrieval seems to be a common problem.

Four steps (you get most right):

1. Connect to server:
Code:
 $dbLink = mysql_connect($host,$user,$pass);

2. Select database:
Code:
 mysql_select_db($myDb,$dbLink);

3. Perform query:
Code:
$result = mysql_query($SQL,$dbLink);

That's the step where people get mixed up. The result is just a resource identifier not the records themselves.

4. Retrieve records:
Code:
 while ($row = mysql_fetch_asoc($result)){
   # do whatever you want in here
}

The while loop is perfectly fine. vbkris prefers accessing each row by incrementing a counter, thats fine too.

Biggest mistake however, is to do all this without error checking. What happens if the connection fails? Can't select the database? Have a SQL error in the query string?

for steps 1,2,3 there should be added:
Code:
 OR die (mysql_error());
or a similar mechanism.

Also, you could check if there are any rows with mysql_num_rows($result);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top