codeigniter-data not retrieve from model - php

I have successfully made a soap server using codeigniter. i want pass an array from model to controller. but i only get null. here my code:
Model:
$this->db->select('player_item, count(*) as total');
$this->db->from('rps');
$this->db->group_by('player_item');
$query = $this->db->get();
$rows = array();
foreach ($query->result_array() as $row)
{
$rows[] = $row;
}
return $rows;
Soap server:
function get_player(){
$CI =& get_instance();
$CI->load->model("player");
$data['player']=$CI->player ->get_player();
return $data;
}
Soap client:
$result = $this->nusoap_client ->call('get_player');
print_r($result);
i don't know my code at server correct or not. i am just new to SOAP..

try to write below of this line
$data['player']=$CI->player ->get_player();
echo '<pre>';
print_r($data);
echo '</pre>';
die;
and check weather you are getting data or not .if you are not getting data then var_dump($this->db->last_query());
and you can also check in model that you are getting data from database or not.
$this->db->select('player_item, count(*) as total');
$this->db->from('rps');
$this->db->group_by('player_item');
$query = $this->db->get();
$rows = array();
foreach ($query->result_array() as $row)
{
$rows[] = $row;
}
echo '<pre>';
print_r($rows);
echo '</pre>';
die;
return $rows;

Add second parameter in select('fields', false) function false to skip automatic apostrophe on fields, CI automatic add apostrophe on mysql query fields, so when you use mysql function like count(*) then it will give you mysql error, your query will look like
SELECT `player_item`, `count(*)` as `total` ......
changes
$this->db->select('player_item, count(*) as total');
to
$this->db->select('`player_item`, count(*) as `total`', false);

Related

magento database sql not working

So do not have an idea why this function is not working? i am trying to select all the ids from the table but nothing is selected.
public function jobsArray()
{
$connection = Mage::getSingleton('core/resource')->getConnection('Envato_CustomConfig_Job');
$result = $connection->fetchAll("SELECT id FROM Envato_CustomConfig_Job");
$rows = array();
foreach($result as $record) {
$rows = ('value'=>$record, 'label'=>$record);
}
return $rows;
}
this function below works fine, I need the function above to do the same as teh function below.
public function toOptionArray()
{
return array(
array('value'=>1, 'label'=>'one'),
array('value'=>2, 'label'=>'Two'),
array('value'=>3, 'label'=>'Three'),
array('value'=>4, 'label'=>'Four')
);
}
There are a couple of issues with your code:
You're only selecting a single item (id, but later, I assume you're expecting an ID and a value).
$result = $connection->fetchAll("SELECT id FROM Envato_CustomConfig_Job");
record is an array from your SQL query, so you should be treating it as such. eg. $record['id']
$rows you want as an array, but you're overwriting it each time, so $rows[] = makes more sense
Something like:
public function jobsArray()
{
$connection = Mage::getSingleton('core/resource')->getConnection('Envato_CustomConfig_Job');
$result = $connection->fetchAll("SELECT id, label FROM Envato_CustomConfig_Job");
$rows = array();
foreach($result as $record) {
$rows[] = array('value'=>$record['id'], 'label'=>$record['label']);
}
return $rows;
}
Try using the core read/write resource. Change
$connection = Mage::getSingleton('core/resource')->getConnection('Envato_CustomConfig_Job');
To
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');

Output multiple rows in json in Zend Framework 1

I've searched and can't find an answer to my question.
I have the following code which is to loop through an array and then fetch back results for the different $id's.
The output when using echo json_encode($row); returns all results but the zend layout displays.
However when using $this->_helper->json($row,true); the layout doesn't display but only one result returns.
How can I return more than one result?
Any help would be much appreciated.
public function testAction()
{
//Get latest revision from database and loop through $id's
$id = array('308', '307', '306');
//Connect to database
foreach($id as $lId) {
$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select('')
->from('LinktagRevisions')
->where('linktagId = ?', $lId)
->order('updated DESC')
->limit(1);
$stmt = $select->query();
while ($row = $stmt->fetch()) {
$this->_helper->json($row,true);
//Encode as json and echo result
// echo json_encode($row);
}
}
}
I think you can try this:
$result = array();
foreach($id as $lId) {
....
$stmt = $select->query();
$result[$lId] = $stmt->fetchAll();
}
$this->_helper->json($result,true);

select all and some attribute from a table get different result in codeigniter

