Hello, I have done a PHP project and on my localhost it is working perfectly but when I uploaded it on my webspace the CMS part of my website did not work as it is supposed to.

The strange thing is that the client side of the website is working correctly not like the admin part of the website(CMS).

The error given is:Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/a5800815/public_html/draw/datagrid.php on line 48

The code for datagrid.php is:

<?php


        class datagrid
        {





            public $dataset;
            public $name;
            public $fields;
            public $altClass;
            public $itemID;
            public $oClass;
            //public $noClass;
            public $buffer;






            function __construct($name, $dataset)
            {
                $this->name = $name;
                $this->dataset = $dataset;

            }




            function datablind()
            {


             echo '<table name="'.$this->name.'"  id="'.$this->name.'" class="'.$this->altClass.'">';
               	echo "<tr>";
			foreach($this->fields as $field => $value) {
                        echo "<th>" . $field . "</th>";
                    }
		echo "</tr>";


             $count = 1;
            while($record = mysql_fetch_assoc($this->dataset))
            {

                /*if (isset($this->noClass['set']))
                {*/

                    $count=$count+1;
                    if (($count % 2)==0)
                    {
                          echo '<tr class="'.$this->altClass.'">';
                                foreach($record as $field => $value)
                                    {

                                        if (in_array($field, $this->fields))
                                        {
                                                /*if (in_array('image',$this->fields))
                                                {
                                                    echo '<td><img src="../uploads/' .$value . '"</td>';

                                                }
                                                else*/
                                                {
                                                     echo '<td>' . $value . '</td>';
                                                }
                                        }


                                }
                               echo '<td><a href="?id='. $record['ID'] .'">Select</a></td>';
                               echo '<td><a href="?updateid='. $record['ID'] .'">Update</a></td>';
                               echo '<td><a href="?deleteid='. $record['ID'] .'">Delete</a></td>';
                                echo "</tr>";

                    }
                    else
                    {
                         echo '<tr class="'.$this->oClass.'">';
                                foreach($record as $field => $value)
                                    {

                                        if (in_array($field, $this->fields))
                                        {
                                                /*if (in_array('image',array_keys($record)))
                                                {
                                                    echo '<td><img src="../uploads/' .$value . '"</td>';

                                                }
                                                else*/
                                                {
                                                     echo '<td>' . $value . '</td>';
                                                }



                                        }


                                    }
                               echo '<td><a href="?id='. $record['ID'] .'">Select</a></td>';
                               echo '<td><a href="?updateid='. $record['ID'] .'">Update</a></td>';
                               echo '<td><a href="?deleteid='. $record['ID'] .'">Delete</a></td>';
                                echo "</tr>";



                    }
                }




           echo "</table>";

		//$row = mysql_fetch_assoc($this->buffer);
                //print_r($row);





            }

            }


        ?>

Would someone be so kind to help me out this problem as I am new to PHP?

Thanks in advance Marius

Dani AI

Generated

A short diagnosis and practical fixes tied to the thread

The runtime warning indicates the fetch call was given an invalid result value rather than a working query result. That usually means the SQL query or connection failed earlier, or the datagrid was handed the wrong value. Problems that show on a remote host but not locally often include wrong credentials, different table name casing on Linux hosting, missing table prefix from a hosting control panel, or a failed query that was never checked.

Immediate troubleshooting (quick checklist)

  • Confirm the database connection and credentials that the live site actually uses.
  • After the query runs, test its return value and log the database error (mysql_error / mysqli_error / PDO exception) instead of proceeding to fetch.
  • Verify table names and included files for case sensitivity on the host filesystem.
  • Ensure the code that constructs the datagrid passes a real result resource/object (or an array of rows), not false/null.
  • Temporarily enable full error reporting during diagnosis: ini_set('display_errors',1); error_reporting(E_ALL); then disable it for production.

Safe fetching examples (use these instead of assuming the query succeeded)

$result = mysqli_query($conn, $sql);
if (! $result) {
    error_log('Query failed: ' . mysqli_error($conn));
    return;
}
while ($row = mysqli_fetch_assoc($result)) {
    // render row
}

Modern alternative (PDO, fetch all rows, then present)

$stmt = $pdo->prepare($sql);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
// pass $rows to the renderer instead of a DB resource

Longer-term: follow 's advice to separate DB logic from HTML output, use prepared statements (PDO or mysqli) and have the datagrid accept an array of rows rather than a DB-specific resource. That both avoids this class of warning and makes migration off deprecated mysql_* functions straightforward.

your datagrid class is poorly designed and here is why:
1. You have mixed Database issues with non database
-Either make it fully database with all connections and queries in one class
2. You make mistake of adding html into class. Html should be in separate php class where you will be calling class methods. Here is small skeletal example. I have mixed DB functions and data getting but it is best to separate them

<?php
class DataClass{
    private $host="localhost";
    private $username="database_username";
    private $password = "password";
    private $db = "blog_database";
    private $conn; //shared connection by all class function via $this->conn
    
    public function __construct(){
       $this->conn = mysql_connect(.....);
       mysql_select_db(...); //use $this->db/conn
    }
    
    public function getBlogData($id){
        //get all the data using query
        //get array of blog data
        $array = mysql_fetch_assoc(....);//array of blog data
        return $array;
    }
    
    //other functions

}
?>


<html>
    <head>
    </head>
    
    <body>
        <div>
        <?php
            $blog_obj = new DataClass();        
            foreach($blog_obj->getBlogData($_POST['id']) as $heading=>$contents){
                echo "<h3>$heading</h3> $contents";
            }
        ?>
        </div>
    </body>

</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.