Trouble with mysql query in php? - php

$sql = "SELECT s1.roomtype, s1.roomno, s1.checkin,s1.checkout FROM
guestrocordtransac s1
JOIN guestrocord s2
ON s1.roomtype = s2.roomtype AND s1.roomno != s2.roomno
WHERE s1.checkin = '".$date1."' BETWEEN s2.checkin = '".$date1."' AND s2.checkout='".$date2."' ";
I dont know where it went wrong .. i have to check roomtype,room no , on and between checkin and checkout date...
Iam getting the roomttype,roomno ,checkin,checkout values from the form ..Now i have to compare it with database.

Here
$sql = "SELECT s1.roomtype, s1.roomno, s1.checkin,s1.checkout FROM
guestrocordtransac s1
JOIN guestrocord s2
ON s1.roomtype = s2.roomtype AND s1.roomno != s2.roomno
WHERE s1.checkin = '".$date1."' BETWEEN s2.checkin = '".$date1."' AND s2.checkout='".$date2."' ";
you are having an invalid syntax in your where clause, as you should not check equality in the between operands. This should fix the issue:
$sql = "SELECT s1.roomtype, s1.roomno, s1.checkin,s1.checkout FROM
guestrocordtransac s1
JOIN guestrocord s2
ON s1.roomtype = s2.roomtype AND s1.roomno != s2.roomno
WHERE s1.checkin = '".$date1."' BETWEEN '".$date1."' AND '".$date2."' ";
Read more about between here.

Related

How to create IF statement in MYSQL query WHERE statement

