urbananarchist
New member
I have this script that connects to mysql server and outputs all the data from a specific table. Works fine but only if I pass ?skill=whatever.
What I want to accomplish is. if skill is not passed and you navigate to the page it will have a default value inputted and use that properly.
<?php
$newdbname = 'Test'; //Have removed these for tests
$newdbname =$_GET['skill']; // and this line as well.
$o .='<font face="arial" size="+4"><center>';
$id_link = @mysql_connect($dbhost, $loginname, $loginpass);
$tables = mysql_list_tables($dbname, $id_link);
$num_tables = mysql_num_rows($tables);
// store table names in an array
$arr_tablenames[] = '';
// store number of fields per table(index 0,1,2..) in an array
$arr_num_fields[] = '';
for ($i=0; $i < $num_tables; $i++) {
$arr_tablenames[$i] = mysql_tablename($tables, $i);
$arr_num_fields[$i] = mysql_num_fields(mysql_db_query($dbname, "select * from $arr_tablenames[$i]", $id_link));
}
// store field names in a multidimensional array:
// == table number, [ii] == field number for that table
for ($i=0; $i < $num_tables; $i++) {
for ($ii=0; $ii < $arr_num_fields[$i]; $ii++) {
$result = mysql_db_query($dbname, "select * from $arr_tablenames[$i]", $id_link);
$hash_field_names[$i][$ii] = mysql_field_name($result, $ii);
}
}
for ($i=0; $i < $num_tables; $i++) {
$o .="<center><h2>Table $arr_tablenames[$i] </h2></center>";
$o .='<table align="center" border="1" id="myTable" class="tablesorter"><thead><tr>';
$result = mysql_db_query($dbname, "select * from $newdbname", $id_link);
for ($ii=0; $ii < $arr_num_fields[$i]; $ii++) {
$o .="<th>";
$o .= $hash_field_names[$i][$ii];
$o .=("</th>");
}
$o .="</tr></thead><tbody><tr>";
$number_of_rows = @mysql_num_rows($result);
for ($iii = 0; $iii < $number_of_rows; $iii++) {
$record = @mysql_fetch_row($result);
for ($ii=0; $ii < $arr_num_fields[$i]; $ii++) {
$o .="<td>";
$o .= $record[$ii];
$o .="</td>";
}
$o .="</tr>";
}
$o .="</tbody>";
$o .="</table>";
}
echo $o;
?>
I have tried
if (skill==NULL)
{
$newdbname = 'Test';
}
else
$newdbname='Test';
Also have used as the if condition if(empty($skill)) and if(skill=="")
And yes I know naming the variable newdbname is kind of confusing as I'm just getting from a table.
I've also tried to define $newdbname="test";
with and without quotes but recieved a parse error. I'm assuming the value set to skill is a string.
I probably need to clean up the script a bit to not get all of the table information from the server but I like being able to use the row names as the table head and I'm not good with php syntax at the moment.
What I want to accomplish is. if skill is not passed and you navigate to the page it will have a default value inputted and use that properly.
<?php
$newdbname = 'Test'; //Have removed these for tests
$newdbname =$_GET['skill']; // and this line as well.
$o .='<font face="arial" size="+4"><center>';
$id_link = @mysql_connect($dbhost, $loginname, $loginpass);
$tables = mysql_list_tables($dbname, $id_link);
$num_tables = mysql_num_rows($tables);
// store table names in an array
$arr_tablenames[] = '';
// store number of fields per table(index 0,1,2..) in an array
$arr_num_fields[] = '';
for ($i=0; $i < $num_tables; $i++) {
$arr_tablenames[$i] = mysql_tablename($tables, $i);
$arr_num_fields[$i] = mysql_num_fields(mysql_db_query($dbname, "select * from $arr_tablenames[$i]", $id_link));
}
// store field names in a multidimensional array:
// == table number, [ii] == field number for that table
for ($i=0; $i < $num_tables; $i++) {
for ($ii=0; $ii < $arr_num_fields[$i]; $ii++) {
$result = mysql_db_query($dbname, "select * from $arr_tablenames[$i]", $id_link);
$hash_field_names[$i][$ii] = mysql_field_name($result, $ii);
}
}
for ($i=0; $i < $num_tables; $i++) {
$o .="<center><h2>Table $arr_tablenames[$i] </h2></center>";
$o .='<table align="center" border="1" id="myTable" class="tablesorter"><thead><tr>';
$result = mysql_db_query($dbname, "select * from $newdbname", $id_link);
for ($ii=0; $ii < $arr_num_fields[$i]; $ii++) {
$o .="<th>";
$o .= $hash_field_names[$i][$ii];
$o .=("</th>");
}
$o .="</tr></thead><tbody><tr>";
$number_of_rows = @mysql_num_rows($result);
for ($iii = 0; $iii < $number_of_rows; $iii++) {
$record = @mysql_fetch_row($result);
for ($ii=0; $ii < $arr_num_fields[$i]; $ii++) {
$o .="<td>";
$o .= $record[$ii];
$o .="</td>";
}
$o .="</tr>";
}
$o .="</tbody>";
$o .="</table>";
}
echo $o;
?>
I have tried
if (skill==NULL)
{
$newdbname = 'Test';
}
else
$newdbname='Test';
Also have used as the if condition if(empty($skill)) and if(skill=="")
And yes I know naming the variable newdbname is kind of confusing as I'm just getting from a table.
I've also tried to define $newdbname="test";
with and without quotes but recieved a parse error. I'm assuming the value set to skill is a string.
I probably need to clean up the script a bit to not get all of the table information from the server but I like being able to use the row names as the table head and I'm not good with php syntax at the moment.