PDO second fetchall() returns empty array - mysql

I try to execute multiple queries in one php file.
The first query works fine, but the second en third one return an empty array.
$DB_con = new PDO($dsn, $DB_user, $DB_pass, array(
PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION
));
$sql_sel_sponsoring = $DB_con->prepare('CALL SP_SELECT_SPONSORING(:id)');
$sql_sel_sponsoring->execute(array(':id' => $sponsor));
$sql_res_sponsoring = $sql_sel_sponsoring->fetchAll();
$sql_sel_sponsoring_type = $DB_con->prepare('CALL SP_SELECT_SPONSORING_TYPE()');
$sql_sel_sponsoring_type->execute();
$sql_res_sponsoring_type = $sql_sel_sponsoring_type->fetchAll();
$sql_sel_cbo_members = $DB_con->prepare('CALL SP_SELECT_CBO_MEMBERS()');
$sql_sel_cbo_members->execute();
$sql_res_cbo_members = $sql_sel_cbo_members->fetchAll();
With a vardump() I have different objects
var_dump($sql_sel_sponsoring);
var_dump($sql_sel_sponsoring_type);
var_dump($sql_sel_cbo_members);
Results
object(PDOStatement)#6 (1) { ["queryString"]=> string(30) "CALL SP_SELECT_SPONSORING(:id)" }
object(PDOStatement)#7 (1) { ["queryString"]=> string(32) "CALL SP_SELECT_SPONSORING_TYPE()" }
object(PDOStatement)#8 (1) { ["queryString"]=> string(28) "CALL SP_SELECT_CBO_MEMBERS()" }
But 2 of the 3 resultsets are empty
print_r($sql_res_sponsoring);
print_r($sql_res_sponsoring_type);
print_r($sql_res_cbo_members);
Giving:
Array ( [0] => Array ( [SID_SPONSORING] => 70 [0] => 70 [SID_SPONSOR] => 88 [1] => 88 [Jaar] => 2014 [2] => 2014 [Bedrag] => 60.00 [3] => 60.00 [Omschrijving] => Publiciteitsboekje heel A5 [4] => Publiciteitsboekje heel A5 ) )
Array ( )
Array ( )
How can I make it work?

Related

How will i print the result in view in CodeIgniter?

I am trying to get a result in view returned as array in controller.This is a result of an api call using the json.
I need to display the results in the view. There is design integrated in view to display it.
How could i do it in CodeIgniter?
Function call:
$data['nearme_values'] = $this->swsdk->nearMe($lat, $lon, $radius, $page, $size);
This is the var_dumb result of the function call.
stdClass Object
( [content] => Array
( [0] => stdClass Object
( [id] => 1 [name] => Make America Great
[screenShotUrl] => http://52.77.190.221/SW/speaker_wire_images/streamSShot/stream-1479113014667.jpg
[type] => PUBLIC [status] => RECORDED [userId] => 1 [userHandle] => abhilekh
[channelId] => 1 [channelName] => Sports [likes] => 0 [views] => 0
[liveViewers] => 0
)
[1] => stdClass Object
( [id] => 2 [name] => Make America Great
[screenShotUrl] => http://52.77.190.221/SW/speaker_wire_images/streamSShot/stream-1479110563909.jpg
[type] => PUBLIC [status] => RECORDED [userId] => 1 [userHandle] => abhilekh
[channelId] => 2 [channelName] => Politics [likes] => 0 [views] => 0
[liveViewers] => 0
)
[2] => stdClass Object
( [id] => 3 [name] => Make America Great
[screenShotUrl] => http://52.77.190.221/SW/speaker_wire_images/streamSShot/stream-1479109601008.jpg
[type] => PUBLIC [status] => RECORDED [userId] => 2 [userHandle] => sudhanshu
[channelId] => 3 [channelName] => Fun [likes] => 0 [views] => 0
[liveViewers] => 0
)
[3] => stdClass Object
( [id] => 4 [name] => Make America Great
[screenShotUrl] => http://52.77.190.221/SW/speaker_wire_images/profiles/pic-130.jpg
[type] => PUBLIC [status] => RECORDED [userId] => 2 [userHandle] => sudhanshu
[channelId] => 4 [channelName] => Art [likes] => 0 [views] => 0
[liveViewers] => 0
)
) [last] => 1 [totalPages] => 1 [totalElements] => 4
[numberOfElements] => 4 [sort] => [first] => 1
[size] => 10 [number] => 0
)
I am new to the CodeIgniter. Please support with sample code.
if you are gonna call as API, no need to put in view.
Just echo like this in controller's method.
$nearme_values = $this->swsdk->nearMe($lat,$lon,$radius,$page,$size);
echo json_encode($nearme_values);
Displaying in View.
// in Controller.
$data['nearme_values'] = $this->swsdk->nearMe($lat, $lon, $radius, $page, $size);
$this->load->view('ur_view_file', $data);
// in view
foreach ($nearme_values->content as $nearme) {
echo $nearme->id .' / '. $nearme->screenShotUrl .' / '. $nearme->type .'<br>';
}

Warning: mysql_fetch_array() expects parameter 1 to be resource, but print_r command display value

