Page 1 of 1
function to select table
Posted: Tue Aug 01, 2006 4:21 am
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?
Posted: Tue Aug 01, 2006 5:00 am
by shiznatix
at a quick glance
Code: Select all
//wrong
table_select($GET['number']);
//correct
table_select($_GET['number']); //notice the underscore
Posted: Tue Aug 01, 2006 5:08 am
by rsmarsha
Lol it's always something simple

, 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.
Posted: Tue Aug 01, 2006 5:17 am
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.
Posted: Tue Aug 01, 2006 5:56 am
by rsmarsha
Changed it to
and it works. Only made it .inc before as i saw suggested on some tutorial once.

Posted: Tue Aug 01, 2006 5:57 am
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.