How to store MySQLi bind_result value for further processing? - mysqli

In the below code,I can echo $count in bind_result by using while loop(commented in the code). But instead I want to keep that value in the $count variable for further processing instead of just printing the value. How do I hold the value for $count?
/*......mysqli connection...*/
$stmt =$mysqli->prepare("select count(*) from tab where rtd_id=? ");
$stmt->bind_param('i',$id);
$id = 1;
$stmt->execute();
$stmt->bind_result($count);
/*while($stmt->fetch())
{
echo $count;
}*/
if($count>0)
{
//DO SOMETHING
}
?>

First of all, you don't need a loop here.
$stmt->bind_result($count);
$stmt->fetch();
if($count>0)
{
//DO SOMETHING
}

Related

Getting sum in PHP MySQL

I want to get sum of integers that are selected from mysql
Here is my code
<?php
Include 'init.php';
$page = '2459';
$ttl = array();
$search = $db->link->query("SELECT id,packid,rating FROM pakrate WHERE packid LIKE '%$q%'");
if($search->num_rows>0){
while($result=$search->fetch_assoc()){
$ttl[] = $row['rating'];
echo $ttl;
echo $result[SUM('rating')];
}
}
else {
echo "no such query";
}
So what I am doing wrong here, any other suggestions?
Just run the sum in your query
<?php
Include 'init.php';
$page = '2459';
$ttl = array();
// Not sure why you're doing this but there should only be one leading connection ($conn->query) unless you're doing some object item elsewhere in init.
// I would also use prepared statements since they're more secure:
$q = "%$q%"; // create your like variable, wherever $q is coming from??
$query = "SELECT sum(rating) from pakrate WHERE packid LIKE ?);
$stmt = $db->prepare($query); // assuming $db is your connection variable
if($stmt){
$stmt->bind_param("s",$q); // Bind the variable in
$stmt->execute(); // Execute
$stmt->bind_result($rating); // Bind the result variable
$stmt->store_result(); // Store it so you can get num_rows
$rows = $stmt->num_rows; // assign rows found
if($rows > 0){
while($stmt->fetch()){ // Loop the result
$ttl[] = $rating; // add to your array
// You can't echo an array like this.... echo $ttl;
// if you really want to, print_r($ttl);
}
}
$stmt->close(); // Close it
}
?>

Returning an array using PDO

I'm trying to use PDO using the following code, I want to select the array of values for the context_list, but it returns one record, any ideas where I'm going wrong?
try {
$sql2 = "SELECT area_easting, area_northing, context_number FROM excavation.contexts";
$sth = $conn->prepare($sql2);
$sth->execute();
while ($row = $sth->fetch(PDO::FETCH_ASSOC))
{
$easting_list = $row['area_easting'];
$northing_list = $row['area_northing'];
$context_list = $row['context_number'];
}
}
catch(PDOException $e )
{
echo "Error: ".$e;
}
echo "context list: ";
echo $context_list;
A partial solution:
This worked:
$query = $conn->prepare("SELECT area_easting, area_northing, context_number FROM excavation.contexts");
$query->execute();
while($r = $query->fetch(PDO::FETCH_OBJ)){
echo $r->area_easting, '|';
echo $r->area_northing;
echo '<br/>';
}
But Now I need to make the $r->area_easting accessible to the session, but that's another question.
Your query may return several records (provided they actually exist in the table).
Your code loops through all the records (with the while loop) but the values ($easting_list, $northing_list and $context_list) are overwritten in the loop.
I suggest the following changes to your code:
$easting_list = array();
$northing_list = array();
$context_list = array();
try {
$sql2 = "SELECT area_easting, area_northing, context_number FROM excavation.contexts";
$sth = $conn->prepare($sql2);
$sth->execute();
while ($row = $sth->fetch(PDO::FETCH_ASSOC))
{
$easting_list[] = $row['area_easting'];
$northing_list[] = $row['area_northing'];
$context_list[] = $row['context_number'];
}
}
catch(PDOException $e )
{
echo "Error: ".$e;
}
echo "context list: ";
echo implode(', ', $context_list);
Now all the values are stored in 3 arrays. explode is used to print a comma-separated list of all values in $context_list array.