My code gave this warning, but when i print_r the row, it display the value selected.
So how do i solve this
This is the code "
$query3 = ("Select b.*, c.ScrubNurseName from scrubnurse b, surgery a, scrubnurselist c
where b.rn_no = '$id'
and a.SurgId = b.SurgId
and b.ScrubNurse = c.ScrubNurseID
ORDER BY b.SurgId, b.SerialNo");
$result3 = mysql_query($query3) or die(mysql_error());
$row3 = mysql_fetch_array($result3);
<?php
/*try utk display scrubnurse */
print_r($row3);
while($row3 = mysql_fetch_array($query3)){?>
<p>Scrub Nurse : <?php echo $row3['ScrubNurse'];?>
<p>Status : <?php echo $row3['Status'];?>
<?php }
and this is the result from print_r($row3)
Scrub Nurse Array ( [0] => 16
[SerialNo] => 16 [1] =>
rand52fad80207b6a1.33040579 [SurgID] => rand52fad80207b6a1.33040579 [2]
=> RN001-13 [Rn_No] => RN001-13 [3] =>
2014-02-10 [Surg_Date] => 2014-02-10 [4] => 015405 [ScrubNurse] =>
015405 [5] => C [SNRole] => C [6] => Azhari Landut [ScrubNurseName] => Azhari
Landut )
Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\xampp\htdocs\cotds3\editpt_surgery2.php on line 470
In line while($row3 = mysql_fetch_array($query3)){? there is typo it should be while($row3 = mysql_fetch_array($result3)){?

Create multi dimensional array in cakephp

Now I have 3 row of data.
id name date
1 data1 12-06-2013
2 data2 12-06-2013
3 data3 16-06-2013
now I want to group the 3 rows of data into a new array, something like:
Array
(
[2013-06-12] => Array
(
[0] => data1
[1] => data2
)
[2013-06-16] => Array
(
[0] => data3
)
)
the question is 1st i using foreach to show all date out.
<?php foreach($datax as $data):
echo $data['Model_Name']['id'];
endforeach;?>
then, while i looping i want to group up the $data sort to date as the multidimensional array as above.
Try below method,. you can add an additional column for date
$a = array(
array('datax' => array('id' => 1, 'name' => 'data1')),
array('datax' => array('id' => 2, 'name' => 'data2')),
array('datax' => array('id' => 3, 'name' => 'data3'))
);
$result = Set::classicExtract($a, '{n}.datax.id');
/* $result now looks like:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
*/
$result = Set::classicExtract($a, '{n}.datax.name');
/* $result now looks like:
Array
(
[0] => data1
[1] => data2
[2] => data3
)
*/

insert data into database from an embedded form in codeigniter

Hi I am using codeigniter and sheepIt clone forms.(embed forms).
I am trying to insert the data into database after submitting.
The output data is in this format when i used print_r()
Array
(
[project] => Array
(
[0] => Array
(
[module] => Design
[features] => Array
(
[feature_0] => Array
(
[feature] => Login
[Hours] => 10
)
[feature_1] => Array
(
[feature] => Signup
[Hours] => 10
)
)
)
[1] => Array
(
[module] => Development
[features] => Array
(
[feature_0] => Array
(
[feature] => Login
[Hours] => 20
)
)
)
)
[submit] => save
)
I can post the code of sheepIt forms also.
ANSWER:
$arr_data = $this->input->post();
foreach($arr_data['project'] as $prj) {
foreach($prj as $i) {
$arr['module'][] = module = $i['module'];
foreach($i['features'] as $f) {
$arr['feature'][] = $f['feature'];
$arr['Hours'][] = $f['Hours'];
}
}
}
print_r($arr);
Use this processed $arr data for storing or other.

DB Query in Drupal 7

I am trying to run a db query in drupal where, the content type has a node association field, and I am trying to get all notes of that type, where the NID of the current node matches any of the nodes specified in said note association field.
a visual example
Nodetype1
-- Node Association Field
NodeType2
I would like to get all Nodetype1's where Node Association Field matches the NID of Nodetype2 that is currently loaded.
My current db query is like so:
db_query("SELECT * FROM field_data_field_promo_profile WHERE field_promo_profile_nid=".$N->nid);
and this returns nothing, when i know for a fact that such a node exists, I also tried dropping the WHERE statement and it returns an array like this:
DatabaseStatementBase Object ( [dbh] => DatabaseConnection_mysql Object ( [shutdownRegistered:protected] => [target:protected] => default [key:protected] => default [logger:protected] => [transactionLayers:protected] => Array ( ) [driverClasses:protected] => Array ( [SelectQuery] => SelectQuery [DatabaseSchema] => DatabaseSchema_mysql [MergeQuery] => MergeQuery [DatabaseTransaction] => DatabaseTransaction [UpdateQuery] => UpdateQuery [InsertQuery] => InsertQuery_mysql ) [statementClass:protected] => DatabaseStatementBase [transactionSupport:protected] => 1 [transactionalDDLSupport:protected] => [temporaryNameIndex:protected] => 0 [connectionOptions:protected] => Array ( [database] => cityhound_dev [username] => blahblah [password] => blahblah [host] => localhost [port] => [driver] => mysql [prefix] => Array ( [default] => ) ) [schema:protected] => DatabaseSchema_mysql Object ( [connection:protected] => DatabaseConnection_mysql Object *RECURSION* [placeholder:protected] => 0 [defaultSchema:protected] => public [uniqueIdentifier:protected] => 4fd7fba9e563e2.50177866 ) [prefixes:protected] => Array ( [default] => ) [prefixSearch:protected] => Array ( [0] => { [1] => } ) [prefixReplace:protected] => Array ( [0] => [1] => ) ) [queryString] => SELECT * FROM field_data_field_promo_profile )
Any one have some ideas ?
db_query() returns an iterable object, so you just need to iterate over it:
$result = db_query("SELECT * FROM field_data_field_promo_profile WHERE field_promo_profile_nid=".$N->nid);
foreach ($result as $row) {
$entity_id = $row->entity_id;
// etc...
}
You should use parameters in your queries to prevent SQL injections.
For instance the query above should look like this:
$result = db_query("SELECT * FROM {field_data_field_promo_profile} p
WHERE p.field_promo_profile_nid = :nid ", array(':nid' => $N->nid);
foreach ( $result as $row ) {
$entity_id = $row->entity_id;
// etc...
}