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
Related
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
}
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().
I'm trying to find the number of rows in a table in the database depending on certain conditions with a limit feature so that i can count the number of rows there are matching the condition after a certain row in the table.
Thus i created my php query:
$q = $db->query("SELECT u.*, f.* FROM updates U LEFT JOIN friends f ON f.fid = u.userid WHERE f.uid = '$userid' ORDER BY u.up_id DESC LIMIT $limitID, 9999999");
$nr = $q->num_rows;
However even if there are more rows in the database after the $limitID, it says there are no rows.
If I try this:
$q = $db->query("SELECT u.*, f.* FROM updates U LEFT JOIN friends f ON f.fid = u.userid WHERE f.uid = '$userid' ORDER BY u.up_id DESC LIMIT $limitID");
$nr = $q->num_rows;
then it works, but it doesnt count after the $limitID. Any udeas?
According yo your query, it should be like
$sql = "SELECT count(*) FROM updates U
LEFT JOIN friends f ON f.fid = u.userid WHERE f.uid = ?";
// getting stuff using prepared statements
$row = $res->fetch_row();
$num = $row[0] - $limitID;
but I doubt it's really what you need either.
Using what Your Common Sense told me I managed to find an answer to my problem, so here it is:
$limitID = $upid;
$sql = "SELECT count(*) FROM updates u
LEFT JOIN friends f ON f.fid = u.userid WHERE
(f.uid = '$userid' AND u.up_id > '$limitID')";
$res = $db->query($sql);
$row = $res->fetch_row();
$num = $row[0];
if($num == 0){
// do nothing
} else {
echo "<span class='newPosts'>" .$num. " New Post"
. (($num>1)?'s':' ') . "</span>";
}
works perfectly
<?php
mysql_connect("localhost","root","");
mysql_select_db('db2012');
$uid = 8;
$mid = 10;
$q = mysql_query("select friend from users_friends where user = $uid") or die(mysql_error());
if(mysql_num_rows($q) > 0)
{
while($r=mysql_fetch_array($q))
{
$qq = mysql_query("select friend from users_friends where user = $mid") or die(mysql_error());
while($rr = mysql_fetch_array($qq))
{
if($r[friend]==$rr[friend])
{
$friend_name_query = mysql_query("select name from users where uid = '$rr[friend]'") or die();
$friend_name = mysql_fetch_array($friend_name_query);
echo $friend_name[name];
}
}
}
}
?>
This query is working but any other way out to use less queries ? i m a beginner programmer please let me know if there is n e thng ...
You should use just one query:
$query = 'SELECT u.name
FROM users_friends f1
INNER JOIN users_friends f2 ON (f2.friend = f1.friend)
INNER JOIN user u ON (u.uid = f2.friend)
WHERE f1.user = "'. $uid. '"
AND f2.user = "'. $mid .'"';
run the query
$q = mysql_query($query) or die(mysql_error());
and do the while loop
while($r=mysql_fetch_array($q)){
echo $r['name'];
}
You only need the if(mysql_num_rows($q) > 0) statement if you do an else, otherwise you can just use the while
You could try below in order to get mutual friends of user 8 and 10.
select * from
user_friends a inner join user_friends b on
a.friend=b.friend and (a.user='8' and b.user='10')
U could then join the user table to get the name.
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'];