Forum Moderators: coopster

Message Too Old, No Replies

Problem with printing an array using while

         

pomalley

7:41 pm on Feb 15, 2007 (gmt 0)

10+ Year Member



Hi,

I am a complete newbie to PHP and am currently trying to get to grips with the language. I have a database of music tracks and am trying to create a search function for them. So far I have the following code:

$sql = "SELECT * FROM `track_info`";

if($trackId!="" ¦¦ $trackName!="" ¦¦ $albumName!="" ¦¦ $artistName!="" ¦¦ $genre!="")
{
$sql.=" WHERE";

if($trackId!="")
{
$addAnd = "true";
$sql.= " `trackid`='$trackId'";
}
if($trackName!="")
{
if($addAnd == "true")
{
$sql.= " AND";
}
$sql.= " `trackname`='$trackName'";
$addAnd = "true";
}
if($artistName!="")
{
if($addAnd == "true")
{
$sql.= " AND";
}
$sql.= " `artistname`='$artistName'";
$addAnd = "true";
}
if($albumName!="")
{
if($addAnd == "true")
{
$sql.= " AND";
}
$sql.= " `albumname`='$albumName'";
$addAnd = "true";
}
if($genre!="")
{
if($addAnd == "true")
{
$sql.= " AND";
}
$sql.=" `genre`=$'genre'";
$addAnd = "true";
}
}
$result = mysql_query($sql);
while($row = mysql_fetch_array($result,MYSQL_ASSOC));
{
print_r($row);
}

The problem is that nothing from the array is getting printed out. I have taken the sql statement that is built and executed it in ems mysql and the correct rows were selected so I know the sql is fine.

Also if I take out the while loop the first row from the array is printed as expected. It just seems to be when I include the while loop that nothing at all is printed out. Would anyone have any ideas to what the problem might be?

Many Thanks

[edited by: jatar_k at 8:45 pm (utc) on Feb. 15, 2007]
[edit reason] fixed formatting [/edit]

whoisgregg

8:24 pm on Feb 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have a semicolon in your while declaration that shouldn't be there... I didn't read through the rest of the code, but I suspect the semicolon is the problem. :)

while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
print_r($row);
}

pomalley

8:51 pm on Feb 15, 2007 (gmt 0)

10+ Year Member



Thank you whoisgregg, that has sorted the problem.

I don't know how many times I have looked at the code and managed to miss that semicolon! :)

eelixduppy

8:52 pm on Feb 15, 2007 (gmt 0)



Yes, that is a difficult problem to spot sometimes.

By the way, Welcome to WebmasterWorld, pomalley! :)