My Query:
select distinct ml.send_to from message_log as ml where NOT exists
(select mobile_no from user_details WHERE user_details.mobile_no = ml.send_to);
$select1 = $db->select()->from('user_details',array('mobile_no'))
->where('user_details.mobile_no = ml.send_to');
$select2 = $db->select()->distinct()
->from(array('ml'=>'message_log'), array('ml.send_to')))
->where('NOT EXISTS ?', $select1);
This will do the trick in the easiest way.
you can use customer query in zend as
$sql = 'select distinct ml.send_to from message_log as ml where NOT exists
(select mobile_no from user_details WHERE user_details.mobile_no = ml.send_to);';
$stmt = new Zend_Db_Statement_Mysqli($db, $sql);
$stmt->execute();
while ($row = $stmt->fetch()) {
echo $row['send_to'];
}
this will really help you
http://framework.zend.com/manual/1.12/en/zend.db.statement.html
Related
I want to make for each data exist on databases then do loop up to get unique data.
Here my code:
$id = rand(10000000,99999999);
$check_id = $db->prepare("SELECT * FROM sh_url WHERE sh_id='$id'");
$check_id->execute();
$count_id = $check_id->rowCount();
for ($count_id != 0) {
$lid = $id+1;
}
$shorturl = htmlentities(base_convert($lid,20,36));
$query = $db->prepare("INSERT INTO `sh_url`(`sh_id`) VALUES (:id)");
$query->bindParam(":id", $lid);
$query->execute();
Why don't you use distinct keyword to do this.
SELECT DISTINCT column1, column2, ...
FROM table_name;
I wrote the following query:
$query1 = "SELECT * FROM session WHERE Id_session IN (SELECT * FROM students_in_session WHERE Username = '$email')";
$res = mysqli_query($conn,$query1);
$query2 ="SELECT * FROM students_in_session WHERE Username='$email'";
$res2 = mysqli_query($conn,$query2);
if (!$res) {
die(mysqli_error($conn));
}else{
while ($row = mysqli_fetch_array($res)) {
print_r($row);
$course = $row['Degree'];
$date = $row['Date'];
$hour = $row['Hour'];
$room = $row['Room'];
}
}
if(!$res2){
die(mysqli_error($conn));
}else{
while ($row = mysqli_fetch_array($res2)) {
print_r($row);
$prof = $row['Professor'];
$assis = $row['Assistent'];
}
}
return "\n\nDegree: ".$course."\n"."Date: ".$date."\n"."Hour: ".$hour."\n"."Room: ".$room."\n"."Prof: ".$prof."\n"."Assistent: ".$assis;
}
Currently using phpmyadmin and testing the query returns the expected result,but using the query in the code the variables are all empty.
These are DB's Table:
session
|Id_Session|Date|Hour|Room|Degree|
students_in_session
|Id_Session|Code|Name|Surname|Username|Professor|Assistent|
In query1 you are just try to compare Id_session with the whole table you need to compere with the session id only.
Just replace this
$query1 = "SELECT * FROM session WHERE Id_session IN
(SELECT * FROM students_in_session WHERE Username = '$email')";
with
$query1 = "SELECT * FROM session WHERE Id_session IN
(SELECT Id_Session FROM students_in_session WHERE Username = '$email')";
I thing it will work for you.
$query1 = "SELECT * FROM session WHERE Id_session IN (SELECT * FROM students_in_session WHERE Username = '$email')";
In this query you must need to select only one column like
$query1 = "SELECT * FROM session WHERE Id_session IN (SELECT Id_session FROM students_in_session WHERE Username = '$email')";
Check it.
when you use a IN over a nested query, the nested query can return only one column:
SELECT * FROM session WHERE Id_session IN
(SELECT Id_Session FROM students_in_session WHERE Username = '$email')
However a nested query is not the best approach in your case, because MySQL will have to execute 2 queries. You would better do an INNER JOIN :
SELECT S.*
FROM session S
INNER JOIN students_in_session SS ON SS.Id_Session=S.Id_Session
WHERE SS.Username = '$email'
I'm trying to select count of repeated dates and output its numbers
$user_curr_id = $_SESSION['user_id'];
$sql = "SELECT COUNT(datum) FROM table_name WHERE user_ids = $user_curr_id";
I have no idea how to do it.
2014-07-23,2014-07-23,2014-07-23 => 3
2014-07-24,2014-07-24 =>2
2014-07-25 => 1
and get $result = 3,2,1
I assume you're looking after the GROUP BY clause:
$sql = "SELECT datum, COUNT(datum) as cnt
FROM table_name
WHERE user_ids = $user_curr_id
GROUP BY datum
ORDER BY COUNT(datum) DESC;";
if your column datum is of the data type DATE.
Note
As already mentioned you're vulnerable to sql injection. You should use a parameterized prepared statement and bind your input value to this parameter like that:
$sql = "SELECT datum, COUNT(datum) cnt
FROM table_name
WHERE user_ids = ?
GROUP BY datum
ORDER BY COUNT(datum) DESC;";
$result = array();
if ($stmt = $mysqli->prepare($sql)) {
if ($stmt->bind_param('s', $user_curr_id)) {
if($res = $stmt->execute()) {
while ($row = $res->fetch_assoc()) {
$result[] = $row['cnt']; // add the content of field cnt
}
}
}
}
echo implode(',', $result);
I am new to both mysql and php.
I have two tables which are 'members' and 'points'. Both of them including the column 'username'. I want to select all the values from these two tables where username= $POST[username].
So I wrote this but this is not working.
$username = $_POST['username'];
$sql = $con->prepare("SELECT *.members, *.points FROM members, points WHERE
username=?");
$sql->bind_param("s", $username);
$sql->execute();
$result = $sql->get_result();
$row = $result->fetch_assoc();
And I tried this :
$sql = $con->prepare("SELECT * FROM members INNER JOIN points
ON username.points = username.members WHERE username=?");
$sql->bind_param("s", $username);
$sql->execute();
$result = $sql->get_result();
$row = $result->fetch_assoc();
And this:
$sql = $con->prepare("SELECT *.points, *.members FROM members INNER JOIN points
ON username.points = username.members WHERE username=?");
$sql->bind_param("s", $username);
$sql->execute();
$result = $sql->get_result();
$row = $result->fetch_assoc();
I can't use UNION because the number of columbs are not equel in these tables.
So, Please help me what is wrong with the code? What is the proper way to select all from multiple tables.
Alias are meant to be used to specify to which table those column belong, so you need to prepend table name to your columns
SELECT * FROM members
INNER JOIN points
ON points.username = members.username
WHERE points.username = ?
You can otherwise assign an alias to your table while selecting and use them
SELECT * FROM members a
INNER JOIN points b
ON a.username = b.username
WHERE a.username = ?
You were close with this:
SELECT *.points, *.members
FROM members
INNER JOIN points ON username.points = username.members
WHERE username=?
Try this instead:
SELECT *
FROM members
INNER JOIN points ON members.username = points.username
WHERE members.username=?
check this
SELECT * FROM points,members WHERE points.username="'.$_POST['username'].'" AND members.username="'.$_POST['username'].'";
you can check this query it is very simple.
I was thinking of accomplishing the following as a PHP multi_query. But I'm trying to figure out how to pass the column value from the select query to the insert and update queries.
$query = "SELECT tbl_links.link, link_id
FROM tbl_links
INNER JOIN tbl_items ON tbl_links.item_id = tbl_items.item_id
WHERE tbl_items.item_name like '".$items_name[$counter]."'
AND NOT EXISTS (
select link_id
from tbl_clickedlinks
where tbl_clickedlinks.link_id = tbl_links.link_id
AND tbl_clickedlinks.cust_id = '$items_custID[$counter]'
)
limit 0, 1;" ;
$query .= "INSERT INTO tbl_claimedlinks (cust_id, link_id, claim_time) VALUES ('$items_custID', $row['link_id'], NOW()) ;";
$query .= "UPDATE tbl_links SET click_count = click_count+1 where link_id = '$linkID' ;";*/
Problem is, I'm not sure how to pass the link_id value to the other queries. So I'm thinking I might have to rearrange the queries into one, but again, I'm just not sure how to pull that off.
Anyone got any suggestions?
You need to execute select query 1st then use its output to execute 2nd & 3rd query.
$query = "SELECT tbl_links.link, link_id
FROM tbl_links
INNER JOIN tbl_items ON tbl_links.item_id = tbl_items.item_id
WHERE tbl_items.item_name like '".$items_name[$counter]."'
AND NOT EXISTS (
select link_id
from tbl_clickedlinks
where tbl_clickedlinks.link_id = tbl_links.link_id
AND tbl_clickedlinks.cust_id = '$items_custID[$counter]'
)
limit 0, 1;" ;
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$query2 = "INSERT INTO tbl_claimedlinks (cust_id, link_id, claim_time) VALUES ('$items_custID', $row['link_id'], NOW()) ;";
$query3 = "UPDATE tbl_links SET click_count = click_count+1 where link_id = '$linkID' ;";*/
mysql_query($query2);
mysql_query($query3);
}