/    Sign up×
Community /Pin to ProfileBookmark

mysql/php help, please

i’m trin to open one table to grab the a value to specify in the second table. then display a name from the first table(and repeat for each) then use a while to display certain records from the second table. Here’s my code cuz that probably confused you.

[code=php] $query = “Select * from category”;
$result = mysql_query($query, $dbconn);
while (mysql_fetch_assoc($result)) {
$catid = $cat[“id”];
$catname = $cat[“name”];
}
$catarr = array($catname);
$query2 = “Select * from forum where CID = “. $catid .””;
$result2 = mysql_query($query2, $dbconn);
foreach($catarr as $catey) {
echo “<table>”;
echo “<tr><td colspan=”4″>$catey</td></tr>”;
echo “<tr><td>Forum</td><td>Total Topics</td><td>Total Posts</td><td>Last Update</td></tr>”;
while($row = mysql_fetch_object($result2)){
$name = $row->name;
$descripe = $row->description;
$threads = $row->t_count;
$posts = $row->p_count;
$updated = $row->updated;
$updater = $row->updater;
echo “<tr><td>$name<br>$descripe</td>”;
echo “<td>$threads</td>”;
echo “<td>$posts</td>”;
echo “<td>$updated<br>By:$updater</td></tr>”;
}
echo “<tr><td colspan=”4″>&nbsp;</td></tr>”;
echo “</table>”;
}
[/code]

the problem is the variables are only grabbing the top record(or highest, however u look at it)

P.S. Yes this is another forum, well start. I’m wanting to make one to learn php/mysql better, cuz a forum uses tons of it. I’ve already got a forum built in asp, so the goals not to get one built, but to learn.

TThanks in advanced

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@shimonJun 14.2004 — Here's your problem:[code=php]$catarr = array($catname);[/code]and then you seem to trying to loop through that by doing:[code=php]foreach($catarr as $catey)[/code]and it's only looping once, because $catname is just one word...try thinking like this:

[code=php]// grab all categories
$query = "SELECT * FROM category;";
$result = mysql_query($query, $dbconn);

// loop through each category
while ($cat = mysql_fetch_assoc($result)) {
$catid = $cat['id'];
$catname = $cat['name'];

// display table header
echo "<table>n";
echo " <tr>n";
echo " <td colspan="4">".$catname."</td>n";
echo " </tr>";
echo " <tr>n";
echo " <td>Forum</td>n";
echo " <td>Total Topics</td>n";
echo " <td>Total Posts</td>n";
echo " <td>Last Update</td>n;";
echo " </tr>n";

// get all forums for that category
$query2 = "SELECT * FROM forum";
$query2 .= " WHERE CID='".$catid."'";
$query2 .= ";";

$result2 = mysql_query($query2, $dbconn);
while($row = mysql_fetch_object($result2)){
$name = $row->name;
$descripe = $row->description;
$threads = $row->t_count;
$posts = $row->p_count;
$updated = $row->updated;
$updater = $row->updater;

// display forum details
echo " <tr>n";
echo " <td>".$name."<br />".$descripe."</td>n";
echo " <td>".$threads."</td>n";
echo " <td>".$posts."</td>";
echo " <td>".$updated."<br />By:".$updater."</td>n";
echo " </tr>n";
}

// now just finish off our table
echo " <tr>n";
echo " <td colspan="4">&nbsp;</td>n";
echo " </tr>n";
echo "</table>n";
}[/code]
Problem was you weren't looping through your mysql resultset at all.

Btw, I would also question why you use both mysql_fetch_assoc() and mysql_fetch_object() - it would be more 'normal' to use one or the other consistently, but it's your script ?
Copy linkTweet thisAlerts:
@GenixdeaeauthorJun 14.2004 — thanks...i didnt put everything within the first while because i thought it would jus keep repeating to open the select statement for every record it found....

also i'm not even sure why i'm using both...i'll choose one, eventually. what one do you guys suggest? i know of three, assoc, object, array...i'm not sure which is "better" or if they are all the same...

Thanks shimon
Copy linkTweet thisAlerts:
@shimonJun 15.2004 — I would personally never use mysql_fetch_array without a good reason - it (by default) eats double the memory of fetch_assoc. Having said that, I'm sure it's not enough to be worth worrying about, still it seems more disciplined to use mysql_fetch_array

As for mysql_fetch_object, I tried it but I didn't like it ? I work a lot with objects, but this function doesn't really return a 'real' object and so I just find the 'object-like' syntax a clumsy and potentially misleading.

But that's just a personal take - you can use whatever you like, just read through the relevant sections of the manual and see which you prefer ?
×

Success!

Help @Genixdeae spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...