I'm trying to compare two arrays:
First array
$query = $db->prepare("SELECT vraagnummer FROM insertquestion");
$query->execute();
$fetch_query = $query->fetchAll(PDO::FETCH_ASSOC);
Second array
$query = $db->prepare("SELECT vraagnummer FROM vraag");
$query->execute();
$fetch_query_exists = $query->fetchAll(PDO::FETCH_ASSOC);
I want to receive all the results of $fetch_query exept for the ones that are in fetch_query_exists
I tried using:
$result=array_diff($fetch_query ,$fetch_query_exists );
But no results are displayed, just an empty array.
So how do I fetch the results of fetch_query excluding the ones that exist in fetch_query_exists ?
You can do it in single query using NOT IN
SELECT vraagnummer FROM insertquestion
WHERE vraagnummer NOT IN(SELECT vraagnummer FROM vraag)
In addition to #Saty's answer I believe the following would also do the trick without the subselect.
SELECT i.vraagnummber
FROM insertquestion i
LEFT JOIN vraag v
ON v.vraagnummer = i.vraagnummer
WHERE v.vraagnummer IS NULL;
Maybe that, but not sure:
$query = $db->prepare("SELECT vraagnummer FROM insertquestion RIGHT JOIN vraag ON insertquestion.vraagnummer = vraag.vraagnummer");
$query->execute();
$fetch_query = $query->fetchAll(PDO::FETCH_ASSOC);
If you don't want to use a single query then use this:
$result = array_diff_assoc($fetch_query , $fetch_query_exists );
Related
I want to count of records by date and domain. php query works fine with phpmyadmin but in php code it's return null. I want to show by date like today, tomorrow. Join both table and count by date and domain.
wp_domain_prefix TABLE
wp_projectid_1 TABLE
PHP query
<?php
$querys = "SELECT `wp_domain_prefix.domain`, count( `wp_projectid_1.record_id` )
FROM `wp_domain_prefix`, `wp_projectid_1` WHERE `wp_domain_prefix.domain` = `wp_projectid_1.domain` AND
`wp_projectid_1.datess` BETWEEN '2014-10-07' AND '2014-10-08'";
$record = mysql_query($querys);
echo $record[count('wp_projectid_1.record_id')];
?>
Change the query as,
$querys = "SELECT wp_domain_prefix.domain, count(wp_projectid_1.record_id) FROM wp_domain_prefix, wp_projectid_1 WHERE wp_domain_prefix.domain = wp_projectid_1.domain WHERE
wp_projectid_1.datess BETWEEN '2014-10-07' AND '2014-10-08'";
$record = mysql_query($querys);
$row = mysql_fetch_array($record);
echo $row['record_id'];
It would be better to use mysqli instead of mysql because mysql is deprecated and support would be ended soon.
You are not fetching data from mysql_fetch_row after doing mysql_query try like this
$querys = "SELECT `wp_domain_prefix.domain`, count( `wp_projectid_1.record_id` ) as totalcount FROM `wp_domain_prefix`, `wp_projectid_1` WHERE `wp_domain_prefix.domain` = `wp_projectid_1.domain` AND
`wp_projectid_1.datess` BETWEEN '2014-10-07' AND '2014-10-08'";
$record = mysql_query($querys);
$row = mysql_fetch_row($record);
print_r($row);
echo $row['totalcount'];
You need to use GROUP BY for aggregate functions like COUNT()
$querys =
"SELECT wp_domain_prefix.domain, count( wp_projectid_1.record_id )
FROM wp_domain_prefix, wp_projectid_1
WHERE wp_domain_prefix.domain = wp_projectid_1.domain
AND wp_projectid_1.datess BETWEEN '2014-10-07' AND '2014-10-08'
GROUP BY wp_domain_prefix.domain";
$record = mysql_query($querys);
$row = mysql_fetch_row($record);
print_r($row);
Read more about GROUP BY
How to select one row with max(select field) and another (specific field)?
Below is my code
1. Get to know max(select field) first
2. Then select all field within the row.
but step 2 doesn't return any thing, wonder know why?
And is there shorter syntax for same result??
Thanks.
$gid = 1;
// get lid
$sth = $db->prepare("SELECT MAX(lid) as lid FROM t WHERE gid = :gid");
$sth->bindParam(':gid', $gid);
$sth->execute();
$arr = $sth->fetch(PDO::FETCH_ASSOC);
print $arr['lid'];
// nothing return??
$lid = $arr['lid'];
$sth = $db->prepare("SELECT * FROM t WHERE gid = :gid AND lid = :lid");
$sth->bindParam(':gid', $gid);
$sth->bindParam(':lid', $lid);
$row = $sth->fetch();
print_r($row);
You could do it in one step with this query:
$sql = "SELECT t.*
FROM t
INNER JOIN (
SELECT MAX(lid) as lid
FROM t
WHERE gid = :gid
) AS x ON t.lid = x.lid";
$sth = $db->prepare($sql);
$sth->bindParam(':gid', $gid);
$sth->execute();
$arr = $sth->fetch(PDO::FETCH_ASSOC);
print_r($arr);
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];
I've a PHP application that works with MySQL through PDO. I have a table with different records and I have to prevenet inserting a duplicate one. But when I want to check existing items, select statement does not return a true value. This is my code:
$sql = "SELECT COUNT(id) FROM tbl_product_category1 WHERE title = '?'";
$q = $db->prepare($sql);
$q->execute(array($title));
if ($q->fetchColumn() == 0)
{
...
I also tested this one:
$sql = "SELECT id FROM tbl_product_category1 WHERE title = '?'";
$q = $db->prepare($sql);
$q->execute(array($title));
$rows = $q->rowCount();
if ($rows == 0)
{
...
Imagine $title=1. I have 4 records with this value. But I can not see anything in SELECT statement. What is wrong here ?
try this: (don't wrap the value of the title with single quotes)
$sql = "SELECT COUNT(id) FROM tbl_product_category1 WHERE title = ? ";
I have the following:
$sql = "SELECT c.category_name
, c.category_name_url
FROM blog_categories AS c
JOIN blog_articles AS a
ON a.category_name = c.category_name
WHERE c.category_status = 'online'
GROUP BY c.category_name
";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$category_name = $row['c']['category_name'];
$category_name_url = $row['c']['category_name_url'];
}
But it's not working (generating blanks). I'm sure I'm doing something wrong, but I don't know what the formal terms of what I'm looking for is so Google is no help =/.
code was not running because you dint supply a valid resource for the mysql_fetch_array.
also $row will be a single dimensional array.
$sql = mysql_query("SELECT c.category_name
, c.category_name_url
FROM blog_categories AS c
JOIN blog_articles AS a
ON a.category_name = c.category_name
WHERE c.category_status = 'online'
GROUP BY c.category_name
");
while($row = mysql_fetch_array($sql)) {
$category_name = $row['category_name'];
$category_name_url = $row['category_name_url'];
}
$category_name = $row['category_name'];
Well, I vote for PDO, but I am pretty sure you can skip the 'c', as $row will refer to the fieldname. The c is just evaluated by the DBMS to associate the field with the propper table.
You should call mysql_query on your $sql (the sql query), you then call mysql_fetch_array againts the result of the call. Anyway, you should almost always call mysql_error to check for errors.