I have two tables set up in SQL... One for users, which works for users to log in and out completely fine...
And another to pull a timetable into a html table, which works fine when a user isn't specified... But I'm trying to include a WHERE statement to make only information with the correct user_id (in the timetable) to show, dependant on the users session id.
I'm only receiving errors with this line...
$sql = "SELECT * FROM time_table ORDER BY Number WHERE $_SESSION['user_id']";
$result = mysql_query($sql)or die(mysql_error());
Any advice on where I'm going wrong here?
You are writing a query with wrong syntax.
The query should be like
$sql = "SELECT * FROM time_table WHERE user_id ='".$_SESSION['user_id']."' ORDER BY Number";
Related
I'm trying to get a list of all users who have received a message (at least) from a session user while excluding those who did not.
But my code excludes only one despite there being more in this category.
Here is my code:
$SQL="select *from message where senderID="$sender"; $result=mysqli_query($link,$SQL);$totalRows=mysqli_num_rows($result);if($totalRows<>0){for($i=0;$i<$totalRows; $I){$resultData[$i]=mysqli_fetch _array($result);
}for($i=0;$i<$totalRows; $I){$receiverID=$resultData[$i]['receiver_id'];
$select="select *from users where memberID NOT IN($receiverID)";} }
Please, how do I achieve this goal?
You can do all that in one SQL query:
$SQL = "SELECT * FROM users WHERE memberID NOT IN (SELECT receiver_id FROM message WHERE senderID = '$sender')";
I have no knowledge of PHP, but you will have to consult its documentation for the correct use of for loops to retrieve the data.
I'm trying to get my site to only output a specific amount of data from my database, and not just the whole thing. Can anyone help me with this? Im trying to get it to display the first 25 lines of data.
Right now my php code, is as follows:
$sql = "SELECT * FROM cards WHERE name LIKE '%$query%';";
$sql = "SELECT * FROM cards WHERE name LIKE '%$query%' LIMIT 10;";
if you are using MySql u can use LIMIT to get a limited number of records.
If you are using MSSQL you can use TOP
hope this helps.
Try it:
$sql = "SELECT * FROM cards WHERE name LIKE '%$query%' LIMIT 0,25;"
My database set up may become cluttered with data from old users due to there being so many different tables which contain user information.
I am looking to run a cron job which will search ap_users (the main user table) for user_id and then use that information to check all other tables for any user_id which does not exist in the ap_users table and delete/remove those records.
I am wondering what would be the quickest and most efficient way of doing this? As I am unsure on how to check for a user_id which doesn't exist?
I have attempted something like this which doesn't seem to work, once I can get this working, I can progress it to the rest of the database:
$mysqli_find = mysqli_query($conn, "SELECT * FROM ap_user_points WHERE user_id NOT IN (SELECT user_id FROM ap_users group by user_id)");
while ($found_rows = mysqli_fetch_array($mysqli_find)){
$user_id = $found_rows['user_id'];
echo $user_id;
}
I have a simple function below to check if the user is already registered on my site. If they are then it doesn't add them and if they aren't it adds them. However In my database for some weird reason a certain user is getting added multiple times with the exact same id. Is there something wrong with mysqli_num_rows?
$check = mysqli_query($con, "SELECT id FROM users WHERE id='$id'");
$row_cnt = mysqli_num_rows($check);
if ($row_cnt == 0) {
$query = "INSERT INTO users (id,username) VALUES ('$id','$username')";
mysqli_query($con, $query);
} else { // If Returned user . update the user record
$query = "UPDATE users SET username='$username' where id='$id'";
mysqli_query($con, $query);
}
A few things here.
First of all, num_rows() notoriously doesn't always return the right value. If you want to know how many rows match some criterion, use this query then read the rowcount in the one-row resultset.
SELECT COUNT(*) AS rowcount FROM users WHERE id='$id'
Second, it looks like you're trying to do a conditional insert / update operation. In this case you will be best off using Use the INSERT ... ON DUPLICATE KEY UPDATE mySQL command. This gets around the race condition that is in the code in your question.
You want a query something like this:
INSERT INTO users (id,username) VALUES ('$id','$username')
ON DUPLICATE KEY UPDATE username='$username';
http://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html
This will either add the row you want or change the username in the existing row, all at once. If some other user tries to do the same thing at the same time, this will do it safely.
i would like to ask you about my database query
i have this table admin_table in my data base
tag id name password profession
xxxxxx Jhon Begly 123 admin
i build a php page to insert a user name and password
i need to check the user name and password that i entered with its profession if its valid then proceed with a new page as bellow
$sql = "SELECT * FROM admin_table WHERE name='".$username."' AND password='".$password."'";
so how can i check profession in the same query above
any update will be highly appreciated
You mean this?
$sql = "SELECT * FROM admin_table WHERE name='".$username."'
AND password='".$password."' AND profession = 'admin'";
If you get a result row back, you matched all three columns. If zero rows, it failed to match at least one. Not really sure your use case.
I also agree with other commenters that this is far from best practices in several ways.
Where are you validating the profession? If it is in another table... use
INNER JOIN profession_table as pt
ON pt.some_column
right after the from part of your query