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

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)){?

Related

PDO second fetchall() returns empty array

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?

fetch data from array and save it in database

I had this code
echo "<pre>";
print_r($data);
echo "</pre>";
through which i got the following data
Spreadsheet_Excel_Reader Object
(
[sheets] => Array
(
[0] => Array
(
[maxrow] => 0
[maxcol] => 0
[numRows] => 2
[numCols] => 5
[cells] => Array
(
[1] => Array
(
[1] => sno
[2] => candidta name
[3] => email
[4] => ctc
[5] => location
)
)
)
)
)
I needed numCols so i used the following code
echo $data->sheets[0]['numCols'];
and got the result 5
Now i wish to fetch the following values
[1] => sno
[2] => candidta name
[3] => email
[4] => ctc
[5] => location
and store them in the table like this
id value
1 sno
2 candidta name
3 email
4 ctc
5 location
Can anyone please tell how it can be done
$Data = array();
foreach($data->sheets[0]['cells']['1'] as $key=>$value):
$Data[] = "('".$key."','".$value."')";
endforeach;
mysql_query("INSERT INTO tbl_name(`id`,`value`) VALUES".implode(",",$Data)) or die (mysql_error());
$newarray = $data->sheets[0]['cells'][1];
for ( $i=1; $i < count($newarray); $i++)
{
echo $newarray[$i];
$sql= "INSERT INTO `tablename` (value) VALUES ('$newarray[$i]')";
if(!mysqli_query($con,$sql))
{
echo "Error: " . $sql. "<br>" . mysqli_error($con);
}
}

fetch_row alternative in Codeigniter

How do I get this in Codeigniter? If I tried in a same, it show Fatal error: non object 'fetch_row', please help me to get using alternative in codeigniter for fetch_row.
$sql = "SELECT DISTINCT attnDate
FROM tbl_attendance
ORDER BY attnDate";
$res = $g_link->query($sql); // mysqli query
while ($row = $res->fetch_row()) {
$dates[] = $row[0];
}
get $dates As below:
Array
(
[0] => 2015-11-25
[1] => 2015-11-29
[2] => 2015-11-30
[3] => 2015-12-01
[4] => 2015-12-02
[5] => 2015-12-03
[6] => 2015-12-05
[7] => 2015-12-06
[8] => 2015-12-07
[9] => 2015-12-08
[10] => 2015-12-09
[11] => 2015-12-10
[12] => 2015-12-12
)
As I trying in codeigniter:
Model:
public function raw_attendance_Status(){
$this->db->distinct();
$this->db->select('attnDate');
$this->db->from('tbl_attendance');
$this->db->order_by('attnDate');
$query = $this->db->get();
return $query->result();
}
Controller:
public function rawAttendance(){
$data['date1'] = $this->AttendanceModel->raw_attendance_Status();
$this->load->view('attendance/rawAttendance', $data);
}
I guess using result_array() can do it for you!
$this->db->distinct();
$this->db->select('attnDate');
$this->db->from('tbl_attendance');
$this->db->order_by('attnDate');
$query = $this->db->get();
return $query->result_array();

How to get the first row of a result in Yii2? (queryRow Equivalent)

how can i get the first row of result? Below is my code which is giving me an error like this Undefined index: module
if ( substr( $action, 0, 4 ) === "stl_" )
{
$query = "SELECT * FROM a_actions LEFT JOIN a_modules ON ( a_modules.id=a_actions.module_id )
WHERE a_actions.id=(SELECT dependency FROM a_actions WHERE action='{$action}') AND a_modules.module_status = 1 ";
$action = \Yii::$app->db->createCommand( $query )
->queryAll();
//print_r($action);die();
$module = $action[ 'module' ];
$action = $action[ 'action' ];
}
$action has value
Array ( [0] => Array ( [id] => 7 [module_id] => 7 [action] => index [label] => Members [dependency] => [created_by] => [created_at] => [updated_by] => [updated_at] => [is_deleted] => 0 [module] => members [module_name] => [module_status] => 1 ) )
in Yii1 i would have used
$action = \Yii::$app->db->createCommand( $query )
->queryRow();
You can use queryOne()
\Yii::$app->db->createCommand( $query )
->queryOne();
QueryOne()

php $json google maps parsing

I have the following code:
<?php
$q = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=35.0078,-97.0929&destinations=45.2078,-97.0929&mode=driving&sensor=false";
$json = file_get_contents($q);
$details = json_decode($json, TRUE);
echo "<pre>"; print_r($details); echo "</pre>";
?>
The result is:
Array
(
[destination_addresses] => Array
(
[0] => 150th St, Summit, SD 57266, USA
)
[origin_addresses] => Array
(
[0] => 38591 Patterson Rd, Wanette, OK 74878, USA
)
[rows] => Array
(
[0] => Array
(
[elements] => Array
(
[0] => Array
(
[distance] => Array
(
[text] => 1,299 km
[value] => 1299343
)
[duration] => Array
(
[text] => 13 hours 26 mins
[value] => 48361
)
[status] => OK
)
)
)
)
[status] => OK
)
I need to extract from $json only the distance (1,299 km).
I am not sure how to do this.
Any help is very much appreciated.
<?php $q = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=35.0078,-97.0929&destinations=45.2078,-97.0929&mode=driving&sensor=false";
$json = file_get_contents($q);
$details = json_decode($json);
$distance=$details->rows[0]->elements[0]->distance->text;
echo $distance; ?>