Member Avatar for doctorphp

Hi everyone.

I am trying to make an if statment that will show text saying create an album if the user has no albums but if they have one or more albums then it will show their albums. Now I have sorted out everything but when I get to the if section of it it shows the two things I described above. I have tried different ways of doing the if function but it just won't work. Here is the code. Any help would be much appreciated

$album_num = mysql_num_rows(mysql_query("SELECT * FROM albums WHERE userid = '" . $id . "'"));

      
if($album_num==0)
{
    echo "You have no albums. <br /> <a href='albums.php'>Create One?</a>";
}
else


echo"<b>Recent Albums</b><br />
<font size='2'>";




echo"<table width='50%'>";
while ($row = mysql_fetch_assoc($album))
{
echo "<font face='arial' size='3'><tr>
        <td>
        <img src='store/".$row['cover']."' width='160' height='100'>
        </td>
        <td>
        <b><a href='viewalbum.php?album=".$row['id']."'>".$row['name']."</a></b>
        
        <br />".$row['description']."<br />
        
        
  </td></tr></font>";
}
echo"</table>";

Try this >

$album_num = mysql_num_rows ( mysql_query ( "SELECT * FROM albums WHERE userid = '" . $id . "'" ) );

if ($album_num == '0') {
	echo "You have no albums. <br /> <a href='albums.php'>Create One?</a>";
} else
{
	
	echo "<b>Recent Albums</b><br /><font size='2'>";

echo "<table width='50%'>";
while ( $row = mysql_fetch_assoc ( $album ) ) {
	echo "<font face='arial' size='3'><tr>
        <td>
        <img src='store/" . $row ['cover'] . "' width='160' height='100'>
        </td>
        <td>
        <b><a href='viewalbum.php?album=" . $row ['id'] . "'>" . $row ['name'] . "</a></b>
        
        <br />" . $row ['description'] . "<br />
        
        
  </td></tr></font>";
}
echo "</table>";
}
Member Avatar for doctorphp

Try this >

$album_num = mysql_num_rows ( mysql_query ( "SELECT * FROM albums WHERE userid = '" . $id . "'" ) );

if ($album_num == '0') {
	echo "You have no albums. <br /> <a href='albums.php'>Create One?</a>";
} else
{
	
	echo "<b>Recent Albums</b><br /><font size='2'>";

echo "<table width='50%'>";
while ( $row = mysql_fetch_assoc ( $album ) ) {
	echo "<font face='arial' size='3'><tr>
        <td>
        <img src='store/" . $row ['cover'] . "' width='160' height='100'>
        </td>
        <td>
        <b><a href='viewalbum.php?album=" . $row ['id'] . "'>" . $row ['name'] . "</a></b>
        
        <br />" . $row ['description'] . "<br />
        
        
  </td></tr></font>";
}
echo "</table>";
}

It doesn't work. All it is doing now is showing the text 'You have no albums' even if the user has created albums

Well, that's a start! no ?
Check this out >

$album_num = mysql_num_rows (mysql_query("SELECT * FROM albums WHERE userid ='$id'"));

if ($album_num == '0') {
	echo "You have no albums. <br /> <a href='albums.php'>Create One?</a>";
} else
{
	
	echo "<b>Recent Albums</b><br /><font size='2'>";

echo "<table width='50%'>";
while ( $row = mysql_fetch_assoc ( $album ) ) {
	echo "<font face='arial' size='3'><tr>
        <td>
        <img src='store/" . $row ['cover'] . "' width='160' height='100'>
        </td>
        <td>
        <b><a href='viewalbum.php?album=" . $row ['id'] . "'>" . $row ['name'] . "</a></b>
        
        <br />" . $row ['description'] . "<br />
        
        
  </td></tr></font>";
}
echo "</table>";
}
Member Avatar for doctorphp

Well, that's a start! no ?
Check this out >

$album_num = mysql_num_rows (mysql_query("SELECT * FROM albums WHERE userid ='$id'"));

if ($album_num == '0') {
	echo "You have no albums. <br /> <a href='albums.php'>Create One?</a>";
} else
{
	
	echo "<b>Recent Albums</b><br /><font size='2'>";

echo "<table width='50%'>";
while ( $row = mysql_fetch_assoc ( $album ) ) {
	echo "<font face='arial' size='3'><tr>
        <td>
        <img src='store/" . $row ['cover'] . "' width='160' height='100'>
        </td>
        <td>
        <b><a href='viewalbum.php?album=" . $row ['id'] . "'>" . $row ['name'] . "</a></b>
        
        <br />" . $row ['description'] . "<br />
        
        
  </td></tr></font>";
}
echo "</table>";
}

