Does a table in a Database need to be open first?

I am getting an error stating the Database does not exit and an php error. Below is the messages I receive and under the messages is my program. The database is called awm and the table is called sales2014. The first program is the data entry which calls focusreport.

Customer Number: 111
Vendor Number: 222
Data received
In SumThree function
7
2014
2013
Database connected
Tables: (6)
customer
products
sales2013
sales2014
salespeople
vendor
sales2014 Does not exist
July
In JulyTY function
Database Error: No database selected
Failed - Database Error: (0)
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\focusreport.php on line 166

Focus List Report li {list-style:none; margin-bottom: 10px} <?php require_once "focusreport.php"; $focusdata = new Focusreport(); $answer = ''; // see if the forms was submitted if($_GET['formsubmit'] == "Submit") { echo "In Get section
"; $custnum = ""; $vendnum = ""; if(isset($_GET['sumreport'])){ $sreport = $_GET['sumreport']; } if(isset($_GET['detailreport'])){ $dreport = $_GET['detailreport']; } if(isset($_GET['threemonth'])){ $month3 = $_GET['threemonth']; } if(isset($_GET['sixmonth'])){ $month6 = $_GET['sixmonth']; } // Process required fields $custnum = filter_var($_GET['custnum'], FILTER_SANITIZE_STRING); if (!(trim($custnum))) { $errors[] = "You must enter a customer number"; } $vendnum = filter_var($_GET['vendnum'], FILTER_SANITIZE_STRING); if (!(trim($vendnum))) { $errors[] = "You must enter a vendor number"; } if ($errors) { foreach ($errors as $error) { echo $error. '
'; } } } $cust = $_GET['custnum']; $vend = $_GET['vendnum']; $focusdata->cust = $cust; $focusdata->vend = $vend; echo 'Customer Number: '. $focusdata->cust. '
'; echo 'Vendor Number: '. $focusdata->vend. '
'; if($_GET['summary'] && $_GET['three']) { echo 'Data received'. '
'; $focusdata->SumThree(); } elseif ($_GET['detail'] && $_GET['three']) { $focusdata->DetailThree(); } elseif ($_GET['summary'] && $_GET['six']) { $focusdata->SumSix(); } elseif ($_GET['detail'] && $_GET['six']) { $focusdata->DetailSix(); } ?> Enter data needed for your Form List Report
  • Customer Number:
  • Vendor Number:
  • Do you want a Summary or Detail Report? (Click One) Summary Detail
  • Do you want a Three or Six Month? (Click One) Three Six

<?php echo $answer; ?>

