Hi Guys,
I am developing a Music Database system.
This is a 'Song.php' page. It will be a Detailed page of a each song. Ex : song.php?id=1

<?php

if(!empty($_GET['id']))
    {
        $sid = mysql_real_escape_string ($_GET['id']);
    }
    // if song id is null, user will redirect to the full song list
    else 
        header ("Location: songlist.php");
?>
<?php
// including database connection
include ('datab.php');

$songs = mysql_query("SELECT * FROM `songs` WHERE `id`='$sid'")or die(mysql_error());
if(mysql_num_rows($songs)> 0)
{
    while($row = mysql_fetch_array($songs)){


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $row['name']; ?> - Music Onz</title>
</head>

<body>

</body>
<?php
}

}
?>
</html>

I want to add message "Invalid or Deleted" if system return 0 after this code

$songs = mysql_query("SELECT * FROM `songs` WHERE `id`='$sid'")or die(mysql_error());

I know I have to write else part to

if(mysql_num_rows($songs)> 0)
     {

but, I have no idea where to place else part :(
Please help me guys

Recommended Answers

All 6 Replies

I need some clarification, but happy to help. Where do you want to add the message? Are you updating the DB table or writing it to the screen?

BTW this is easily accomplished using PDO (you'll need the $conn part):

Example from the manual:

<?php
$sql = "SELECT COUNT(*) FROM fruit WHERE calories > 100";
if ($res = $conn->query($sql)) {

    /* Check the number of rows that match the SELECT statement */
  if ($res->fetchColumn() > 0) {

        /* Issue the real SELECT statement and work with the results */
         $sql = "SELECT name FROM fruit WHERE calories > 100";
       foreach ($conn->query($sql) as $row) {
           print "Name: " .  $row['NAME'] . "\n";
         }
    }
    /* No rows matched -- do something else */
  else {
      print "No rows matched the query.";
    }
}

$res = null;
$conn = null;
?>

I need some clarification, but happy to help. Where do you want to add the message? Are you updating the DB table or writing it to the screen?

Thank You for your reply.

Simply we are doing this way. Aren't we?

$songs = mysql_query("SELECT * FROM `songs` WHERE `id`='$sid'")or die(mysql_error());
if(mysql_num_rows($songs)> 0)
    {
    echo $row['name'];
    }
    else
    {
       echo "Invalid / Deleted!"
    }

But in this case I can't use that. Because there is while($row = mysql_fetch_array($songs)){

in between if{ }

Therefore I need your help to place else party

Why not just set a vaiable to whatever string you want and echo it if the IF statement fails?

$var = "Invalid / Deleted!";
if (mysql_num_rows($songs)> 0){
echo $row['name'];
}else{
echo $var;
}

Why not just set a vaiable to whatever string you want and echo it if the IF statement fails?

Then where should I write while($row = mysql_fetch_array($songs)){ part?

You can have the IF/ELSE run inside the WHILE. If not the query related variables wont be available.

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.