How do I pass multiple queried results to new a query - php

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;
}
}
}

Related

mySQL request not working

I'm not really much into PHP or mySQL so I hope you can help me.
I got a php script with a function that returns a json with all the entries in a database table:
public function select($table, $wheres = null)
{
$connect = $this->connect();
if ($wheres == null)
{
$query = mysqli_query($connect, 'SELECT * FROM `'.$table.'`');
} else {
$query = mysqli_query($connect, 'SELECT * FROM `'.$table.'` WHERE '.$this->wheres($wheres));
}
$i = 0;
$ret = array();
while ($row = mysqli_fetch_assoc($query)) {
foreach ($row as $key => $value) {
$ret[$i][$key] = $value;
}
$i++;
}
return ($ret);
}
Edit:
I have my application which calls this function. Everything worked as expected but then I added 2 more fields to one of the tables ( 1 text, 1 varchar ), and now when I call this function it returns nothing ; literally an empty string.
I've also noticed that if I just delete those new fields it works, which is kinda annoying plus I need those fields and I can't figure out where the problem root is.
Also, the application code is not the problem: when this function is called it only passes 2 parameters (the method name, and the table name), it can't be wrong.
By the way, here is the table structure if it may help you help me:
pic related
This function helps you to get data form database in json from , this return json
<?php
function getData($tbl_name=null,$id=null)
{
$con = mysqli_connect("localhost","root","","mydb"); //my db is my database name
if(!$con){ die( "could't connect with database" ); }
$sql = mysqli_query($con,'SELECT * FROM $tbl_name WHERE id = {"$id"}');
while($row = mysqli_fetch_assoc($sql))
{
#echo"<pre>";
#print_r($row); //this will return data form data base in array from
return json_encode($row);
}
}
?>

codeigniter-data not retrieve from model

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);

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);

What's wrong with this sql query and mysql_fetch_array?

These lines are from a php function on a web server, the client is an ios app,
I get an error on the result2 query
$result = query("SELECT field1 FROM table1 WHERE field2='%s' limit 1", $value);
$row = mysql_fetch_array($result);
$result2 = query("SELECT field2 FROM table1 WHERE field3='%s' limit 1", $row['field1']);
on Xcode I get the error (in json):
{
error = "The operation couldn't be completed. (Cocoa error 3840.)";
}
here is the definition of the function query
//executes a given sql query with the params and returns an array as result
function query() {
global $link;
$debug = false;
//get the sql query
$args = func_get_args();
$sql = array_shift($args);
//secure the input
for ($i=0;$i<count($args);$i++) {
$args[$i] = urldecode($args[$i]);
$args[$i] = mysqli_real_escape_string($link, $args[$i]);
}
//build the final query
$sql = vsprintf($sql, $args);
if ($debug) print $sql;
//execute and fetch the results
$result = mysqli_query($link, $sql);
if (mysqli_errno($link)==0 && $result) {
$rows = array();
if ($result!==true)
while ($d = mysqli_fetch_assoc($result)) {
array_push($rows,$d);
}
//return json
return array('result'=>$rows);
} else {
//error
return array('error'=>'Database error');
}
}
what's wrong with that query?
thanks
You are using mysqli_ functions in your query() function yet you are trying to use mysql_fetch_array() to get the results. You need to use: mysqli_fetch_array()
http://www.php.net/manual/en/mysqli-result.fetch-array.php
Actually it looks like your query() function does it for you. You shouldn't need to use a function at all. Look at this section of your query() function:
$rows = array();
if ($result!==true)
while ($d = mysqli_fetch_assoc($result)) {
array_push($rows,$d);
}
//return json
return array('result'=>$rows);
However not sure why it says it is json cause it is just a normal array not a json array. So you should just use this in your code:
$row = $result['result'][0];
This will get the first row.
maybe you have to json_encode the result?
//return json
return json_encode(array('result'=>$rows));

How to send the my sql result to controller in codeigniter as a variable

In my mode I am selecting a field as
$query1 = $this->db->query("SELECT dPassword
FROM tbl_login
WHERE dEmailID='a#a.in'");
How to return dpassword as a variable to my controller
I tried this way return dpassword;
Check out the Query Results section of the CI manual. $query1 is set to a mysql resource from the query you executed. You then need to call additional functions to get the data
http://codeigniter.com/user_guide/database/results.html
$dataArray=$query1->result_array();
return $dataArray["dPassword"];
The following is also fine:
if($query1->num_rows() > 0){
$row = $query1->row();
}
return $row->dPassword;
Then if your query was to return more than a single row you could operate on the results like so:
foreach($query1->result() as $row){
echo $row->field1;
echo $row->field2;
echo $row->etcetera;
}
For single row results i usually return the row directly from the model like so:
return $query1->row();
Here is an example of this:
function select_provider_details($provider_id)
{
$this->db->select('*');
$this->db->from('providers');
$this->db->where('provider_id', $provider_id);
$query = $this->db->get();
if($query->num_rows() > 0)
{
$result['success'] = TRUE;
$result['query'] = $query->row();
}
else
{
$result['success'] = FALSE;
$result['error'] = "Provider not found in database";
$result['errorcode'] = "E003";
$result['query'] = $query->row();
}
return $result;
}
Or for a query expected to return multiple results i return the entire results object:
return $query1;
Maybe you can use tray this.
$query1 = $this->db->query("SELECT dPassword
FROM tbl_login
WHERE dEmailID='a#a.in'");
if($query1){
// if you are working with objects
return $query1->result();
// if you are working with arrays try
return $query1->result_array();
}else{
return false;
}

Categories