<?php /** * focusreport.php * * Focusreport class file * * @version 1.0 2014-06-08 * @package Example */ /** Focusreport class * * @package Example */ class Focusreport { /** * Customer Number * @var string */ public $cust; /** * Vendor Number * @var string */ public $vend; /** * SumThree * * Three Month Summary Report * */ function SumThree() { echo "In SumThree function". '
'; $month = date('n'); $year = date('Y'); $lyear = ( date('Y') - 1 ); echo $month. '
'; echo $year. '
'; echo $lyear. '
'; $db_host = "localhost"; $db_username = "John"; $db_pass = "013631"; $db_name = "awm"; $mysqli = new mysqli($db_host, $db_username, $db_pass, $db_name); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } echo "Database connected". '
'; // Show tables test if ($result2 = $mysqli->query("SHOW TABLES")) { $count = $result2->num_rows; echo "Tables: ($count)".'
'; while ($row =$result2->fetch_array()) { echo $row[0]. '
'; } } // End Show Tables Test // Test to see if sales2014 exist $val = mysql_query('select 1 from sales2014'); if($val !== FALSE) { echo "sales2014 Exists". '
'; }else{ echo "sales2014 Does not exist". '
'; } // End Test switch($month) { case 1: JanTY(); break; case 2: FebTY(); break; case 3: MarTY(); break; case 4: AprTY(); break; case 5: MayTY(); break; case 6: echo "June". '
'; JunTY(); break; case 7: echo "July". '
'; $this->JulTY($year, $mysqli); break; case 8: AugTY(); break; case 9: SepTY(); break; case 10: OctTY(); break; case 11: NovTY(); break; case 12: DecTY(); break; } } function JanTY() { } function FebTY() { } function MarTY() { } function AprTY() { } function MayTY() { } function JunTY() { //$JunTY = $mysqli->query("SELECT SUM(`CasesShip`) FROM sales2014 WHERE Cust_Code == $cust && // (Date >= 06/01/$year && <= 06/31/$year)); //$result = $JunTY; //echo $result; // $result = $mysqli->query("SELECT Name FROM City LIMIT 10")) // printf("Select returned %d rows.\n", $result->num_rows); } function JulTY($year,$mysqli) { echo "In JulyTY function". '
'; $JulyTY = "SELECT SUM(`CasesShip`) FROM sales2014 WHERE Cust_Code == 1235 && (Date >= 07/01/$year && <= 07/31/$year)"; //$result=mysql_query($JulyTY); $result=$mysqli->query($JulyTY); if (!$result) { echo "Database Error: ".mysql_error(). '
'; echo "Failed - Database Error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } // $JulyTY->data_seek(0); while($row = mysql_fetch_array($result)) { // while ($row = $JulyTY->fetch_assoc()) { echo "Customer: ".$row[Cust_Code]." Product: ".$row[Prod_Num]." Total Cases: ".$result; } // $JulyTY->free(); } function AugTY() { } function SepTY() { } function OctTY() { } function NovTY() { } function DecTY() { $result = 'The FocusList file shows as '.$this->cust.' and '.$this->vend; return $result; } } /** * DetailThree * * Three Month Detail Report * */ function DetailThree() { echo 'In DetailThree function'; $result = 'The Detail FocusList file shows as '.$this->cust.' and '.$this->vend; return $result; } /** * SumSix * * Six Month Summary Report * */ function SumSix() { echo 'In SumSix function'; $result = 'The Six Summary FocusList file shows as '.$this->cust.' and '.$this->vend; return $result; } /** * DetailSix * * Six Month Detail Report * */ function DetailSix() { echo 'In DetailSix function'; $result = 'The Six Detail FocusList file shows as '.$this->cust.' and '.$this->vend; return $result; } ?>

Hi, no one read you whole question, :stuck_out_tongue: please write only php warnings.

Ditto. At least in enclose your code in the forum PHP tags so its easy to read .

I apologize for making it it hard to read and understanding my question. I was in a hurry and should have taken my time. The first part is may way of tracking where the program went through along with the error which is shown under Part 1. Part 2, is the program. I am trying to figure out is why the program is unable to search and select the data found through the table called sales2014 in the awm database. You will see in the messages in Part 1 that it found sales2014. In the ‘Test to see if sales2014 exist’, it sends a message that the’ sales2014 does not exist’. I am confused.

 Part 1

In SumThree function
7
2014
2013
Database connected
Tables: (6)
customer
products
sales2013
sales2014
salespeople
vendor
sales2014 Does not exist
July
In JulyTY function
Database Error: No database selected
Failed - Database Error: (0)
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\focusreport.php on line 166

Part 2


<?php /** * focusreport.php * * Focusreport class file * * @version 1.0 2014-06-08 * @package Example */ /** Focusreport class * * @package Example */ class Focusreport { /** * Customer Number * @var string */ public $cust; /** * Vendor Number * @var string */ public $vend; /** * SumThree * * Three Month Summary Report * */ function SumThree() { echo "In SumThree function". '
'; $month = date('n'); $year = date('Y'); $lyear = ( date('Y') - 1 ); echo $month. '
'; echo $year. '
'; echo $lyear. '
'; $db_host = "localhost"; $db_username = "John"; $db_pass = "013631"; $db_name = "awm"; $mysqli = new mysqli($db_host, $db_username, $db_pass, $db_name); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } echo "Database connected". '
'; // Show tables test if ($result2 = $mysqli->query("SHOW TABLES")) { $count = $result2->num_rows; echo "Tables: ($count)".'
'; while ($row =$result2->fetch_array()) { echo $row[0]. '
'; } } // End Show Tables Test // Test to see if sales2014 exist $val = mysql_query('select 1 from sales2014'); if($val !== FALSE) { echo "sales2014 Exists". '
'; }else{ echo "sales2014 Does not exist". '
'; } // End Test switch($month) { case 1: JanTY(); break; case 2: FebTY(); break; case 3: MarTY(); break; case 4: AprTY(); break; case 5: MayTY(); break; case 6: echo "June". '
'; JunTY(); break; case 7: echo "July". '
'; $this->JulTY($year, $mysqli); break; case 8: AugTY(); break; case 9: SepTY(); break; case 10: OctTY(); break; case 11: NovTY(); break; case 12: DecTY(); break; } } function JanTY() { } function FebTY() { } function MarTY() { } function AprTY() { } function MayTY() { } function JunTY() { } function JulTY($year,$mysqli) { echo "In JulyTY function". '
'; $JulyTY = "SELECT SUM(`CasesShip`) FROM sales2014 WHERE Cust_Code == 1235 && (Date >= 07/01/$year && <= 07/31/$year)"; //$result=mysql_query($JulyTY); $result=$mysqli->query($JulyTY); if (!$result) { echo "Database Error: ".mysql_error(). '
'; echo "Failed - Database Error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } // $JulyTY->data_seek(0); while($row = mysql_fetch_array($result)) { // while ($row = $JulyTY->fetch_assoc()) { echo "Customer: ".$row[Cust_Code]." Product: ".$row[Prod_Num]." Total Cases: ".$result; } // $JulyTY->free(); } function AugTY() { } function SepTY() { } function OctTY() { } function NovTY() { } function DecTY() { $result = 'The FocusList file shows as '.$this->cust.' and '.$this->vend; return $result; } } /** * DetailThree * * Three Month Detail Report * */ function DetailThree() { echo 'In DetailThree function'; $result = 'The Detail FocusList file shows as '.$this->cust.' and '.$this->vend; return $result; } /** * SumSix * * Six Month Summary Report * */ function SumSix() { echo 'In SumSix function'; $result = 'The Six Summary FocusList file shows as '.$this->cust.' and '.$this->vend; return $result; } /** * DetailSix * * Six Month Detail Report * */ function DetailSix() { echo 'In DetailSix function'; $result = 'The Six Detail FocusList file shows as '.$this->cust.' and '.$this->vend; return $result; } ?>

I figured out the code which caused the error ‘sales2014 Does not exit’. I changed that code to;

// Test to see if sales2014 exist

$test1 = “SELECT * FROM sales2014”;

$val=$mysqli->query($test1);

if($val !== FALSE)
{
echo “sales2014 Exists”. ‘
’;
}else{
echo “sales2014 Does not exist”. ‘
’;
}

// End Test

The message now appears as ‘sales2014 Exists’. Now it is figuring out the code in the July function where I am getting the Database error.

I have to ask, is this a homework assignment or ment for a production site/ application? You really need to rethink your database design if it is for the latter. That can be accomplished far better with much less code and in a better optimized search aspect than the way you are presenting.

Sponsor our Newsletter | Privacy Policy | Terms of Service