Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Fatal error: Call to undefined method stdClass::num_rows() in (...) line 36
    primarykey
    data
    text
    <p>Here my first question . I'm a newbie in PHP and CI but i want to learn :)</p> <p>First my model : </p> <blockquote> <p>class Login_model extends MY_Model {</p> <pre><code>protected $table = 'users'; protected $primary = 'user_id'; function __construct() { parent::__construct(); } public function validate() { // Récupère le POST du login $user_mail = $this-&gt;input-&gt;post('user_mail'); $password = sha1($this-&gt;input-&gt;post('user_mail') . $this-&gt;input-&gt;post('password')); //Execution de la requete $query = $this-&gt;users_m-&gt;get(array('user_mail' =&gt; $user_mail)); // Verification qu'il existe un enregistrement var_dump($query); if($query-&gt;num_rows() == 1) { // If there is a user, then create session data $row = $query-&gt;row(); $data = array( 'userid' =&gt; $row-&gt;userid, 'fname' =&gt; $row-&gt;fname, 'lname' =&gt; $row-&gt;lname, 'username' =&gt; $row-&gt;username, 'validated' =&gt; true ); $this-&gt;session-&gt;set_userdata($data); return true; // } // Si le login ne se valide pas // return false. return false; } } } </code></pre> </blockquote> <p>This model extends My_model MY_MODEL.PHP</p> <pre><code>&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /** * MY_Model */ class MY_Model extends CI_Model { /** * Connexion à la BDD * * @var object */ protected $link; /** * Groupe de connexion BDD * * @var string */ protected $db_group = 'default'; /** * Table de la BDD * * @var string */ protected $table = ''; /** * Clé(s) primaire(s) * * @var string/array */ protected $primary = 'id'; /** * Déclanche une exception * * @var boolean */ private $_throw_exception = TRUE; /** * Constructeur */ public function __construct() { parent::__construct(); if ($this-&gt;table != '') { $this-&gt;_set_link(); } } /** * Connexion à la BDD */ private function _set_link() { $this-&gt;link = $this-&gt;load-&gt;database($this-&gt;db_group, TRUE); } /** * Active la gestion des exceptions */ public function enable_exception() { $this-&gt;_throw_exception = TRUE; } /** * Desactive la gestion des exceptions */ public function disable_exception() { $this-&gt;_throw_exception = FALSE; } /** * Récupère un enregistrement unique * * @param array $where * @return boolean * @throws Exception */ public function get($where = array()) { if (is_array($where) &amp;&amp; $where) { foreach ($where as $key =&gt; $value) { $this-&gt;link-&gt;where($key, $value); } } $query = $this-&gt;link-&gt;get($this-&gt;table); /* Traitement de la requete */ $data = FALSE; if (is_object($query)) { if ($query-&gt;num_rows() == 1) { $result = $query-&gt;result(); $data = $result[0]; } else if ($this-&gt;_throw_exception) { throw new Exception($query-&gt;num_rows(), 11); } } else { throw new Exception($this-&gt;link-&gt;_error_number() . ":" . $this-&gt;link-&gt;_error_message(), 10); } return $data; } /** * Liste des enregistrements de la table * * @param array $orders * @return array */ public function get_all($where = array(), $orders = array(), $limit = NULL, $offset = NULL) { /* Conditions */ if (is_array($where) &amp;&amp; $where) { foreach ($where as $key =&gt; $value) { $this-&gt;link-&gt;where($key, $value); } } /* Tris */ if (is_array($orders) &amp;&amp; $orders) { foreach ($orders as $order) { $this-&gt;link-&gt;order_by("$this-&gt;table.$order"); } } else if ($orders) { $this-&gt;link-&gt;order_by("$this-&gt;table.$orders"); } else if (is_array($this-&gt;primary)) { foreach ($this-&gt;primary as $order) { $this-&gt;link-&gt;order_by("$this-&gt;table.$order"); } } else { $this-&gt;link-&gt;order_by("$this-&gt;table.$this-&gt;primary"); } /* Requete */ $query = $this-&gt;link-&gt;get($this-&gt;table, $limit, $offset); /* Traitement de la requete */ $data = array(); if (is_object($query)) { if ($query-&gt;num_rows() &gt; 0) { $data = $query-&gt;result(); } } else if ($this-&gt;_throw_exception) { throw new Exception($this-&gt;link-&gt;_error_number() . ":" . $this-&gt;link-&gt;_error_message(), 10); } /* Retour */ return $data; } /** * Nombre d'enregistrements de la table * * @return integer */ public function count($where = array()) { if (is_array($where) &amp;&amp; $where) { foreach ($where as $key =&gt; $value) { $this-&gt;link-&gt;where($key, $value); } } $this-&gt;link-&gt;from($this-&gt;table); return $this-&gt;link-&gt;count_all_results(); } /** * Insertion dans la BDD * * @param array $data * @return boolean * @throws Exception */ public function insert($data = array()) { $insert = FALSE; if ($this-&gt;link-&gt;insert($this-&gt;table, $data)) { if (is_array($this-&gt;primary)) { $insert = TRUE; } else if (isset($data[$this-&gt;primary])) { $insert = $data[$this-&gt;primary]; } else { $insert = $this-&gt;link-&gt;insert_id(); } } else if ($this-&gt;_throw_exception) { throw new Exception($this-&gt;link-&gt;_error_number() . ":" . $this-&gt;link-&gt;_error_message(), 20); } return $insert; } /** * Insertion de masse dans une base de donnée * * @param array $data * @return boolean * @throws Exception */ public function insert_batch($data) { $insert = FALSE; if ($this-&gt;link-&gt;insert_batch($this-&gt;table, $data)) { $insert = TRUE; } else if ($this-&gt;_throw_exception) { throw new Exception($this-&gt;link-&gt;_error_number() . ":" . $this-&gt;link-&gt;_error_message(), 20); } return $insert; } /** * Mise à jour d'enregistrements de la BDD * * @param array $data * @param array $where * @return boolean * @throws ExceptionModel */ public function update($data = array(), $where = array()) { $update = FALSE; if (is_array($where) &amp;&amp; $where) { foreach ($where as $key =&gt; $value) { $this-&gt;link-&gt;where($key, $value); } } if ($this-&gt;link-&gt;update($this-&gt;table, $data)) { $update = TRUE; } else if ($this-&gt;_throw_exception) { throw new Exception($this-&gt;link-&gt;_error_number() . ":" . $this-&gt;link-&gt;_error_message(), 30); } return $update; } /** * Suppression d'enregistrements de la BDD * * @param array $where * @return boolean * @throws Exception */ public function delete($where = array()) { $delete = FALSE; if (is_array($where) &amp;&amp; $where) { foreach ($where as $key =&gt; $value) { $this-&gt;link-&gt;where($key, $value); } } if ($this-&gt;link-&gt;delete($this-&gt;table)) { $delete = TRUE; } else if ($this-&gt;_throw_exception) { throw new Exception($this-&gt;link-&gt;_error_number() . ":" . $this-&gt;link-&gt;_error_message(), 40); } return $delete; } } /* End of file MY_Model.php */ /* Location: ./application/core/MY_Model.php */ </code></pre> <p>I have my controller : Accueil ( will be the default route )</p> <p>The method login() call the view login.php</p> <p>ACCUEIL.PHP : </p> <pre><code> &lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Accueil extends MY_Controller { public function __construct() { parent::__construct(); $this-&gt;load-&gt;model('users_model', 'users_m'); $this-&gt;load-&gt;library('form_validation'); $this-&gt;load-&gt;helper(array('form', 'url')); $this-&gt;load-&gt;library('template'); } public function index($msg = NULL) //msg en cas d'erreur , NULL si pas d'erreur { $data['msg'] = $msg; $this-&gt;load-&gt;view('/login', $data); } public function login() { // Chargement du model de login $this-&gt;load-&gt;model('login_model'); // Validation que l'user puisse se connecter $result = $this-&gt;login_model-&gt;validate(); // Verification du résultat if (!$result) { // Pas de validation on renvoit vers l'index $msg = '&lt;font color=red&gt;Nom d\'utilisateur ou mot de passe incorrect(s).&lt;/font&gt;&lt;br /&gt;'; $this-&gt;index($msg); } else { // Si validation ok, // On l'envoit vers la partie "protégée" redirect('home'); } } } </code></pre> <p>Finally the view : LOGIN.PHP</p> <pre><code> &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;connexion&lt;/title&gt; &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt; &lt;meta name="description" content=""&gt; &lt;meta name="author" content="kliklipse"&gt; &lt;!-- Le styles --&gt; &lt;link rel="stylesheet" href="&lt;?php echo css_url('bootstrap.min'); ?&gt;"&gt; &lt;style&gt; body { padding-top: 40px; padding-bottom: 40px; background-color: #f5f5f5; } .form-signin { max-width: 300px; padding: 19px 29px 29px; margin: 0 auto 20px; background-color: #fff; border: 1px solid #e5e5e5; -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05); -moz-box-shadow: 0 1px 2px rgba(0,0,0,.05); box-shadow: 0 1px 2px rgba(0,0,0,.05); } .form-signin .form-signin-heading, .form-signin .checkbox { margin-bottom: 10px; } .form-signin input[type="text"], .form-signin input[type="password"] { font-size: 16px; height: auto; margin-bottom: 15px; padding: 7px 9px; } &lt;/style&gt; &lt;link rel="stylesheet" href="&lt;?php echo css_url('bootstrap-responsive.min'); ?&gt;"&gt; &lt;link rel="stylesheet" href="&lt;?php echo css_url('main'); ?&gt;"&gt; &lt;!-- HTML5 shim, for IE6-8 support of HTML5 elements --&gt; &lt;!--[if lt IE 9]&gt; &lt;script src="../assets/js/html5shiv.js"&gt;&lt;/script&gt; &lt;![endif]--&gt; &lt;!-- Fav and touch icons --&gt; &lt;link rel="apple-touch-icon-precomposed" sizes="144x144" href="../assets/ico/apple-touch-icon-144-precomposed.png"&gt; &lt;link rel="apple-touch-icon-precomposed" sizes="114x114" href="../assets/ico/apple-touch-icon-114-precomposed.png"&gt; &lt;link rel="apple-touch-icon-precomposed" sizes="72x72" href="../assets/ico/apple-touch-icon-72-precomposed.png"&gt; &lt;link rel="apple-touch-icon-precomposed" href="../assets/ico/apple-touch-icon-57-precomposed.png"&gt; &lt;link rel="shortcut icon" href="../assets/ico/favicon.png"&gt; &lt;/head&gt; &lt;body&gt; &lt;div class="container"&gt; &lt;form class="form-signin" action="&lt;?php echo base_url();?&gt;accueil/login" method="post" name="process"&gt; &lt;h2 class="form-signin-heading"&gt;Identifiez vous&lt;/h2&gt; &lt;br /&gt; &lt;?php if(! is_null($msg)) echo $msg;?&gt; &lt;input type="text" name="user_mail" id="user_mail" class="input-block-level" placeholder="Adresse Email"&gt; &lt;input type="password" name="password" id="password" class="input-block-level" placeholder="Mot de passe"&gt; &lt;label class="checkbox"&gt; &lt;input type="checkbox" value="remember-me"&gt; Se souvenir de moi &lt;/label&gt; &lt;button class="btn btn-large btn-primary" type="submit"&gt;Se connecter&lt;/button&gt; &lt;/form&gt; &lt;/div&gt; &lt;!-- /container --&gt; &lt;div id="footer"&gt; &lt;div class="container-narrow"&gt; &lt;p&gt;&amp;copy; Kliklipse 2013 &lt;a href="&lt;?php echo base_url(); ?&gt;"&gt;Gestion des utilisateur &amp; groupes&lt;/a&gt;&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="&lt;?php echo js_url('bootstrap.min'); ?&gt;"&gt;&lt;/script&gt; &lt;script src="&lt;?php echo js_url('sorttable'); ?&gt;"&gt;&lt;/script&gt; &lt;script&gt;var base_url = &lt;?php echo base_url(); ?&gt;&lt;/script&gt; &lt;!-- {elapsed_time} seconds--&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>I get the error with num_rows() that its a undefined method... just dont know why..</p> <p>If any questions, ask me ( and sorry for my english ^^)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload