2017-06-23 2 views
1

Ich möchte Daten aus der Datenbank mit MySQLi holen. Aber ich habe einen Fehler darin. Bitte überprüfen Sie meinen Code und geben Sie mir Vorschläge zur Verbesserung.in Bezug auf oops in PHP

-Code von class.php

class main{ 
    public $host="localhost"; 
    public $username="root"; 
    public $password=""; 
    public $db_name= "db_tvw"; 
    private $img_path = 'slider_img_upload/'; 

    public function __construct(){ 
     $this->run= new mysqli($this->host, $this->username, $this->password, $this->db_name); 
     if (mysqli_connect_errno()){ 
      echo "database connection is fail"; 
      exit; 
     } 
    } 

    public function select_data_from_db($table_name ,$run){ 
     $stmt=$run->prepare("SELECT * FROM ".$table_name); 
     $stmt->execute(); 
     $result = $stmt->get_result(); 
     $total_count=$result->num_rows; 
     $result= array(); 
     if($total_count>0){ 
      while ($row = mysqli_fetch_array($query)) { 
       $result[] = $row; 
      } 
     } 
     return $result; 
    } 

-Code von index.php

<?php $myrow=$obj->select_data_from_db("home_slider",$run); ?> 
<tr> 
    <td><?php echo $myrow['id']; ?> </td> 
    <td><?php echo $myrow['title']; ?> </td> 
    <td><?php echo $myrow['description']; ?> </td> 
</tr> 

Fehler, die ich erhalte:

Notice: Undefined variable: run in 
D:\Xampp\htdocs\admin\slider_fetch_data.php on line 24. 

Fatal error: Call to a member function prepare() on null in 
D:\Xampp\htdocs\admin\config.php on line 116. 
+0

Wo erstellen Sie ein Objekt der 'main' Klasse? zeige diesen Code. –

+0

In der Klasse.php am Ende der Datei hier bin ich ein Objekt der wichtigsten Klassencode ist hier: öffentliche Funktion URL ($ url) { Header ("location:". $ Url); \t}} $ obj = new main; – kulu

+0

Nur als eine Frage des Stils - Klassennamen beginnen normalerweise mit einem Kapital, also wäre Main vorzuziehen. Außerdem ist $ run eine Instanzvariable, daher sollte mit 'this' referenziert werden - in diesem Fall: $ stmt = $ this-> run-> prepare ("SELECT * FROM". $ Table_name); ' –

Antwort

0
<?php 
    $myrow = new main(); 
    $myrow=$obj->select_data_from_db("home_slider",$myrow->run); 
?> 
<tr> 
    <td><?php echo $myrow['id']; ?> </td> 
    <td><?php echo $myrow['title']; ?> </td> 
    <td><?php echo $myrow['description']; ?> </td> 
</tr> 

Sie können es versuchen

+0

Hey nach dem Versuch, diesen Code i Ich bekomme diese Fehler: Hinweis: Undefinierte Variable: Abfrage in D: \ Xampp \ htdocs \ admin \ config.php in Zeile 122 Warnung: mysqli_fetch_array() erwartet Parameter 1 zu mysqli_result, Null in D gegeben: \ Xampp \ htdocs \ admin \ config.php on line 122 Hinweis: Undefinierter Index: ID in D: \ Xampp \ htdocs \ admin \ slider_fetch_data.php in Zeile 29 – kulu

+0

Können Sie Ihre linkedin ID mit mir für die Diskussion über dieses Problem teilen. – kulu

+0

Sie erhalten Fehler, weil die Abfrage null zurückgibt. In der Funktion select_data_from_db ändern Sie: while ($ row = mysqli_fetch_array ($ result)) { $ result [] = $ row; }} –

0

Da auf Ihrem Class.php Sie den Lauf mit diesem Code

$this->run= new mysqli($this->host, $this->username, $this->password, $this->db_name); 

denken initialisiert i Sie brauchen nicht

public function select_data_from_db($table_name ,$run){} 

das Entfernen zweiter Parameter


sollte es sein, diese

public function select_data_from_db($table_name){} 

Dies ist der richtige Code (wenn ich nicht falsch bin und der mysqli Arbeits)

auf Class.php

class main{ 
    public $host="localhost"; 
    public $username="root"; 
    public $password=""; 
    public $db_name= "db_tvw"; 
    private $img_path = 'slider_img_upload/'; 

    public function __construct(){ 
     $this->run= new mysqli($this->host, $this->username, $this->password, $this->db_name); 
     if (mysqli_connect_errno()){ 
      echo "database connection is fail"; 
      exit; 
     } 
    } 

    public function select_data_from_db($table_name){ 
     $stmt=$this->run->prepare("SELECT * FROM ".$table_name); 
     $stmt->execute(); 
     $result = $stmt->get_result(); 
     $total_count=$result->num_rows; 
     $result= array(); 
     if($total_count>0){ 
      while ($row = mysqli_fetch_array($query)) { 
       $result[] = $row; 
      } 
     } 
     return $result; 
    } 
... 

auf index.php

<?php $myrow=$obj->select_data_from_db("home_slider"); ?> 
<tr> 
    <td><?php echo $myrow['id']; ?> </td> 
    <td><?php echo $myrow['title']; ?> </td> 
    <td><?php echo $myrow['description']; ?> </td> 
</tr>