Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts

    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:\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 Code:
    <?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->querymysql_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 Code:
    <?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

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,858
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Try changing $this->query= mysql_query($select); to $this->query= mysql_query($select) or die(mysql_error());

    PS: Stop using the deprecated mysql_ library. Use mysqli_ or PDO instead.
    You are just duplicating the code by making the class. It's not the proper way!
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •