I have this query.
$stmt = $this->getconnection()->prepare("SELECT * FROM detailuser where id = ?");
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
and I want to add amount in the result after the dateadded, but I am confuse how to add the amount. the amount is in another table and I have no reference on the first table (detailuser)
$amount = 100;
$rec = array(
'data'=>$result,
);
echo json_encode($rec);
{"data":[{"firstname":"Alex","friendname":"Alice","amount_due":"600.00","dateadded"
:"2015-09-14 13:30"},{"firstname":"Annie","friendname":"Karren","amount_due":"600
.00","dateadded":"2015-09-14 13:30"},{"firstname":"Helen","friendname":"Alice","amount_due":"600.00","dateadded":"2015-09-14 13:30"}]}
Thank you in advance
You can prepare a SELECT statement to fetch all rows and in that statement, you can add your dummy column.
SELECT *, 100 AS 'amount' FROM table_name
Instead of abusing your SELECT statement you can also loop over your result set.
foreach ( $result as $row ){
$row['amount'] = 100;
}
//encode and output
Use PDO prepare and then fetchAll() results!
$stmt= $dbh->prepare("SELECT firstname, friendname, amount_due, dateadded, "100" AS amount FROM yourTable");
$stmt->execute();
$result = $stmt->fetchAll();
Related
$sql = "select id from images";
$st = $db->prepare($sql);
$st->execute();
$total = $st->rowCount();
echo $total;
Do I really need all the above to get number of rows in a table? Is there a shorter way?
Use count to retrieve the number of rows in a table.
$st = $db->query('SELECT COUNT(*) FROM images');
$total = $st->fetchColumn();
echo $total;
(Thanks to Funk Forty Niner for the hint to remove the prepare/execute completely)
Okay so I have a query like this one
$get_downlines = "SELECT * FROM referrals WHERE ref_upline = :rupline";
$get_downlines = $pdo->prepare($get_downlines);
$get_downlines-> bindValue(':rupline', $sessionid);
$get_downlines-> execute();
while($fetch_downlines = $get_downlines->fetch()){
$rdownline = $fetch_downlines['ref_downline'];
$dr = "SELECT * FROM `ads_viewed` WHERE av_user = :user";
$dr = $pdo->prepare($dr);
$dr-> bindValue(':user', $rdownline);
$dr-> execute();
echo $dr_count = $dr->rowCount();
}
The code above gives me the row counts as say 3456 (all are separate counts like 3,4,5,6). Now I want to sum up all these rows here and get the result as 3+4+5+6 = 18. And assign it to a global variable which can be used anywhere outside while loop (if possible). How can this be done?
You could do the following:
$get_downlines = "SELECT * FROM referrals WHERE ref_upline = :rupline";
$get_downlines = $pdo->prepare($get_downlines);
$get_downlines-> bindValue(':rupline', $sessionid);
$get_downlines-> bindValue(':direct', "direct");
$get_downlines-> execute();
$totalrows;
while($fetch_downlines = $get_downlines->fetch()){
$rdownline = $fetch_downlines['ref_downline'];
$dr = "SELECT * FROM `ads_viewed` WHERE av_user = :user";
$dr = $pdo->prepare($dr);
$dr-> bindValue(':user', $rdownline);
$dr-> execute();
echo $dr_count = $dr->rowCount();
$totalrows+= $dr->rowCount();
}
echo $totalrows;
First you create the $totalrows; variable outside of the loop. You can increment this variable with the amount of rows in your query using $totalrows += $dr->rowCount(); inside of the while loop
This can be done with a single query:
$stmt = $pdo->prepare("
SELECT COUNT(*) AS cnt
FROM referrals
JOIN ads_viewed ON ads_viewed.av_user = referrals.ref_downline
WHERE ref_upline = :rupline
");
$stmt->execute(['rupline' => $sessionid]);
$dr_count = $stmt->fetchColumn();
Note: Everytime you execute an SQL query in a while-fetch-loop you probably better use a JOIN. And everytime you use SELECT * just to get the number of rows, you are wasting resources and should use COUNT(*) instead.
I'm trying to input a PHP variable (in this case $beg) into a mySQL query but it returns an empty array result. The type of the field in the database is an integer. When I type in an actual value instead of the variable I get the correct result. What's wrong?
$beg = time()-5000;
settype($beg, "integer");
$result = mysql_query('SELECT * FROM records WHERE time>=$beg ORDER BY time ASC');
$statusdata = array();
while ($row = mysql_fetch_array($result)) {
array_push($statusdata, $row["status"]);
}
Make sure you use double quotes when using $variables inside the string.
$result = mysql_query("SELECT * FROM records WHERE time>= $beg ORDER BY time ASC");
You should use prepared statements instead of mysql_query.
$beg = time()-5000;
settype($beg, "integer");
$db = new mysqli("host","user","pw","database");
$stmt = $db->prepare("SELECT status FROM records WHERE time>=? ORDER BY time ASC");
$stmt->bind_param('i', $beg);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($status);
$statusdata = array();
while($stmt->fetch())
{
array_push($statusdata, $status);
}
$stmt->close();
Change the line
$result = mysql_query("SELECT * FROM records WHERE time>=$beg ORDER BY time ASC");
You must use double quote strings to put variables.
Change your query
$result = mysql_query(" SELECT * FROM records WHERE time >= $beg ORDER BY time ASC ");
You cannot use variable inside single quotes.
try this method, I use a lot:
$beg = time() - 5000;
$query = sprintf("SELECT * FROM %s WHERE time >= '%o' ORDER BY %s ASC", "records", $beg, "time");
$result = mysql_query($query);
remeber, time() result is Integer, you don't need set him in to an Integer
hello can i merge those 2 queries in one query my first query get the number of articles in database and second query get the sum of all visits of all article whats the best method to make it one query
$stmt = $db->query('SELECT * FROM stories');
$story_count = $stmt->rowCount();
$stmt = $db->query("SELECT sum(visits) FROM stories");
$total_visits = $stmt->fetchColumn();
Try like
$stmt = $db->query('SELECT COUNT(*) as total_cnt,
SUM(visits) as total_visits FROM stories');
then excute your query,you will get result from "total_cnt" and "total_visits"
SELECT COUNT(*) as total, SUM(visits) as total_visits FROM stories;
SELECT Story.*, COUNT(*) as total, SUM(Story.visits) as total_visits FROM stories AS Story;
If you want to get other fields along with SUM and COUNT use .*.
Yes try this:
$stmt = $db->query('SELECT count(*),sum(visits) FROM stories');
$result = $stmt->fetch_array(MYSQLI_NUM);
$story_count = $result[0];
$total_visits = $result[1];
How do I get the results and count the results in one query? (Im using PDO statements)
SELECT * FROM table; // gets results
SELECT COUNT(*) FROM table; // counts results
$result = mysql_query( "SELECT * FROM table");
$count = mysql_num_rows( $result);
Using PDO:
$statement = $dbh->prepare('SELECT * FROM table');
$statement->execute();
$count = $statement->rowCount();
This will put the record count at the end of each row.
SELECT *
, COUNT(1) OVER () AS RecordCount
FROM table;
SELECT *, (select count(*) FROM table) ct FROM table
you should execute only one query...
$sql = SELECT * FROM table;
$res = mysql_query($sql);
you can have total count by mysql_num_rows(); function.. like this ..
$count = mysql_num_rows($res);