0

Hi,

I am new to PHP Development. I am retrieving data from my database table clf_email. I am having a mysql_num_rows error in results. I cannot understand where i am going wrong and what error i am doing in the code. Please help in removing this error.

<?php 

require_once("../db.php");

		$id = $_GET['emailID'];

		$query = "SELECT * FROM `clf_email` WHERE emailID ='".$id."'";

		$result =mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);
		
		$sql = "DELETE FROM $t_email WHERE emailID = $id";
		$result = mysql_query($sql);

?>
<table width="943" border="1" cellpadding="0" cellspacing="0" id="email_tbl">

<?php  if(mysql_num_rows($result) < 1){

echo "<div align='center' class='err'><b>There is no data available in the database!</b></div>";

}else {

//echo "<div class='flag'><b>Database is searched and the data you asked is given below!</b></div>";

while( $row = mysql_fetch_array($result)){
?>

<tr>
    <td width="110" id="email"><a href="<?php echo $row['email']; ?>" class="email_link"><?php echo $row['email']; ?></a></td>

    <td width="417" id="email_txt"><?php echo $row['message']; ?></td>

    <td width="115" id="email_txt"><?php echo $row['date']; ?></td>
</tr>
<?php } } }; ?>
3
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by simirnov
0

line 18:


if(mysql_num_rows($result) < 1) // it returns a number = true, otherwise is FALSE, i don't think you can use operator here, try below


if(!mysql_num_rows($result)) // if not true

0

Lots of thanks for the help cheelee!

I followed your suggestion and used the if(!mysql_num_rows($result)) but the error "mysql_num_rows() supplied argument is not valid" is still there.

0

ok! I have echo the $query and found that the problem is in the $id = $_GET;.

In $id veriable, $_GET is not retrieving the email id no from the database.

When i type $query = "SELECT * FROM `clf_email` WHERE emailID = 3"; it returns the email data having the id# 3.

So, it means that $_GET is not getting the email id from the database.

Please help me why the $_GET is not getting the email id from the database.

Edited by simirnov: some additions needed

0

try this, this will print out all the email.

<?php

 

require_once("../db.php");

 

//delete $id = $_GET['emailID'];

 

$query = "SELECT * FROM `clf_email`";

 

$result =mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);

 

$sql = "DELETE FROM $t_email WHERE emailID = $id";

$result = mysql_query($sql);

 

?>

do you have search form? if you don't, $_GET didnt work because it cant receive unique id when u want to read specific email. For example,

echo "

$id = $_POST['id'];

<form action='reademail.php?emailID=$id' method='POST'>
TEXT : <br />
<input type='text' name='id' value='true'>

<input type='submit' value='Search'>
</form>


";

Edited by amie900218: n/a

0

Thanks for your reply and time Amie!

Yes, you are right i am not using any form in the code b/c page does not need it.
I have taken the form as suggested above but the $id variable is still empty.

<form action='read_emails.php?emailID=$id' method='POST'>
<table width="943" border="1" cellpadding="0" cellspacing="0" id="email_tbl">
<?php  if(!mysql_num_rows($result)){
echo "<div align='center' class='err'><b>There is no data available in the database!</b></div>";
}else {
//echo "<div class='flag'><b>Database is searched and the data you asked is given below!</b></div>";
while( $row = mysql_fetch_array($result)){
?>
   <tr>
    <td width="110" id="email"><a href="read_emails.php?emailID=$id" class="email_link"><?php echo $row['email']; ?></a></td>

    <td width="417" id="email_txt"><?php echo $row['message']; ?></td>
    <td width="115" id="email_txt"><?php echo $row['date']; ?></td>

<?php } }; ?>

The result is this when i echo the query. SELECT * FROM `clf_email` WHERE emailID= ''

and in the address bar, it publishes the query string.
http://localhost/zero/admin/read_emails.php?emailID=$id

Edited by simirnov: n/a

0

my bad, you didnt need search form to get specific email. just a simple view list of mail like this will do.

1. email 1 read|delete
2. email 2 read|delete
3. email 4 read|delete

so, when you want to read example like email 2, the link will be like this
http://localhost/zero/admin/read_emails.php?emailID=2

if that case, its simple.

first, create emailList.php . this will make a list view for your email.

<?php
include 'class.php';

$obj = new email();

//Setup our connection vars
$obj->host = 'localhost';
$obj->username = 'root';
$obj->password = '';
$obj->db = emaillist';

//Connect To Our DB
$obj->connect();
?>
<html>
<head>Email
</head>

<body>
<table>
<tr>
<td>

<?php echo $obj->manage_email()?>


</td>
</tr>
</table>

</body>


<html>

this page will print your email list. pls notice that above html code,
there will php code that will intergerate with the
class. then, for the class, create class.php.

<?php

class email{
	 var $host;
	 var $username;
	 var $password;
	 var $db;
	 
//connect to databse

	 function connect(){
	 $con = mysql_connect($this->host, $this->username, $this->password) or die(mysql_error());
	 mysql_select_db($this->db, $con) or die(mysql_error());
	 }

//code start here. this code is for emailList.php

function manage_email() {


$sql = "SELECT * FROM clf_email";
$id = $row['emailID'];
$res = mysql_query($sql) or die(mysql_error());
$res2 = mysql_query($sql) or die(mysql_error());
$num_rows = mysql_num_rows($res2);
		
	if ($num_rows==0){
	echo "No email right now.</br>";
		echo "</br>";
		echo "</br>";
			
			
		}else{
		while($row = mysql_fetch_assoc($res)) :
		$res2 = mysql_query($sql) or die(mysql_error());
		$num_rows = mysql_num_rows($res2);

		?>
		
<h3 class="title">Email Name <?php echo $row['emailTitle']?></h3><a href="read.php?id=<?php echo $row['emailID']?>">Read</a> 
			</div>
			
		<?php
		
			endwhile;
			
				echo "</br>";
			echo "</br>";
			echo "</br>";
		
			}
	 


}

//the next code is for read.php

function read_email()
{

$id = $_GET['id'];
$get = mysql_query("SELECT * FROM clf_email WHERE emailID = '$id'");

while ($get_row = mysql_fetch_assoc($get))

{

//get data from database
// i only presume that you have message field in database
// this function will take 'message' data from email that u hve choose

	$get_text = $get_row['message']; 
	
	echo $get_text;
	
}

}

}

and for read.php

<?php
include 'class.php';

$obj = new email();

//Setup our connection vars
$obj->host = 'localhost';
$obj->username = 'root';
$obj->password = '';
$obj->db = emaillist';

//Connect To Our DB
$obj->connect();
?>
<html>
<head>Email
</head>

<body>
<table>
<tr>
<td>

<?php echo $obj->read_email()?>


</td>
</tr>
</table>

</body>


<html>

this is only for reference. i have not tested is yet.
u will get the idea.

Edited by amie900218: n/a

0

hi Amie,

Thanks for such detailed answer! This has solved my problem!

So nice of you!:)
TC!
Bye.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.