I've try to get result from sql query in codeigniter with mode select all attribute but it returns a PHP error but when I specify some attribute in give the right answer but the query is too long to be written.
This 2 mode select that I've try and give different result.
First
class model_kemiskinan extends CI_Model {
..... //constructor here
function get_kemiskinan_provinsi(){
$this->tahun = "2011";
$this->kodeProv = "31";
$this->query = "select * from kemiskinan where id_provinsi = ".$this->kodeProv." and tahun = ".$this->tahun;
$this->result = $this->db->query($this->query);
return $this->result->result();
}
Then the controlller pass it
public function testquery(){
$this->load->model('model_kemiskinan');
$data['hasil'] = $this->model_kemiskinan->get_kemiskinan_provinsi();
$data['main_content'] = 'test';
$this->load->view('template', $data);
}
and the view 'test' respond it with these code:
if(is_array($hasil)){
foreach ($hasil as $baris ) {
echo $baris->tahun;
echo $baris->id_provinsi;
echo "<br/>";
}
And the result is this
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$tahun
Second
But if I change the select mode that look like this :
$this->query = "select tahun, id_provinsi from kemiskinan where id_provinsi = ".$this->kodeProv." and tahun = ".$this->tahun;
It will work properly
Is there any solution about select all mode?
-Thanks before-
like the documentation (http://ellislab.com/codeigniter/user-guide/database/examples.html) says:
$query = $this->db->query('SELECT name, title, email FROM my_table');
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->email;
}
echo 'Total Results: ' . $query->num_rows();
you get only one row by calling result() so you need to call it by foreach:
function get_kemiskinan_provinsi(){
$this->tahun = "2011";
$this->kodeProv = "31";
$this->query = "select * from kemiskinan where id_provinsi = ".$this->kodeProv." and tahun = ".$this->tahun;
$this->result = $this->db->query($this->query);
$res = array();
foreach ($this->result->result() as $row)
{
$res[] = $row;
}
return $res;
}
Please recognize that the fields in the result object are case sensitive! If you have a field named "Tahun" in your db then "select tahun ..." will work in mysql and will give you an object where you can access $res->tahun.
If you do "select * ...." then you can only access it like this: $res->Tahun (with capial T)

How do I pass multiple queried results to new a query

I have succeeded in querying one table to get information that is needed to query another table (If you can see a better way I would be grateful!)
My question is: How can I have multiple values come back from the first query and have my second query come back with multiple results.
As you can see I am inserting the returned result from query one into query two “msg_id = ?”(I use '$datas' to fill the ‘?’) but if my results from query one have multiple values then how will this work?
Also how can I make it get multiple results from query one? at the moment if there are multiple values in mysql it only grabs the first one it reads.
My MODEL code is as follows:
function check() {
$this->db->select('msgto_message');
$this->db->from('msgto');
$this->db->where('msgto_display', 'y');
$this->db->where('msgto_recipient', '1');
$w = $this->db->get();
if ($w->num_rows() > 0) {
$rowe = $w->row_array();
$datas = $rowe['msgto_message'];
}
$sql = "SELECT msg_content FROM msg WHERE msg_id = ?";
$data = $this->db->query($sql, $datas) or die(mysql_error());
if ($data->num_rows() > 0) {
foreach($data->result_array() as $row) {
$data = $row;
}
return $data;
}
}
My CONTROLLER code is as follows:
function index() {
$this->load->model('data_model');
$data['rows'] = $this->data_model->check();
$this->load->view('home', $data);
}
Thank you anyone that helps me, I greatly appreciate it!
You may find a database join helpful here. While I'm not entirely sure what you're trying to do here (especially in that foreach loop!), something like this might get you going in a more efficient direction:
function check() {
$this->db->select('msg.msg_content');
$this->db->from('msgto');
$this->db->join('msg', 'msgto.msgto_message = msg.msg_id');
$this->db->where('msgto.msgto_display', 'y');
$this->db->where('msgto.msgto_recipient', '1');
$data = $this->db->get();
if ($data->num_rows() > 0) {
return $data->result_array();
}
}
This asks the database to join together the msg and msgto tables based on msgto_message matching msg_id, and can use the WHERE critera on msgto while returning the results from msg.
Right not you are getting only the first row from query one
if ($w->num_rows() > 0) {
$rowe = $w->row_array();
$datas = $rowe['msgto_message'];
}
To get all the records you need to cycle through the result
if ($w->num_rows() > 0) {
foreach($rowe = $w->row_array() as $datas) {
$datas = $rowe['msgto_message'];
$sql = "SELECT msg_content FROM msg WHERE msg_id = ?";
$data = $this->db->query($sql, $datas) or die(mysql_error());
if ($data->num_rows() > 0) {
foreach($data->result_array() as $row) {
$data = $row;
}
return $data;
}
}
}

PHP: reusing database class

I built a class that allows me to do:
$db->query($query);
and it works perfectly, although if I want to do:
$db->query($query);
while($row = $db->fetch_assoc()){
$db->query($anotherquery);
echo $db->result();
}
it "breaks" the class. I don't want to constantly have to redeclare my class (eg: $seconddb = new database()), is there a way to get around this? I want to be able to reuse $db within $db, without overwriting the "outside" db. currently I'm create an array of data (from db->fetch_assoc() then doing a foreach and then doing the db call inside that:
$db->query('SELECT * FROM table');
while($row = $db->fetch_assoc()){
$arr[] = $row;
}
foreach($arr as $a){
$db->query(); // query and processing here
}
Is this the best method or am I missing the obvious? Should I consider passing a connection link ID with the database connection?
Have $db->query() return a "query" object which you can then iterate for the query results.
You should consider having the $db->query() method return a result so you don't need to save the states in the result.
Ex.
// psaudo
class DB{
function query($sql){
$db->query('SELECT * FROM table');
$arr = array();
while($row = $db->fetch_assoc()){
$arr[] = $row;
}
return $arr;
}
}
But the way, having a nested query loop like your example may be very slow and not scale up.
You should use
$query = $this->db->query("YOUR QUERY");
foreach ($query->result_array() as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}

Categories