Load specific data from array - php

So i have this class for loading data from my MySQL database :
class Db {
protected static $connection;
public function connect() {
if(!isset(static::$connection)) {
$config = parse_ini_file('config.ini');
static::$connection = new mysqli('localhost',$config['username'],$config['password'],$config['dbname']);
}
if(static::$connection === false) {
return false;
}
return static::$connection;
}
public function query($query) {
return $this->connect()->query($query);
}
public function select($query) {
$rows = array();
$result = $this->query($query);
if($result === false) {
return false;
}
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
public function error() {
return $this->connect()->error;
}
public function quote($value) {
return "'" . $this->connect()->real_escape_string($value) . "'";
}
}
I use that class like this :
$db = new Db();
$rows = $db -> select("SELECT name, shortlink FROM `test` WHERE id=3");
It gives me an array with the data.
The problem is that i want to pull out specific data, for example the shortlink field.
How do i do that? I have tried echo $rows['shortlink'], but that gives me the following error :
Undefined index: shortlink
So how do I print specific data?

Your returned $rows columns is an array of associative arrays, to pull out shortlink data returned from the query you have to do something like this:
foreach($rows as $row) {
echo $row['shortlink'];
}

Related

PHP object is not being created

I have received the following error:"Fatal error: Call to a member function session_type() on a non-object".
Supposedly I'm not creating an object in the variable "$tabsessiontype2". I don't see the reason why the object isn't created. Thanks for you help.
Here is my TeacherController.php
if(isset($_POST['search']) && $_POST['sessiontype_name']){
$tabsessiontype2=Db::getInstance()->select_session_type2($_POST['sessiontype_name']);
if($tabsessiontype2->session_type()=='X'){
$view='teacher.php';
}
elseif($tabsessiontype2->session_type()=='XO'){
$view='teacherxo.php';
}
elseif($tabsessiontype2->session_type()=='note'){
$view='teachernote.php';
}
}
This is the function I call:
public function select_session_type2($name_session_type) {
$query = "SELECT * FROM session_types WHERE session_types.name_session_type='$name_session_type'";
$result = $this->_db->query($query);
$tableau = array();
if ($result->rowcount()!=0) {
while ($row = $result->fetch()) {
$tableau[] = new Sessiontype($row->code_session_type,$row->name_session_type,$row->quarter_session_type,$row->code_lesson,$row->session_type);
}
}
return $tableau;
}
Here is the SessionType class:
class SessionType{
private $_code_session_type;
private $_name_session_type;
private $_quarter_session_type;
private $_code_lesson;
private $_session_type;
public function __construct($code_session_type,$name_session_type, $quarter_session_type, $code_lesson,$session_type){
$this->_code_session_type = $code_session_type;
$this->_name_session_type = $name_session_type;
$this->_quarter_session_type = $quarter_session_type;
$this->_code_lesson = $code_lesson;
$this->_session_type = $session_type;
}
public function code_session_type(){
return $this->_code_session_type;
}
public function name_session_type(){
return $this->_name_session_type;
}
public function quarter_session_type(){
return $this->_lastname;
}
public function code_lesson(){
return $this->_email_student;
}
public function session_type(){
return $this->_session_type;
}
}
It looks like the function select_session_type2 is returning an array:
$tableau = array();
if ($result->rowcount()!=0) {
while ($row = $result->fetch()) {
$tableau[] = new Sessiontype($row->code_session_type,$row->name_session_type,$row->quarter_session_type,$row->code_lesson,$row->session_type);
}
}
return $tableau;
and you are expecting an object:
$tabsessiontype2=Db::getInstance()->select_session_type2($_POST['sessiontype_name']);
if($tabsessiontype2->session_type()=='X'){
$view='teacher.php';
}

Functions with `$this` don't work in Base class PHP

I have recently started working with OO PHP and I've come across an error.
I have a base class BaseController.php
and a class DatabaseController.php which extends BaseController.php
At the moment these classes are pretty basic but I want a few reusable functions in Base and overall this has been successful.
However, I am trying to use functions in the Base Controller together and it seems functions with $this->functionName(); don't return anything.
Here is my code to further explain:
Base Controller
public function connectDB() {
$conn = mysqli_connect("localhost", "root", "", "db_name");
return $conn;
}
public function getSource($id) {
$conn = $this->connectDB();
$sql = "SELECT * FROM sources WHERE id = '$id'";
$query = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($query)) {
$name = $row['slug'];
}
return $name;
}
public function mapColumnsToFunctions($row) {
foreach($row as $key => $value) {
if($key == "source") {
$result = $this->getSource($value);
$row["source_slug"] = $result;
}
return $row;
}
}
Database Controller
class DatabaseController extends BaseController {
public function getDataItem($type, $id) {
$conn = $this->connectDB();
$id = $this->checkField($id, "");
$sql = "SELECT * FROM $type WHERE id = '$id'";
$query = mysqli_query($conn, $sql);
$response = array();
while($row = mysqli_fetch_assoc($query)) {
$response[$type][] = $this->mapColumnsToFunctions($row);
}
$response = json_encode($response);
return $response;
}
}
I should be seeing a JSON response with source_slug as a field but this isn't coming through to the feed.
{
"id": "213023",
"name": "Source Name",
"url": "http://sample.url",
"source": "1"
// NO source_slug: "english equiv of 1"
},
mapColumnsToFunctions has a return statement inside the foreach loop. So it returns immediately during the first iteration, even if it hasn't updated anything in $row.
The return statement should be after the loop.
public function mapColumnsToFunctions($row) {
foreach($row as $key => $value) {
if($key == "source") {
$result = $this->getSource($value);
$row["source_slug"] = $result;
}
}
return $row;
}
The loop seems unnecessary (unless it's an abbreviation of what you're really doing). It could be rewritten as:
public function mapColumnsToFunctions($row) {
if (isset($row["source"])) {
$row["slug_source"] = $this->getSource($row["source"]);
}
return $row;
}

