can't figure this out

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stuffradio
Forum Newbie
Posts: 14
Joined: Tue Jan 03, 2006 2:33 am

can't figure this out

Post by stuffradio »

I am trying to make a code that displays items from the database that is linked from one table to the other. My schema is:

Table: categories
structure:
cid int(10)
name varchar(20)

Table:station
structure:
sid int(12)
cid int(12)
mname varchar(25)
sname varchar(30)

code for station.php

Code: Select all

<?php
include("config.php");
$mname= $_POST['mname'];
$cat = mysql_query("SELECT * FROM categories");
$category = mysql_fetch_array($cat);

	echo "<table width=400 height=75 border=1 bordercolor=black cellpadding=0 cellspacing=0>
	<tr><td><center>Station Name</td><td><center>Mercora Name</td></tr>";
if ($_GET['$c'] == $category[cid]) {
$stat = mysql_query("SELECT * FROM station WHERE $category[cid]='$cid'");
while($station = mysql_fetch_array($stat)) {
extract($station);
	echo ("<tr><td><center><a href=profile.php?view=$sid>$sname</a></td><td><center>$mname</td></tr>");
}
}
echo "</table>";
exit;
?>
To see the webpage go to http://www.stufftorrents.be/mradio . Hope you can help,

Thanks
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Code: Select all

SELECT cid, sid, sname, mname
FROM station
INNER JOIN categories USING cid
ORDER BY cid, sid

Code: Select all

$prev_cid = "";
while ($row = mysql_fetch_assoc($result)) {
  // test if new category is started
  if ($row['cid'] != $prev_cid) {
    // eg: ould output a </tr><tr> line to start a new table row
  }

  // display station
  // echo stuff with $row['sname'] etc...

  // update the previous cid
  $prev_cid = $row['cid'];
}
stuffradio
Forum Newbie
Posts: 14
Joined: Tue Jan 03, 2006 2:33 am

Post by stuffradio »

This is what I have now:

Code: Select all

<?php
include("config.php");
$mname= $_POST['mname'];
$sname= $_POST['sname'];
$cat = mysql_query("SELECT cid, sid, sname, mname FROM station INNER JOIN categories USING cid ORDER BY cid, sid");
$category = mysql_fetch_array($cat);

	echo "<table width=400 height=75 border=1 bordercolor=black cellpadding=0 cellspacing=0>
	<tr><td><center>Station Name</td><td><center>Mercora Name</td></tr>";
$prev_cid = "";
$stat = mysql_query("SELECT * FROM station WHERE cid='$category[cid]'");
while($station = mysql_fetch_aassoc($stat)) {
if ($station['cid'] != $prev_cid) {
	echo ("<tr><td><center><a href=profile.php?view=$station[sid]>$station[sname]</a></td><td><center>$station[mname]</td></tr>");
}
$prev_cid = $station['cid'];
}
echo "</table>";
exit;
?>
This is what shows up on the website:

Code: Select all

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/carl/public_html/mradio/station.php on line 6
Station Name Mercora Name 

Fatal error: Call to undefined function: mysql_fetch_aassoc() in /home/carl/public_html/mradio/station.php on line 12
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Well, it's obvious that the fatal error is because you spelled mysql_fetch_assoc()wrong. Simple mistake.

The other message is telling you $cat is not a valid resource - likely because of something wrong with your query. Copy and paste your query into the command line and see what kind of error MySQL gives you.

Also, using [syntax=php][/syntax] tags colourizes your PHP code a lot nicer that the [syntax=php][/syntax] tags.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
stuffradio
Forum Newbie
Posts: 14
Joined: Tue Jan 03, 2006 2:33 am

Post by stuffradio »

This is what mysql says when I copy the query in:

Code: Select all

#1064 - You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql_query("SELECT cid, sid, sname, mname FROM station INNER J
Query:

Code: Select all

mysql_query("SELECT cid, sid, sname, mname FROM station INNER JOIN categories USING cid ORDER BY cid, sid");
sheila
Forum Commoner
Posts: 98
Joined: Mon Sep 05, 2005 9:52 pm
Location: Texas

Post by sheila »

Looks like you to sent PHP code to MySQL. The query is just this part

Code: Select all

SELECT cid, sid, sname, mname FROM station INNER JOIN categories USING cid ORDER BY cid, sid
What does it say about that?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

needs to be:

LEFT INNER JOIN x USING ( y)
Post Reply