/    Sign up×
Community /Pin to ProfileBookmark

OOP PHP and MySQL need help in multiple select statement

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:xampphtdocsoop_reportclass_lib.php on line 46

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:xampphtdocsoop_reportclass_lib.php on line 46

this is my

[code=php]
<?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”;
}
}
?>
[/code]

and index.php

[code=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
?>
[/code]

Thank you

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsMay 06.2014 — That's a really bad class, lacks functionality.

When you use *_query() you typically want to check if it failed before working on the result.

Example[code=php]<?php
$db = mysql_connect(null, 'user', 'pwd');

if ($db === false || !mysql_select_db('dbname', $db)) {
exit('Unable to connect to database.');
}
$result = mysql_query('SELECT * FROM table', $db);

if ($result !== false) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
// ...
}
mysql_free_result($result);
} else {
// handle error with grace...
}

// how I typically shorthand query...
if (($result = mysql_query('SELECT * FROM table', $db)) !== false) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
// ...
}
mysql_free_result($result);
} else {
// handle error with grace...
}

// another shorthand way... a lot more readable on very long SQL queries.
$sql = 'SELECT * FROM table';

if (($result = mysql_query($sql, $db)) !== false) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
// ...
}
mysql_free_result($result);
} else {
// handle error with grace...
}
?>[/code]
×

Success!

Help @newphpcoder spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...