Hi,
Good day!
I am new in coding of PHP in OOP way.
I found a sample OOP MySQL connect to database and it works fine, but when I tried to add another query for displaying output I got an error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\oop_report\class_lib.php on line 46
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\oop_report\class_lib.php on line 46
this is my
<?php
class createConnection //create a class for make connection
{
var $host = "localhost";
var $username = "root";
var $password = "";
var $database = "operators_report";
var $myconn;
var $select;
var $query;
function connectToDatabase() //create a function for connect to database
{
$conn = mysql_connect($this->host,$this->username,$this->password);
if(!$conn) // testing the connection
{
die ("Cannot connect to the database");
}
else
{
$this->myconn = $conn;
echo "Connection established";
}
return $this->myconn;
}
function selectDatabase() //selecting the database
{
mysql_select_db($this->database); //use php inbuil function for select database
if(mysql_error()) //if error occured display the error message\\
{
echo "Cannot find the database " . $this->database;
}
echo "Database selected..";
}
function query_select($select)
{
$this->query= mysql_query($select);
}
function fetch_query()
{
return mysql_fetch_array($this->query);
}
function closeConnection() //close the connection
{
mysql_close($this->myconn);
echo "Connection closed";
}
}
?>
and index.php
<?php
include('class_lib.php');
$connection = new createConnection(); //I created a new object
$connection->connectToDatabase(); //connected to the database
echo "<br/>"; //putting a html break
$connection->selectDatabase(); // select database
echo "<br/>";
$select = "SELECT * From process_list";
$result = $connection->query_select($select); //query process_list table
echo "<table border= '1'>";
echo "<tr>";
while ($row = $connection->fetch_query()) //fetch data
{
$process_id = $row['process_id'];
$process_name = $row['process_name'];
echo "<th>$process_name</th>";
$select_ = "SELECT p.process_name, SUM(o.compound_output) AS output, shift_date
FROM process_list AS p JOIN op_output AS o ON (p.process_id = o.process_id)
WHERE WEEK(shift_date, 0) = '45' AND p.process_name = '$process_name';
GROUP BY shift_date";
$result_ = $connection->query_select($select_); //query process_list table
echo "<tr>";
while($row_output = $connection->fetch_query()){
$output = $row_output['output'];
echo "<td>$output</td>";
}
echo "</tr>";
}
echo "</tr>";
echo "</table>";
echo "<br/>";
$connection->closeConnection(); // closed connection
?>
Thank you