Forum Moderators: coopster

Message Too Old, No Replies

Strange thing in my script

         

Mohamed

12:39 pm on Nov 22, 2006 (gmt 0)

10+ Year Member



I am getting strange thing from my script. the strange thing is I am getting same result for two different functions and that is not what I intended to get. the two functions work when I am not using classes and objects, but they do not work when I am using classes.

$temp = new db();
$temp->select_db($dbname);

in this part what I want to get is a list of available artist and it works well.

$list = $temp->browse_by_letter($table, $letter);

in this section what I want to get is details of specific artist, but it gives me same result as the above function.

$details = $temp->get_artist($table, $artist_id);

the two functions.

function browse_by_letter($table, $leter)
{
$res = mysql_query("select artist, MIN(id) as id from $table where artist like '$leter%' group by artist order by artist asc");
while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
extract($row);
$artist = $row['artist'];
$id = $row['id'];
$res1 = mysql_query("select count(song) as count from $table where artist=\"$artist\"");
while ($row1 = mysql_fetch_array($res1, MYSQL_ASSOC)) {
extract($row1);
} //while
$res2 = mysql_query("select song, viewed from $table where artist=\"$artist\" order by viewed asc");
while ($row2 = mysql_fetch_array($res2, MYSQL_ASSOC)) {
extract($row2);
$song = $row2['song'];
$viewed = $row2['viewed'];
} //while
$this->result[$id][$artist][$count][$song] = $viewed;
} //while
return $this->result;
}


function get_artist($table, $artist_id)
{
$artist_tmp = $this->get_artist_name($table, $artist_id);
$res = mysql_query("select artist, id,song from $table where artist=\"$artist_tmp\"");
while ($row = mysql_fetch_array($res)) {
$artist = $row['artist'];
$song = $row['song'];
$id = $row['id'];
$this->result[$artist][$id] = $song;
} //while
return $this->result;
}
function get_artist_name($table, $artist_id)
{
$res = mysql_query("select artist from $table where id='$artist_id'");
while($row = mysql_fetch_array($res, MYSQL_ASSOC))
$this->artistname = $row['artist'];
return $this->artistname;
}

mcibor

9:17 pm on Nov 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know if you know, but

$res = mysql_query("select artist, MIN(id) as id from $table where artist like '$leter%' group by artist order by artist asc");

will return just one id - the smallest one.

Instead I would run this query:

$res = mysql_query("select artist, id from $table where artist like '$leter%' group by artist"); //group already orders the rows and order by is omitted.

Also in the second you can use one query:

SELECT a.artist, s.id, s.song from artist a, song s WHERE s.artist=a.name AND a.id='$artist_id';

And I would store in table song artist as ID not as name.

Then

SELECT a.artist, s.id, s.song from artist a, song s WHERE s.artist=a.id AND a.id='$artist_id';

Regards
Michal