I'm using a row to return all of the users for a particular project, which isn't a problem. However, how would I order it so that the first result is always the logged in user.
I see two solutions, both of which I've tried with no success:
Printing the user's name then using an if statement to only print the rest of the row if they are different from the user's name.
print user's name;
if (username !== user's name) {
print this name;
}
Printing them all in a row but ordering it somehow so that the current user is at the top.
Any ideas?
EDIT
I'm trying it now with the following code:
$query7 = mysql_query("SELECT users_ID FROM projects_users WHERE projects_id = '$id' AND WHERE users_id <>'$q6[0]'") or die(mysql_error());
But I get the error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE users_id <>'1'' at line 1
I've tried != and NOT instead of <> but I can't see what's going wrong!
Never mind, fixed it. For future reference, don't sure WHERE twice ;)
Thanks for your help everyone.
You first solution is perfect.
If you want to use your second solution you should build your SQL query like this:
(SELECT * FROM user WHERE id = :currentUserID) UNION (SELECT * FROM user)
And you are good
I think you could do this with a simple mysql query:
SELECT * FROM table_name ORDER BY FIELD(username, 'username');
To learn more:
http://dev.mysql.com/doc/refman/5.0/en/sorting-rows.html
SELECT *
FROM USERS
ORDER BY (CASE user_id WHEN :currentUserID THEN 1 ELSE 2 END);
if you want to use php than, You can try something like this
$currentuser = "";
$otherusers = "";
if(username == user's name)
{
$currentuser = username;
}
else
{
$otherusers .= username;
}
echo $currentuser;
echo $otherusers;
Related
By the way, before it is mentioned, I am well aware I should be using mysqli. Thanks in advance.
This is my code:
$q5 = "select listingid FROM userlisting WHERE userid = '$_SESSION[UserID]'";
$r5 = mysql_query($q5) or die(mysql_error());
$a5 = mysql_fetch_array($r5);
The userlisting table is a 'lookup' table and has two columns:
userid and listingid
It has a many to many relationship. In other words, there could be one userid attached (associated) to multiple listingids and thus having multiple rows in that table.
e.g.
userid|listingid
1|1
1|2
1|3
2|1
etc
To keep things simple: What I want to do is check the following:
$a5['listingid'] == $_GET['id']
And if it is True I will display information and if it is False the information will not be displayed.
So on the page mywebsite.com there will be an id as so, mywebsite.com?id=[id here]. I am trying to see if the user $_SESSION[UserID] has an entry in userlisting table that matches the id of the page (well, it is a property website and the id is that of the property listing).
At the moment the code I have above just searches/checks for the first row for that userid only. In the example I gave above that would be listingid ='1' It is not seeing that row 2 and 3 also have entries in them too, listingid = '2' and '3' respectively. So on mywebsite.com?id=1 it is true, but on ?id=2 and id=3 it is coming up false, but userid = 1 has three rows with entries 1, 2 and 3.
I have been trying to find a solution for a while and I am starting to feel frustrated now. I would much appreciate it if someone could come up with a quick solution for me.
You can check both on SQL with some clause like
WHERE userid=XX AND listingid=XX
And remember to escape the get parameter ;)
PS: You can use too a while for iterate the mysql_fetch_row and search if anyone is correct. Something like:
$correct_check = false;
while($a5 = mysql_fetch_array($r5)) {
if($a5['listingid'] == $_GET['id']) $correct_check = true;
}
if($correct_check) ....
else ....
Try something like this
$page_id = $_GET['id'];
$q5 = "select listingid FROM userlisting WHERE userid = '$_SESSION[UserID]' and listingid = '$page_id' ";
$res = mysql_qury($result);
$num_rows = $mysql_num_rows($res);
if($num_rows > 0)
//your ok code
else
//fail message
$getwhole_messages = $db->query("
SELECT * FROM `user_messages`
WHERE `sender_id`='".$_SESSION['RVuser']."' ||
`reciever_id`='".$_SESSION['RVuser']."' &&
`sender_id`='".$_GET['uid']."' ||
`reciever_id`='".$_GET['uid']."'"
);
Where as I want to put check that will let me know that session id is match with uid. It would be in sender_id or reciever_id.
Too long for a comment, beyond what tadman said (You're at severe risk of SQL injection attacks), this query is left way too open.
You have this matching when any of these conditions exist:
Condition 1: When sender_id = $_SESSION['RVuser']
Condition 2: When receiver_id = $_SESSION['RVuser'] AND sender_id = $_GET['uid']
Condition 3: When receiver_id = $_GET['uid']
So all it would take is for someone to load your page with ?uid=# to get information because of condition 3.
Always sanitize vulnerable inputs! I'm assuming you're using MySQLi in my example.
$select = sprintf("SELECT * FROM user_messages WHERE sender_id=%d || reciever_id=%d && sender_id=%d || reciever_id=%d;",
$_SESSION['RVuser'],
$_SESSION['RVuser'],
$db->real_escape_string($_GET['uid']),
$db->real_escape_string($_GET['uid'])
);
if($getwhole_messages = $db->query($select))
// Prosess results
} else {
// Output Query error
}
Please provide error details so we can provide more of an answer.
I tried searching for this question, but theirs is a different error. I'm using PHP 5.2 for some reason.
What I'm trying to do is, get the mysql_fetch_array from one table, then use a column of that table to get a row from another table.
Here is my code (Advanced apologies for the horrible format)
$sql = mysql_query("SELECT * FROM testimonies WHERE visibility != 'Hide' ORDER BY date_submitted DESC");
$testimonyCount = mysql_num_rows($sql); // count the output amount
if ($testimonyCount > 0) {
//get data 'testimonies' table
while($info = mysql_fetch_array($sql)){
$username = $info['username'];
//Get id (in admin table) where username = username (in testimonies table)
$userid = mysql_query("SELECT * FROM admin WHERE username = $username LIMIT 1");
while($user = mysql_fetch_array($userid)){ $id = $user['id'];}
$testimonies .= "<div class='row'><div class='col-lg-2'><center>
<img src='Pictures/Profile_Pictures/".$userid['id'].".jpg' width='160' height='160'>
<br>".$info['username']."</center></div><div class='col-lg-2'><center>
<img src='back/inventory_images/34.jpg' width='160' height='160'><br>"
.$info['product_used']."</center></div><div class='col-lg-8'><center><u>(Date: "
.$info['date_submitted']." - Ordered from our branch in <strong>"
.$info['branch_ordered']."</strong>, City)</u></center>".$info['testimony']."
</div></div><br>";
}
}
else {
$testimonies = "No one has submitted their testimonies yet.";
}
So you see, my table "testimonies" have a column of 'username' and so is the "admin" table.
What' I'm trying to do is get the row of the username (in admin table) where the username is the
same as the one in the "testimonies" table. I used the information from the 2nd table in line 14 for the image src.
Unfortunately it gives me this error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/_____/public_html/Testimonies.php on line xx
Notice: Undefined variable: id in /home/_____/public_html/Testimonies.php on line xx
Other's from what I've searched get their data on the first loop, but I don't get anything at all. The 2nd loop is certainly a problem. Help.
Thanks.
First I'll mention that it's a really bad idea to build queries in a such a way that they can be looped. As is for each of your testimonies you're executing another query to get the admin info. So for 10 testimonies you're hitting MySQL 10 times [ +1 for the original query ]. You generally want to get in, get your data, and get out.
So the better way to handle this is to let MySQL do it for you, with a join :
SELECT *
FROM testimonies inner join admin on testimonies.username = admin.username
WHERE visibility != 'Hide'
ORDER BY date_submitted DESC
The above will only return you testimonies with a matching admin. If you wanted to return testimonies even if they don't have an admin you'd want an outer join. You'd replace inner join admin with left outer join admin
So remove your second while loop and try that.
Also, the mysql_ functions are deprecated .. so you shouldn't be developing new code using them. Try and get my change to work as-is but you should consider looking at PDO [ Why shouldn't I use mysql_* functions in PHP? ].
I have a PHP chat script that calls a MySQL database when a user signs out to delete them from the database.
My script is:
if(isset($_GET['logout'])){
mysql_query("DELETE FROM users WHERE username='" .$user['username']. "' AND rank='0'");
header("Location: login_mini.php?logout=1");
}
What I want to do is delete the user if they have a rank of 0 when they leave. Why isn't this script working?
DELETE doesn't take column arguments
Remove the *
The syntax for MYSQL Delete example:
DELETE FROM somelog WHERE user = 'jcole'
ORDER BY timestamp_column LIMIT 1;
So you're query is wrong that's the reason why it is not running:
It should be
//without * and add quotes in your $user['username']
mysql_query("DELETE FROM users WHERE username=" .$user['username']. " AND rank='0'");
mysql_query("DELETE FROM users WHERE username='$user[username]' AND rank=0");
There is no * or any columns in DELETE operation because you are deleting the whole row(s).
Are you sure that users table have a record that have rank == 0 ?
Check it by
SELECT COUNT(*)
FROM users
WHERE rank='0'
then if there is check your variable $user['username'] if it has value.
var_dump($user);
then if both has value then try to execute this manually on your mysql
SELECT *
FROM users
WHERE username = #the value of the username
AND rank = '0'
If there is a result then maybe your PHP is throwing an error while executing the mysql_query. try to insert this code after the mysql_query
if (mysql_error()) {
die(mysql_error());
}
I am having some difficulty running some SQL code.
What I am trying to do is, find a row that contains the correct username, and then get a value from that correct row.
This is my SQL in the php:
mysql_query("SELECT * FROM users WHERE joined='$username' GET name")
As you can see, it looks for a username in users and then once found, it must GET a value from the correct row.
How do I do that?
You need some additional PHP code (a call to mysql_fetch_array) to process the result resource returned by MySQL.
$result = mysql_query("SELECT name FROM users WHERE joined='$username'");
$row = mysql_fetch_array($result);
echo $row['name'];
mysql_query("SELECT `name` FROM users WHERE joined='$username' ")
Just select the right column in your 'select clause' like above.
Edit: If you are just starting out though, you might want to follow a tutorial like this one which should take you through a nice step by step (and more importantly up to date functions) that will get you started.
mysql_query("SELECT name FROM users WHERE joined='$username'")
$q = mysql_query("SELECT * FROM users WHERE joined='$username'");
$r = mysql_fetch_array($q);
$name = $r['user_name']; // replace user_name with the column name of your table
mysql_query("SELECT name FROM users WHERE joined='$username' ")
Read documentation : http://dev.mysql.com/doc/refman/5.0/en/select.html