No it still doesnt work :(

it should have worked by now ... what does it return ?

you should try to debug it first.

$album_num = mysql_num_rows ( mysql_query ( "SELECT * FROM albums WHERE userid = '" . $id . "'" ) );

echo $album_num . "<--album_num output" // this will give you a hint

i hope this helps. :icon_wink:

<?php

$album_num = mysql_num_rows(mysql_query("SELECT * FROM albums WHERE userid = '" . $id . "'")); // ---> is $id a string data type? 

if($album_num==0)
{
    echo "You have no albums. <br /> <a href='albums.php'>Create One?</a>";
}
else 

echo"<b>Recent Albums</b><br />
<font size='2'>";

echo"<table width='50%'>";
while ($row = mysql_fetch_assoc($album))  // ---> should the while loop inside the else condition ? 
{
echo "<font face='arial' size='3'><tr>
        <td>
        <img src='store/".$row['cover']."' width='160' height='100'>  
        </td>
        <td>
        <b><a href='viewalbum.php?album=".$row['id']."'>".$row['name']."</a></b>
        
        <br />".$row['description']."<br />
        
        
  </td></tr></font>"; 
}
echo"</table>"; // ---> you should also check the double qoute (") inside another double quote (")
								// ---> you may also just use the single quote(') 
								
// ---- i hope this helps ----

// 1. first check

$album_num = mysql_num_rows(mysql_query("select * from albums where userid = '" . $id . "'"));  // ----> or userid = "."$id"." (if string) 
																																														// ---> or userid = ".$id." 
																																														// ---> or userid = '$id'" (if not)
																																														
// ---> you may first check the value of $id by echoing the $album_num like britoniah3480 said
// ---> the problem may start from the query

// 2. second

// ---> this may help 

if($album_num == 0 || $album_num == null) // ---> null value instead
{
    echo "You have no albums. <br /> <a href='albums.php'>Create One?</a>";
}

else 
{
	echo"<b>Recent Albums</b><br />
	<font size='2'>";

	echo"<table width='50%'>";
	while ($row = mysql_fetch_assoc($album))  // ---> while loop inside the else condition 
	{
		echo "<font face='arial' size='3'><tr>
			<td>
			<img src='store/".$row['cover']."' width='160' height='100'>  
			</td>
			<td>
			<b><a href='viewalbum.php?album=".$row['id']."'>".$row['name']."</a></b>
			
			<br />".$row['description']."<br />
			
			</td></tr></font>"; 
	}
	
	echo"</table>"; 
}

// 3. or you may use this query instead

// ---> for the query and if else statement

$myquery = mysql_query("select * from albums where userid = '$id'"); // ---> use the right datatype for the value of $id in getting the equivalent of userid

if (mysql_fetch_object($myquery) == null)
{
	// ---> your statement
	echo "You have no albums. <br /> <a href='albums.php'>Create One?</a>";
}

else
{
	echo "you have existing albums"; // ---> for checking only
	// ---> you may also include again here your query for the existing albums display
	
	// ---> your statement again
	echo"<b>Recent Albums</b><br />
	<font size='2'>";

	echo"<table width='50%'>";
	while ($row = mysql_fetch_assoc($album))  // ---> while loop inside the else condition 
	{
		echo "<font face='arial' size='3'><tr>
			<td>
			<img src='store/".$row['cover']."' width='160' height='100'>  
			</td>
			<td>
			<b><a href='viewalbum.php?album=".$row['id']."'>".$row['name']."</a></b>
			
			<br />".$row['description']."<br />
			
			</td></tr></font>"; 
	}
	
	echo"</table>"; 
	
}


?>

good Luck!

$album_num = mysql_num_rows(mysql_query("SELECT * FROM albums WHERE userid='%s'",$id));

mysql_query takes sql syntax, not php syntax.
not a guaranteed solution I dont work much in sql either

hey, what is actually the datatype of your $id variable?

Member Avatar for doctorphp

hey, what is actually the datatype of your $id variable?

Hi

The id variable is a session. $_SESSION

Member Avatar for doctorphp

Thanks for all your help but i solved the problem. I had the $id variable in a header page which was included in this page. For some reason I had to create a new $id variable with the same text as the other one and put it in the page i was doing this for. Thanks again for all your help.

Thanks for all your help but i solved the problem. I had the $id variable in a header page which was included in this page. For some reason I had to create a new $id variable with the same text as the other one and put it in the page i was doing this for. Thanks again for all your help.

good for you. Ϋ

don't forget to mark this thread as solved. :)
happy day!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.