I have a simple class that is not outputting:
class ICRR extends Profile {
public $recommendation_text;
public $comments;
public $men_id;
public $recommendation;
public $uid;
public function __construct($uid)
{
include 'con.php';
$stmt = $conn->prepare(
'SELECT * FROM icrr WHERE uid = :uid');
$stmt->execute(array(':uid' => $uid));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$this->men_id = $row['men_id'];
$this->uid = $row['uid'];
$this->nid = $row['nid'];
$this->recommendation = $row['reccomendation'];
$this->comments = $row['comments'];
$this->date_submitted = $row['date_submitted'];
}
}
public function recommendation_text() {
switch ($this->recommendation){
case 1 :
$this->recommendation = 'Yes Certify';
break;
case 2 :
$this->recommendation = 'No Do Not Certify';
break;
case 3 :
$this->recommendation = 'Certify Pending Requirements';
break;
default : echo "Recommendation Not Received";
}
return $this->recommendation_text;
}
}
When I ask:
$ICRR = new ICRR($uid);
var_export($ICRR);
Everything is null. I checked the query and it is returning the data fine... I used a class like this before and it worked fine so I think I am missing something I just cant see..
Related
i am setting up my client controller , ( case: connect and connected) the select works with my primary key example select($key)
but the select with attributes wont example($login,$password)
even if put correct login and password i get redirected to the login form which
i ive tried changing the sql request and the form inputs
a part of model.php
public function select($cle_primaire) {
** $sql = "SELECT * from ".static::$table." WHERE ".static::$primary."=:cle_primaire";**
$req_prep = Model::$pdo->prepare($sql);
$req_prep->bindParam(":cle_primaire", $cle_primaire);
$req_prep->execute();
$req_prep->setFetchMode(PDO::FETCH_CLASS, 'Model'.ucfirst(static::$table));
if ($req_prep->rowCount()==0){
return null;
die();
}else{
$rslt = $req_prep->fetch();
return $rslt;
}
}
public function connexion($lg,$pw) {
** $sql = "SELECT * from ".static::$table." WHERE ".static::$login."=:lg AND".static::$password."=:pw";**
$req_prep = Model::$pdo->prepare($sql);
$req_prep->bindParam(":lg", $lg);
$req_prep->bindParam(":pw", $pw);
$req_prep->execute();
$req_prep->setFetchMode(PDO::FETCH_CLASS, 'Model'.ucfirst(static::$table));
if ($req_prep->rowCount()==0){
return null;
die();
}else{
$rslt = $req_prep->fetch();
return $rslt;
}
}
a part of modelclient.php
require_once ("Model.php");
class ModelClient extends Model{
private $cin;
private $clientname;
private $clientemail;
private $clientpw;
private $tel;
private $adress;
protected static $table = 'client';
protected static $primary = 'cin';
protected static $login = 'clientemail';
protected static $password = 'clientpw';
a part of controllerclient
case "connect":
$pagetitle = "login ";
$view = "login";
require ("{$ROOT}{$DS}view{$DS}view.php");
break;
case"connected":
if(isset($_REQUEST['lg']) && isset($_REQUEST['pw'])){
$lg = $_REQUEST["lg"];
$pw = md5($_REQUEST["pw"]);
$cnx=ModelClient::connexion($lg,$pw);
}
if($cnx!=null){
$pagetitle = "logged in ";
$view = "interface";
require ("{$ROOT}{$DS}view{$DS}view.php");
}
else
$pagetitle = "login ";
$view="login";
require ("{$ROOT}{$DS}view{$DS}view.php");
break;
running out of time
All is in the title. why I only get the first line of my table and not all the fields.
Function :
class Services_Label extends Services_Abstract
{
public function getlibelle()
{
$sRequest = 'SELECT NAME FROM menu WHERE id_application = 2';
$this->executeQueries($sRequest);
$aResult = $this->getOneResult();
$this->freeStatement();
return $aResult;
}
}
function application :
$oMessage = new Services_Label();
$toto = $oMessage->getlibelle();
var_dump($toto);
answer :
string(15) "Prise en charge"
table :
Going by the name of the getOneResult(), your resultset is probably being limited to one result.
Can you post the whole function to confirm?
I have no idea from your question what your table columns are called, but if you want all of the columns use SELECT *
class Services_Label extends Services_Abstract
{
public function getlibelle()
{
$sRequest = 'SELECT * FROM menu WHERE id_application = 2';
$this->executeQueries($sRequest);
$aResult = $this->getOneResult();
$this->freeStatement();
return $aResult;
}
}
It is most often better to actually name only the columns you actually want returned so you would do that like this
class Services_Label extends Services_Abstract
{
public function getlibelle()
{
$sRequest = 'SELECT NAME,COL2,COL3 FROM menu WHERE id_application = 2';
$this->executeQueries($sRequest);
$aResult = $this->getOneResult();
$this->freeStatement();
return $aResult;
}
}
this is the function getoneresult()
public function getOneResult()
{
$row = $this->getNextRow(OCI_BOTH | OCI_RETURN_NULLS);
if (is_array($row)) {
return $row[0];
} else {
return false;
}
}
You need to collect the rows using a while loop. So change your function 'getOneResult' to something like this.
public function getResults()
{
$array = array();
while ($row = $this->getNextRow(OCI_BOTH | OCI_RETURN_NULLS)) {
$array[] = $row;
}
return $array;
}
Hello I am new to OOP and I have a class with a method and I am trying to return a variable to the class from a method but I am obviously missing something...
Here is my Class:
class Certification {
public $uid;
public $men_id;
public $sm_id;
public $sm_rec;
public $three_days;
public $min_on_test;
public $signed;
public $mid_year;
public $police_check;
public $io_comments;
public $date_certified;
public function __construct($uid)
{
include 'conn.php';
$stmt = $conn->prepare(
'SELECT * FROM certification WHERE uid = :uid');
$stmt->execute(array(':uid' => $uid));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$this->uid = $row['uid'];
$this->nid = $row['nid'];
$this->nid = $row['date_certified'];
$this->men_id = $row['men_id'];
$this->sm_id = $row['sm_id'];
$this->sm_rec = $row['sm_rec'];
$this->three_days = $row['three_days'];
$this->min_on_test = $row['min_on_test'];
$this->signed = $row['signed'];
//$this->mid_year = $row['mid_year'];
$this->police_check = $row['police_check'];
$this->io_comments = $row['io_comments'];
}
}
public function get_mid_year ($uid) {
include 'conn.php';
$stmt = $conn->prepare(
'SELECT courseReviewed FROM vti_users WHERE uid = :uid');
$stmt->execute(array(':uid' => $uid));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$this->mid_year = $row['courseReviewed'];
return $this->mid_year;
}
}
}
I thought that this method would return the $mid_year variable to the class but is null when I use var_dump().
Any advice about correcting my flawed logic would be a great help for a beginner.
I have made a class function which will get rows from a table in my database along with an optional argument. This works when getting a single row however I cant get this to work when multiple rows are returned.
Here is what is in the Users class
public function getUsers($filter="") {
$Database = new Database();
if($filter == 'male')
$extra = "WHERE gender = 'm'";
$sql = "SELECT *
FROM users
$extra";
if ($Database->query($sql))
return $Database->result->fetch_assoc();
else
return false;
}
Database Class
class Database {
private $db = array();
private $connection;
private $result;
public function __construct() {
$this->connect();
}
public function connect() {
$this->connection = mysqli_connect('mysql.com', 'username', 'pass');
mysqli_select_db($this->connection, 'database');
}
public function query($sql) {
$this->result = mysqli_query($this->connection, $sql);
return $this->result;
}
This is the code used to try and display the rows
if ($student = $User->getUsers($filter)) {
echo "<table>\n";
echo "<tr><td>Col 1</td><td>Col 2</td><td>Col 3</td><td>Col 4</td><td></td><td></td></tr>";
foreach($student as $row) {
echo "<tr>";
echo "<td>$row[col1]</td>";
echo "<td>$row[col2]</td>";
echo "<td>$row[col3]</td>";
echo "<td>$row[col4]</td>";
echo "<td>$row[col5]</td>";
echo "<td>$row[col6]</td>";
echo "</tr>\n";
}
echo "</table>";
}
(I'm learning OO PHP, bear with me)
I'm still learning OOP but I have used this:
protected $connection, $result, $_numRows;
public function query($sql)
{
$this->result = mysql_query($sql, $this->connection);
$this->_numRows = mysql_num_rows($this->result);
}
/*
* #desc get result af query
*
* #returns string $result
*/
public function getResult()
{
return $this->result;
}
/*
* #desc Return rows in table
* #returns int $_numRows
*/
public function numRows()
{
return $this->_numRows;
}
/*
* #desc Count rows and add to array
* #return string $rows array
*/
public function rows()
{
$rows =array();
for ($x=0; $x < $this->numRows(); $x++) {
$rows[] = mysql_fetch_assoc($this->result);
}
return $rows;
}
Then you could use something like this to get the rows:
public function getUsers($filter="") {
$Database = new Database();
if($filter == 'male')
$extra = "WHERE gender = 'm'";
$sql = "SELECT *
FROM users
$extra";
if ($this->numRows() == 0) {
echo "No rows found";
} else {
foreach ($this->rows() as $b) {
$c = array('something' => $b['col']);
}
return $c;
}
Modify the last part to suit your needs.
It's not clear in your code where $result comes from...
You should call the fetch_assoc() method of an object returned by:
$mysqli->query($query)
Why are you using $result->fetch_assoc() twice? you are returning an array.. you should deal with $student.
why my code is not working here I think I made something wrong but I just can not determine it if anyone can spot what I have made wrong here I will be thankfull
here is my code: I am trying here to use php class to insert new row in the database
<?php
$connection = new PDO('mysql:dbname=master;host=localhost','root','123');
/*if($connection)
{
echo 'Database is connected successfully';
}
else
{
echo 'please connect to a database';
}*/
class Topics
{
public $id;
public $title;
public $body;
public static $table_name = 'topics';
public static $fields = array ('title','body');
private function attributes()
{
$string = array();
foreach (self::$fields as $field)
{
if (!empty($field)) {
if(is_int($this->$field))
{
$string[] = $field." =".$this->$field;
}
else
{
$string[] = $field." ="."'".$this->$field."'";
}
}
}
return join(',', $string);
}
public function add()
{
global $connection;
$sql = 'INSERT INTO'.self::$table_name.'SET'.$this->attributes();
$number_of_affected_rows = $connection->exec($sql);
if($number_of_affected_rows >0)
{
$this->id = $connection->lastInsertId();
}
return ($number_of_affected_rows>0) ? $number_of_affected_rows : FALSE;
}
}
$mohamed = new Topics();
$mohamed->title = 'Hello';
$mohamed->body = 'Hello world again';
return $mohamed->add();
?>
What's not working exactly? Your SQL is wrong anyway, beacause it needs whitespaces in it.
'INSERT INTO '.self::$table_name.' SET '.$this->attributes();
And btw you should also escape table and field name in your SQL.