Why is the same date echoing despite being created and stored properly in db?

I am pulling comments from my database. The 'created_on' field I have created is inserting properly, however when I echo the results, it's a random date and time that is the same no matter what(Mar 11, 2013 at 10:50 AM).
Here is my query to pull the records:
public function get_airwave_comments($profile_id)
{
$session = $this->session->userdata('is_logged_in');
$user_id= $this->session->userdata('id');
if($profile_id == $user_id)
{
$comments_query = "SELECT * FROM airwaves_comments aw,users u WHERE u.id=aw.from_id AND AW.FROM_id=aw.to_id AND aw.from_id=".$profile_id." order by aw.created_on desc" ;
}
else
{
$comments_query = "SELECT * FROM airwaves_comments aw,users u WHERE u.id=aw.from_id AND aw.to_id=".$profile_id." order by aw.created_on desc" ;
}
$query = $this->db->query($comments_query);
if($query->num_rows() >= 1)
{
$data = $query->result_array();
// return whole resultset. It contains zero, one or more records
return $data;
}
else return false;
$query = $this->db->query($poster_info_query);
if($query->num_rows() >= 1)
{
$data = $query->result_array();
// return whole resultset. It contains zero, one or more records
return $data;
}
else return false;
}
}
Here is the view in which I'm trying to echo the 'created_on' field properly:
if ($airwave && $profile_id == $session_id)
{
foreach ($airwave as $airwave_comment_row)
$airwave_comment_row = $airwave[0];
{
echo "<div id='profile_airwave'>";
echo $airwave_comment_row['comment'];
echo "<br />";
echo "<span class='profile_airwave_info'>";
echo date('M d, Y',strtotime($airwave_comment_row['created_on'])); ?> at <?php echo date('g:i A',strtotime($airwave_comment_row['created_on']));
echo "</span>";
echo "</div>";
}
}
so basically even if I just do echo $airwave_comment_row['created_on']; It's echoing the same said date.
thanks in advance.
I changed the name of the datetime column in the comments table so that it was different from the datetime column in the users table it's being spliced with.

Result binding in mysqli

In the following example, can any folks show me how to code the binding part as I select all fields from the table
$stmt = $mysqli_conn->stmt_init();
if ($stmt->prepare("SELECT * FROM books")) {
$stmt->execute();
$stmt->bind_result( **WHAT DO I PUT HERE** );
$stmt->close();
}
In bind_result, you put in the variables where you want to fetch the data into, instead of using an array returned otherwise.
$stmt->bind_result($col1, $col2, $col3, $col4);
while($stmt->fetch_assoc())
echo "$col1 $col2 $col3 $col4";
Alternatively, if you don't want to bind the result
while($resultArray = $stmt->fetch_assoc()) {
echo "$resultArray[columnName1] $resultArray[columnName2] ...";
}

why mysqli one pulling one row with foreach loop in php?

Hi friends why mysqli one pulling one row with foreach loop in php ? Shouldn't it fetch arrays of all rows ?
$link = new mysqli(DB_SERVER, DB_USER,DB_PASSWORD,DB_NAME) or die('error connecting');
$mob = query("SELECT mobile FROM members_db");
foreach ($mob as $numbers){
$mob_numbers = $numbers['mobile'];
print_r($mob_numbers); exit();
}
You need to do the following
Change:
foreach ($mob as $numbers)
{
$mob_numbers = $numbers['mobile'];
print_r($mob_numbers); exit();
}
To
$results = array();
foreach ($mob as $numbers)
{
$results[] = $numbers['mobile'];
}
print_r($results); exit();
You have to 'Fetch' the records one by one
$mob = query("SELECT mobile FROM members_db");
while($row = mysqli_fetch_array($link, $mob)){
print_r($row['mobile'])
}
Also, not sure why you're calling exit() inside the loop. Doesn't make sense.
remove your exit(); within the loop