S
S
StanislavValiev2015-12-09 08:39:31
PHP
StanislavValiev, 2015-12-09 08:39:31

Mysql query in php, why does it throw an error when using a class?

Nothing works and Warning is displayed: mysql_num_rows() expects parameter 1 to be resource, string given in L:\OpenServer\domains\CMS-lite\engine\templates\default\index.php on line 71
When I try to pull data through a class :

//сам класс
class DataBase{

  const BD_HOST = "localhost";
  const BD_USER = "stas";
  const DB_PASS = "stas";
  const DB = 'CMS-Lite';

  public function __construct(){
    @mysql_connect(DB_HOST,DB_USER,DB_PASS);
    @mysql_select_db(DB);
    }

    public function getDb(){

     }
    public function query($query){
            return $query;
    }
 }

//использование класса
 $mysqli = new DataBase;
 $query = $mysqli->query('SELECT * FROM news');
 while ( $row = mysql_fetch_array($query) ) {
        foreach ($row as $col_value) {
         print "$col_value";
           }
         }

Если написать без класса, а просто инструкции, то все проходит нормально:
@mysql_connect('localhost','stas','stas');
@mysql_select_db('CMS-Lite');
$query = mysql_query('SELECT * FROM news');
 while ( $row = mysql_fetch_array($query) ) {
                  foreach ($row as $col_value) {
                     print "$col_value";
                  }
                }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2015-12-09
@StanislavValiev

Did you try to read the text of the error? It says that a string was passed to mysql_num_rows, not a resource. And it even says in which line. In the piece of code you cited, this call is not present at all.
And in general, swallowing mistakes is a bad idea. Remove dogs, add error display to base calls.
And yes, the mysql_* functions are deprecated, in php7 they were cut out altogether. It's time to learn PDO or at least mysqli.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question