Is this possible to be done with SQL?
I need to make a SQL selection depending if a $query is false WHERE (u.id_user = ".$userId." OR fu.id_user = ".$userId." OR ff.id_user = ".$userId.")" ELSE "$query";
That's my starting point where both conditions have to be met:
$validatedSearchData = array(
"q"=>strip_tags($_GET["q"])
);
$query= " AND a.tags LIKE ".lib::$db->qstr("%".$validatedSearchData["q"]."%");
$feed = lib::$db->GetAll("SELECT SQL_CALC_FOUND_ROWS
a.*,
u.name,
fu.id_user AS fu_user_id,
ff.id_followed_user AS ff_user_id
FROM feed AS a
LEFT JOIN userfollow AS fu ON a.id_author = fu.id_user
LEFT JOIN userfollow AS ff ON a.id_author = ff.id_followed_user
INNER JOIN user_profiles AS u ON a.id_author = u.id_user
WHERE (u.id_user = ".$userId." OR fu.id_user = ".$userId." OR ff.id_user = ".$userId.")" . $query. "
GROUP BY a.id_article
");
Change this line
$query= " OR a.tags LIKE ".lib::$db->qstr("%".$validatedSearchData["q"]."%");
replace OR in the place of AND

Complicated array comparison

1- I'm building a comparison app which compares the relationship between two users.
If the user A and user B has the same country address then it will print alert to any of the same similar values.
2- There is another issue which is some users has multiple addresses for example user A has address x1 and user b has address x1 and address x2, in this scenario how do I compare user A's address with the multiple addresses with user B and alert that the address x1 is matched between the two users?
here is the queries for both users
$query1 = "
SELECT
a.PID, a.`Name`, a.Avatar, a.Email,
b.City, a.LDate, b.Country, b.MPhone,
b.Gender, b.Birthday
FROM
`Profile` AS a
LEFT JOIN Details AS b ON a.PID = b.PID
WHERE
a.PID = '1'
";
$UserInfo1 = mysqli_query($mysqli, $query1);
while( $row = mysqli_fetch_array($UserInfo1) ){
$Avatar1 = "{$row['Avatar']}";
$Name1 = "{$row['Name']}";
$Email1 = "{$row['Email']}";
$Country1 = "{$row['Country']}</td>";
$MPhone1 = "{$row['MPhone']}</td>";
$Gender1 = "{$row['Gender']}</td>";
$City1 = "{$row['City']}</td>";
$Birthday1 = "{$row['Birthday']}</td>";
}
$query2 = "
SELECT
a.PID, a.`Name`, a.Avatar, a.Email,
b.City, a.LDate, b.Country, b.MPhone,
b.Gender, b.Birthday
FROM
`Profile` AS a
LEFT JOIN Details AS b ON a.PID = b.PID
WHERE
a.PID = '2'
";
$UserInfo2 = mysqli_query($mysqli, $query2);
while( $row = mysqli_fetch_array($UserInfo2) ){
$Avatar2 = "{$row['Avatar']}";
$Name2 = "{$row['Name']}";
$Email2 = "{$row['Email']}";
$Country2 = "{$row['Country']}</td>";
$MPhone2 = "{$row['MPhone']}</td>";
$Gender2 = "{$row['Gender']}</td>";
$City2 = "{$row['City']}</td>";
$Birthday2 = "{$row['Birthday']}</td>";
}
3- The final results is that I want to make flowchart and point the matched information to each other something like this screenshot:
What would be the best and simplest way to make this comparison in php or Mysql?
Your help is highly needed and appreciated.

inner join not working in PHP, but working in phpMyAdmin SQL

I have a MySql Query that is not returning correct values in PHP, but if I run the same MySql Query in phpMyAdmin, it returns a value. If I display the select in a web browser, I get a 'Resource id #27' on the end of it.
PHP Code
$SQL_PhotoQueryList = "SELECT count(*) FROM `invoice_detail`".
" INNER JOIN `photos` ON invoice_detail.photo_id = photos.photo_id".
" INNER JOIN `invoice` ON invoice_detail.invoice_id = invoice.invoice_id".
" WHERE invoice.invoice_active = '$PassStatus' AND photos.user_id = '$SessionUserID'".
$SQL_PhotoResultList = mysql_query($SQL_PhotoQueryList);
$ListPhotoCount = mysql_result($SQL_PhotoResultList,0);
echo "SQL Query = $SQL_PhotoQueryList<br>";
echo "ListCount = $ListPhotoCount<br>";
Screen Output
SQL Query = SELECT count(*) FROM `invoice_detail` INNER JOIN `photos` ON invoice_detail.photo_id = photos.photo_id INNER JOIN `invoice` ON invoice_detail.invoice_id = invoice.invoice_id WHERE invoice.invoice_active = '2' AND photos.user_id = '2'Resource id #27
ListCount = 0
Code calling the routine ($SessionUserID is a $_SESSION Variable)
$PassStatus = "2"; // Active
require("get_invoice.php");
$InfoTotalSales = $ListGalleryCount;
It looks like you have a typo.
$SQL_PhotoQueryList = "SELECT count(*) FROM `invoice_detail`".
" INNER JOIN `photos` ON invoice_detail.photo_id = photos.photo_id".
" INNER JOIN `invoice` ON invoice_detail.invoice_id = invoice.invoice_id".
" WHERE invoice.invoice_active = '$PassStatus' AND photos.user_id = '$SessionUserID'".
$SQL_PhotoResultList = mysql_query($SQL_PhotoQueryList);
$ListPhotoCount = mysql_result($SQL_PhotoResultList,0);
echo "SQL Query = $SQL_PhotoQueryList<br>";
echo "ListCount = $ListPhotoCount<br>";
Note the full stop (actually, concatenation operator) on the last line of the query:
" WHERE invoice.invoice_active = '$PassStatus' AND photos.user_id = '$SessionUserID'".
This should be a semicolon. Yep, I've done that too. Sometimes the hardest mistake to find.
The resource ID at the end is from the result of mysql_query().

Mysqli LEFT JOIN count

I am really noob in mysqli so i need help..
Please help me to convert this mysql to mysqli...i can't figure it out..
$new_mail = mysql_result(
mysql_query(
"SELECT COUNT(*) FROM `mail`
LEFT JOIN `contact` ON `mail`.`user_id` = `contact`.`from_id`
AND `contact`.`user_id` = '$user_id
WHERE `mail`.`from_id` = '$user_id'
AND `mail`.`read` = '0'
AND `mail`.`delete` != '$user_id'
AND `contact`.`ban` != '1'")
, 0);
if ($new_mail)
$list[] = 'Message - $new_mail '
I converted...tnx anyway :)
$new_mail = mysqli_fetch_array(mysqli_query($db, "SELECT COUNT(*) FROM `mail` LEFT JOIN `contact` ON `mail`.`user_id`=`contact`.`from_id` AND `contact`.`user_id`='$user_id' WHERE `mail`.`from_id`='$user_id' AND `mail`.`obrisano`!='$user_id' AND `contact`.`ban`!='1'"));
#ryan i just wanted to convert that mysql query to mysqli,but tnx :)

PHP coding question

How can I grab the count value from the query MySQL query below using PHP.
Here is My MySQL code.
$dbc = mysqli_query($mysqli,"SELECT COUNT(*) FROM((SELECT users_friends.id
FROM users_friends
INNER JOIN users ON users_friends.user_id = users.user_id
WHERE users_friends.user_id = 1
AND users_friends.friendship_status = '1')
UNION
(SELECT users_friends.id
FROM users_friends
INNER JOIN users ON users_friends.friend_id = users.user_id
WHERE users_friends.friend_id = 1
AND users_friends.friendship_status = '1')) as friends");
using SQL_CALC_FOUND_ROWS should simplify things:
$dbc = mysqli_query($mysqli,"SELECT SQL_CALC_FOUND_ROWS users_friends.id
FROM users_friends
INNER JOIN users ON users_friends.user_id = users.user_id
WHERE users_friends.user_id = 1
AND users_friends.friendship_status = '1'
");
then afterwards do
$rs = mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
$rec = $rs->fetch_array();
$count = $rec[0];
This method will return the number of records that match the query, ignoring any LIMIT statement, whereas using $rs->num_rows will only give you the number of records actually returned. Depends which one you want.
if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10")) {
printf("Select returned %d rows.\n", mysqli_num_rows($result));
/* free result set */
mysqli_free_result($result);
http://us.php.net/manual/en/mysqli.query.php
Assuming that you are correctly connected to the MySQL server and your query are executed correctly, you can use the following code:
$values = mysql_fetch_row($dbc);
$count = $values[0];
Your query should look like SELECT COUNT(*) as numThings FROM xxx
The numThings is what you will reference in PHP:
$result = mysql_query("SELECT COUNT(*) as `numThings` FROM xxx");
$row = mysql_fetch_assoc($result);
$count = $row['numThings'];

Categories