function to select table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

function to select table

Post by rsmarsha »

I have the following function to find which table an order number is contained in.

Code: Select all

function table_select($orderno)
{
	global $table;
$table_select = "SELECT orderno FROM orders WHERE orderno='$orderno'";
$tq = mysql_query($table_select) or die ("Query 1:$table_select Failed".mysql_error());
if (mysql_num_rows($tq)>0) 
	{ 
	$table = 'orders';
	echo '1<br />';
	return;
	}
$table_select = "SELECT orderno FROM orders_main WHERE orderno='$orderno'";
$tq = mysql_query($table_select) or die ("Query 2:$table_select Failed".mysql_error());
if (mysql_num_rows($tq)>0) 
	{
	$table = 'orders_main'; 
	echo '2<br />';
	return;
	}
$table_select = "SELECT orderno FROM orders_trash WHERE orderno='$orderno'";
$tq = mysql_query($table_select) or die ("Query 3:$table_select Failed".mysql_error());
if (mysql_num_rows($tq)>0) 
	{ 
	$table = 'ordertrash';
	echo '3<br />';
	return;
	}
$table_select = "SELECT orderno FROM orders WHERE orderno='$orderno'";
$tq = mysql_query($table_select) or die ("Query 4:$table_select Failed".mysql_error());
if (mysql_num_rows($tq)>0) 
	{ 
	$table = 'orders_trash'; 
	echo '4<br />';
	}
}
//call the function
table_select($GET['number']);
It used to work and set the table as $table, but for some reason it doesn't now echo anything out as a result. Any ideas?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

at a quick glance

Code: Select all

//wrong
table_select($GET['number']);

//correct
table_select($_GET['number']); //notice the underscore
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

Lol it's always something simple :oops: , thanks for that.

Still having a problem though. When i run the file on it's own in the browser it works fine, but when i have it included into an index i get a blank section where the file is.

With

Code: Select all

require 'functions.inc';
//table select
table_select($_GET['number']);
in the file i just get

Code: Select all

//table select function table_select($orderno) { global $table; $table_select = "SELECT orderno FROM orders WHERE orderno='$orderno'"; $tq = mysql_query($table_select) or die ("Query 1:$table_select Failed".mysql_error()); if (mysql_num_rows($tq)>0) { $table = 'orders'; echo '1
'; return; } $table_select = "SELECT orderno FROM orders_main WHERE orderno='$orderno'"; $tq = mysql_query($table_select) or die ("Query 2:$table_select Failed".mysql_error()); if (mysql_num_rows($tq)>0) { $table = 'orders_main'; echo '2
'; return; } $table_select = "SELECT orderno FROM orders_trash WHERE orderno='$orderno'"; $tq = mysql_query($table_select) or die ("Query 3:$table_select Failed".mysql_error()); if (mysql_num_rows($tq)>0) { $table = 'ordertrash'; echo '3
'; return; } $table_select = "SELECT orderno FROM orders WHERE orderno='$orderno'"; $tq = mysql_query($table_select) or die ("Query 4:$table_select Failed".mysql_error()); if (mysql_num_rows($tq)>0) { $table = 'orders_trash'; echo '4
'; } } 
Fatal error: Call to undefined function: table_select() in /homepages/40/d108546070/htdocs/test/sec_test/admin/orders/ordershow.php on line 5
outputted to the browser. It seems to be showing the function file as text and not as code, which is why the function is undefined, but i can't see why.




If i remove the function and manually set $table to 'orders_main', it works fine.
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

The functions.inc file is :

Code: Select all

//table select
function table_select($orderno)
{
	global $table;
$table_select = "SELECT orderno FROM orders WHERE orderno='$orderno'";
$tq = mysql_query($table_select) or die ("Query 1:$table_select Failed".mysql_error());
if (mysql_num_rows($tq)>0) 
	{ 
	$table = 'orders';
	echo '1<br />';
	return;
	}
$table_select = "SELECT orderno FROM orders_main WHERE orderno='$orderno'";
$tq = mysql_query($table_select) or die ("Query 2:$table_select Failed".mysql_error());
if (mysql_num_rows($tq)>0) 
	{
	$table = 'orders_main'; 
	echo '2<br />';
	return;
	}
$table_select = "SELECT orderno FROM orders_trash WHERE orderno='$orderno'";
$tq = mysql_query($table_select) or die ("Query 3:$table_select Failed".mysql_error());
if (mysql_num_rows($tq)>0) 
	{ 
	$table = 'ordertrash';
	echo '3<br />';
	return;
	}
$table_select = "SELECT orderno FROM orders WHERE orderno='$orderno'";
$tq = mysql_query($table_select) or die ("Query 4:$table_select Failed".mysql_error());
if (mysql_num_rows($tq)>0) 
	{ 
	$table = 'orders_trash'; 
	echo '4<br />';
	}
}
Removing the require and pasting this into the page works, but i want to have the require in so i don't have to have all the functions pasted.
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

Changed it to

Code: Select all

require 'functions.php';
and it works. Only made it .inc before as i saw suggested on some tutorial once. :)
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

rsmarsha wrote:and it works. Only made it .inc before as i saw suggested on some tutorial once. :)
That must have been one rubbish tutorial.

You can do it like that, but you need to set up your server to parse .inc files.
Post Reply