Sending a function to library in Codeigniter

This works in my controller.
$this->load->driver('cache');
//working
// $data['advisory']=$advisory = $this->cache->memcached->get('advisory');
// if(! $advisory)
// {
// $data['advisory']=$advisory = $this->Mhomework->getbyadvisory($this->teacherid);
// $this->cache->memcached->save('advisory' , $advisory);
// }
I made a library to make for this as following so that I can use it for others.
function cache($key, $data)
{
$this->CI->load->driver('cache');
$cache = $this->CI->cache->memcached->get($key);
if (!$cache) {
// There's been a miss, so run our data function and store it
$cache = $data($CI);
//$cache = $data;
$this->CI->cache->memcached->save($key, $cache);
}
return $cache;
}
And in controller I changed to the following which gives an error.
$data['advisory'] = $this->hwtracker->cache('advisory.'.$this->teacherid, function(&$CI){
return $CI->Mhomework->getbyadvisory($this->teacherid);
});
// error Call to a member function getbyadvisory() on a non-object in ... controller
// Message: Trying to get property of non-object
My question is how I can send function to my library in CodeIgniter? Or Can I?
Update:    
function getbyadvisory($id){
  $Q="SELECT *, studentid, COUNT(studentid),be_user_profiles.first_name,   
       be_user_profiles.last_name
FROM be_user_profiles
LEFT JOIN hw_homework
ON be_user_profiles.user_id= hw_homework.studentid
WHERE be_user_profiles.advisor = $id
GROUP BY be_user_profiles.user_id
ORDER BY COUNT(studentid) DESC";
$query = $this->db->query($Q);
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$data[] = $row;
}
}
else
{
$data = FALSE;
}
$query->free_result();
return $data;
}
Please change your library to be like this
function cache($key, $data)
{
$this->CI->load->driver('cache');
//bind your key here
$key_details = $data.$key;
$cache = $this->CI->cache->memcached->get($key_details);
if (!$cache) {
// There's been a miss, so run our data function and store it
$cache = $this->$data($key);
//$cache = $data;
$this->CI->cache->memcached->save($key, $cache);
}
return $cache;
}
//new advisory function in library
function advisory($teacherid){
return $this->CI->Mhomework->getbyadvisory($teacherid);
}
And in controller call like this
$data['advisory'] = $this->hwtracker->cache($this->teacherid,'advisory');

Fetching rows using PHP OO Class

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.

PHP OOP: How to get database rows as objects?

I can do this when selecting a single row fine but cant quite get my head around doing this for multiple rows of data.
For the single row I simply instantiate a new object that does a number of operations behind the scenes that bascially produces a row from the database as our object.
Example:
$object = new Classname($param);
foreach($object->row as $key=>$value) {
echo $key.":".$value."\n";
}
//output
id:1
firstname:steve
lastname:took
etc...
Any clever people here able to point me in the right direction please?
NOTE: just want to be able to create an object for each row rather than the one object with nested arrays
EDIT: sorry $object->row is a member of the class that stores selected row from the database
If I got you the answer is pretty simple mysql_fetch_object
Example:
while ($row = mysql_fetch_object($result)) {
echo $row->user_id;
echo $row->fullname;
}
Why don't you consider to use an ORM (Object Relational Mapper), like Doctrine or Propel?
I prefer Doctrine: http://www.doctrine-project.org/
Enjoy! :)
Here is an example
class users extends db {
public $users_id = 0;
public $users_group = 0;
public $users_firstname = 0;
public $users_lastname = 0;
public function open($id) {
if (is_numeric($id)) {
$sql = "SELECT * FROM users WHERE users_id = '$id'";
$res = parent::DB_SELECT($sql);
if (mysql_num_rows($res) <> 1) {
return 0;
}
} else {
$sql = "SELECT * FROM users " . $id;
$res = parent::DB_SELECT($sql);
if (mysql_num_rows($res) <= 0) {
return 0;
}
}
$data = array();
while ($row = mysql_fetch_array($res)) {
$this->users_id = $row['users_id'];
$this->users_group = $row['users_group'];
$this->users_firstname = $row['users_firstname'];
$this->users_lastname = $row['users_lastname'];
$data[] = (array) $this;
}
return $data;
}
public function setUsersId($users_id) { $this->users_id = addslashes($users_id); }
public function getUsersId() { return stripslashes($this->users_id); }
public function setUsersGroup($users_group) { $this->users_group = addslashes($users_group); }
public function getUsersGroup() { return stripslashes($this->users_group); }
public function setUsersFirstname($users_firstname) { $this->users_firstname = addslashes($users_firstname); }
public function getUsersFirstname() { return stripslashes($this->users_firstname); }
public function setUsersLastname($users_lastname) { $this->users_lastname = addslashes($users_lastname); }
public function getUsersLastname() { return stripslashes($this->users_lastname); }
}
You could use MySQLi.
// Connect
$db = new mysqli('host', 'user', 'password', 'db');
// Query
$users = $db->query('SELECT * from users');
// Loop
while($user = $users->fetch_object()) {
echo $users->field;
}
// Close
$users->close();
$db->close();
More info here: http://php.net/manual/en/book.mysqli.php

Categories