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 :)
Related
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
I have two queries that count the number of data for both "artists" and "groups" in my database. I want to display a message if there is data to display for either artists or groups (or both), and if the data returns 0 for both of them then not to display anything.
I have the following code which doesn't seem to work:
<?php if (($numrowsartists==0)OR($numrowsgroups==0)) {
} else {
echo "There is information to display.";
}
?>
Below are the queries I have:
$query = "SELECT COUNT(*) FROM `Credit_To_Artist` AS c2a
INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
INNER JOIN `Artist` AS a ON a.artist_id = c2a.artist_id
WHERE c2a.song_id = $id";
$res = mysql_query($query);
$numrowsartists = mysql_fetch_assoc($res);
$query = "SELECT COUNT(*) FROM `Credit_To_Artist` AS c2a
INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
INNER JOIN `Artist_Group` AS ag ON ag.group_id = c2a.group_id
WHERE c2a.song_id = $id
ORDER BY ag.group_name ASC";
$res = mysql_query($query);
$numrowsgroups = mysql_fetch_assoc($res);
Thanks in advance. I'm sure it's probably a super basic fix but I'm still very new to php and would appreciate some help.
You should getthe value frorm the row eg using alias for column name
$query = "SELECT COUNT(*) as num_artists FROM `Credit_To_Artist` AS c2a
INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
INNER JOIN `Artist` AS a ON a.artist_id = c2a.artist_id
WHERE c2a.song_id = $id";
$res = mysql_query($query);
$row = mysql_fetch_assoc($res);
$numrowsartists = row['num_artists'];
$query = "SELECT COUNT(*) as num_groups FROM `Credit_To_Artist` AS c2a
INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
INNER JOIN `Artist_Group` AS ag ON ag.group_id = c2a.group_id
WHERE c2a.song_id = $id
ORDER BY ag.group_name ASC";
$res = mysql_query($query);
$row = mysql_fetch_assoc($res);
$numrowsgroups = row['num_groups'];
There are several solutions, the easiest being the following:
if($numrowsartists[0]+$numrowsgroups[0] > 0)
However, as people have said, you shouldn't use mysql_* functions anymore.
Assuming the ID is user input, you should really use prepared statements.
Also, you can handle both tests in a single query:
$stmt = $con->mysqli_prepare("SELECT COUNT(1) as `count` FROM `Credit_To_Artist` AS c2a
INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
INNER JOIN `Artist` AS a ON a.artist_id = c2a.artist_id
INNER JOIN `Artist_Group` AS ag ON ag.group_id = c2a.group_id
WHERE c2a.song_id = ?");
$stmt->bind_param('i',$id);
$stmt->execute();
if($stmt->get_result()->fetch_array()[0] > 0){
...
}else{
//message that nothing was found
}
$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.
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'];
$activeQuery = mysql_query("SELECT count(`status`) AS `active` FROM `assignments` WHERE `user` = $user_id AND `status` = 0");
$active = mysql_fetch_assoc($activeQuery);
$failedQuery = mysql_query("SELECT count(`status`) AS `failed` FROM `assignments` WHERE `user` = $user_id AND `status` = 1");
$failed = mysql_fetch_assoc($failedQuery);
$completedQuery = mysql_query("SELECT count(`status`) AS `completed` FROM `assignments` WHERE `user` = $user_id AND `status` = 2");
$completed = mysql_fetch_assoc($completedQuery);
There has to be a better way to do that, right? I don't know how much I need to elaborate as you can see what I'm trying to do, but is there any way to do all of that in one query? I need to be able to output the active, failed, and completed assignments, preferably in one query.
You can try something like this query
SELECT Status , COUNT(*) StatusCount
FROM assignments
WHERE Status IN (0, 1, 2)
AND User = $user_id
GROUP BY Status
Try this
$activeQuery = SELECT status, count(status) as "status count" FROM `assignments` WHERE `user` = $user_id GROUP BY `status`
edit: added group by
Instead of doing them individually you could use the following single SQL statement
SELECT count(*), `status`
FROM `assignments`
WHERE `user` = $user_id
AND `status` in (0,1,2)
GROUP BY `status`
ORDER BY `status`
The loop around the result set to extract the results.
SELECT
(SELECT COUNT(*) FROM `assignments` WHERE `user` = $user_id AND `status` = 0) AS active,
(SELECT COUNT(*) FROM `assignments` WHERE `user` = $user_id AND `status` = 1) AS failed,
(SELECT COUNT(*) FROM `assignments` WHERE `user` = $user_id AND `status` = 2) AS completed,
FROM `assignments`
GROUP BY active, failed, completed
Haven't checked the markup, but this is near or near enough.
Use the GROUP BY to get it in one query, but on separate rows.
$query = mysql_query("SELECT `status`, count(*) AS `num` FROM `assignments` WHERE `user` = $user_id AND `status` in (0,1,2) GROUP BY `status` ORDER BY `status` ASC");
$active_count = 0;
$failed_count = 0;
$completed_count = 0;
while ($array = mysql_fetch_assoc($query))
{
if ($array['status'] == 0)
$active_count = $array['num'];
else if ($array['status'] == 1)
$failed_count = $array['num'];
else if ($array['status'] == 2)
$completed_count = $array['num'];
}
When I have the option, I'm in the habit of using prepared statement(s) to help protect against sql-injection protection and for efficiency (requires using mysqli).
But if this is not an option, then
SELECT
CASE `status`
WHEN 0 THEN `active`
WHEN 1 THEN `failed`
WHEN 2 THEN `completed`
ELSE `unknown`
END
AS `statuslabel`,
COUNT(status)
AS `statuscount`
FROM `assignments`
WHERE `user` = $escaped_user_id
GROUP BY `statuslabel`
ORDER BY `statuslabel`
Note the use of the $escaped_user_id, which would be created beforehand:
$escaped_user_id = mysql_real_escape_string($user_id);