Jump to content

[SOLVED] testing to see if result id is found in db table


bradkenyon

Recommended Posts

I have a download script, and I only want it to run if the id is found in the db table, which refers to the document filename.

 

I think you have to test to see if the number of times it is found, which should be once, since it is a unique id, then it should be a test for if it is larger than 0, then run the script, if not, then display a msg saying no file w/ that id.

 

Here is what I have:

<?php 
include('../cgi-bin/cms/collegeevents/email_function.php');

$docid = $_GET['docid'];

if(!$docid == '')
{
	$query = "SELECT * FROM calendar_items WHERE id = '$docid'";
	$result = mysql_query($query);

	while($row = mysql_fetch_array($result))
	{
		print mysql_num_rows($result);
		if(mysql_num_rows($result) > 0)
		{
			DownloadFile($row['docurl']);
			print 'downloaded';
		}
		else
		{
			print 'no file for that id';
		}
	}
}
?>

 

Any help is appreciated.

 

That's pretty much the way you should go. You can use LIMIT 1 to fetch only one row from the table which will allow you to drop the

while($row = mysql_fetch_array($result)) {}

loop

 

So:

 

<?php 
include('../cgi-bin/cms/collegeevents/email_function.php');

$docid = $_GET['docid'];

if(!$docid == '')
{
	$query = "SELECT * FROM calendar_items WHERE id = '$docid' LIMIT 1";
	$result = mysql_query($query);

	print mysql_num_rows($result);
	if(mysql_num_rows($result) > 0)
	{
		$row = mysql_fetch_array($result);
		DownloadFile($row['docurl']);
		print 'downloaded';
	}
	else
	{
		print 'no file for that id';
	}

}
?>

Thanks, that worked.

 

Modded it a little, to do a check to see if the docid actually has a file associated w/ it.

 

As well if it is a valid id.

 

Now I want to know why whenever I test using a valid id, but the id doesn't has an empty docurl, it prints out "downloaded" and doesn't print out "no file for that id"

 

<?php include('../cgi-bin/cms/collegeevents/email_function.php');

$docid = $_GET['docid'];

if($row['docurl'] != '')
{
	$query = "SELECT * FROM calendar_items WHERE id = '$docid' LIMIT 1";
	$result = mysql_query($query);

	if(mysql_num_rows($result) > 0)
	{
		$row = mysql_fetch_array($result);

		if($row['docurl'])
		{
			DownloadFile($row['docurl']);
			print 'downloaded';
		}
		else
		{
			print 'no file for that id';
		}
	}
	else
	{
		print 'invalid id';
	}

}?>

 

nevermind, it works.

 

thank you!

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.