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.

 

Link to comment
Share on other sites

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';
	}

}
?>

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.