I use a library that helps me with db functionality. This is a valid syntax:
if ($memID = $db->get_var("SELECT id FROM users WHERE social_id = ".$_SESSION['user'])) {
// user found
$db->query("
UPDATE users
SET
nameF = '".$NameF."',
nameL = '".$NameL."'
WHERE id = ".$memID."
LIMIT 1");
} else {
// user not found
}
I need to add additional checks:
if ($memID = $db->get_var("SELECT id FROM users WHERE social_id = ".$_SESSION['user']) ||
$memID = $db->get_var("SELECT id FROM users WHERE email = '".$Email."' AND password IS NULL") ||
($_COOKIE['socid'] != '' && $memID = $db->get_var("SELECT id FROM users WHERE FIND_IN_SET(social_id, '".$_COOKIE['socid']."'))
){
}
But I think I'm over complicating this and introduce errors along the way. What am I doing wrong?
Related
I need help with update my column in database. I use InnoDB, probably problem is here
$sql_update_heslo = "UPDATE users SET u_password = $_noveHeslo WHERE u_name = '$_SESSION[username]'";
first I am checking if Button was clicke. If yes, then I am checking if there is only 1 user with this name who is logged in, then I am checking if MD5 password from the database is same as user input, if yes then update password based on the user entry.
if (isset($_POST['pass_aktualizovat'])) {
$_old_password = md5($_POST['o_pass']);
$sql_search_for_all_userss = "SELECT * FROM users WHERE u_name = '$_SESSION[username]' ";
$result = mysqli_query($connect_to_db, $sql_search_for_all_userss);
// ak sa najde jedna zhoda v databazy
if ($db_data = mysqli_num_rows($result) == 1) {
while (mysqli_fetch_assoc($result)) {
$_aktualneHeslo = $db_data['u_password'];
}
if (md5($_POST['o_pass'])==$_aktualneHeslo) {
$_noveHeslo = md5($_POST['n_pass']);
$sql_update_heslo = "UPDATE users SET u_password = '$_noveHeslo' WHERE u_name = '".$_SESSION['username']."'";
mysqli_query($connect_to_db, $sql_update_heslo);
echo "treti";
}
echo "druhy";
}
echo "prvy";
}
?>
How can I optimize this function? I need to loop though users, and then loop through user submitted data, and update values, it's taking > 1 minute for 264 users with around 80 records per user.
public function calculateUserPoints(){
set_time_limit(0);
/* Select users */
$sql = "SELECT user_id FROM users";
$query = $this->db->prepare($sql);
$query->execute();
$users = $query->fetchAll(PDO::FETCH_ASSOC);
/* User guesses Query */
$ug_sql = "SELECT
guesses.guess_id AS r_guess_id, guesses.user_id, guesses.game_id, guesses.team_1_score AS guess_team_1, guesses.team_2_score AS guess_team_2, guesses.joker,
games.game_id, games.game_team_1, games.game_team_2, games.real_score_team_1, games.real_score_team_2,
users.user_id AS usr_id
FROM games
JOIN users
ON users.user_id = :user_id AND games.real_score_team_1 IS NOT NULL AND games.real_score_team_2 IS NOT NULL
LEFT JOIN guesses
ON guesses.user_id = users.user_id
AND guesses.game_id = games.game_id
";
$ug_query = $this->db->prepare($ug_sql);
foreach($users as $u){ /* Loop users */
/* Get guesses per user basis */
/*echo 'User '.$u['user_id'].'<br/>';*/
$ug_query->bindParam(':user_id', $u['user_id']);
$ug_query->execute();
$usr_guesses = $ug_query->fetchAll(PDO::FETCH_ASSOC);
$u_points = 0;
$sql = "UPDATE users SET points = :u_points WHERE user_id = :usr_id";
$query = $this->db->prepare($sql);
$sql2 = "UPDATE guesses SET guess_points = :guess_points WHERE guesses.guess_id = :guess_id";
$query2 = $this->db->prepare($sql2);
foreach($usr_guesses AS $ug){
$err = false;
$g_points = 0;
if(isset($ug['guess_team_1']) && isset($ug['guess_team_1'])){
$g_points = $this->calcPoints($ug['guess_team_1'], $ug['guess_team_2'], $ug['real_score_team_1'], $ug['real_score_team_2'], $ug['joker']);
} else {
$u_points -= 1;
}
$u_points += $g_points;
/* echo $ug['guess_team_1'].' - '.$ug['guess_team_2'].' :: '.' '.$ug['real_score_team_1'].' - '.$ug['real_score_team_2'].' jk: '.$ug['joker'].' / pt: '.$g_points.':: T: '.$u_points.'<br/>';*/
$query->bindParam(':u_points', $u_points);
$query->bindParam(':usr_id', $u['user_id']);
$query2->bindParam(':guess_points', $g_points);
$query2->bindParam(':guess_id', $ug['r_guess_id']);
if($query->execute() && $query2->execute()){
$err = false;
} else {
$err = true;
}
}
}
if($err == true){
return false;
} else {
return true;
}
}
guess_id and user_id are indexes on all columns.
I'm not looking for a code answer, I'd prefer merely a push in the right direction.
Thanks.
$sql = "SELECT user_id FROM users"; can be joined with $ug_sql query into one query.
If you use InnoDB tables, you can do the updates in transaction, it will speed it up.
And think about removing some indexes, they slow down inserts, updates, and deletes.
Here is the code
if ($st) active_code = '1';
if (!$st) active_code > '0';
SELECT username FROM users WHERE active_code = '1'
SELECT username FROM users WHERE active_code > '0'
Is there is a way to make then one sql query ?
Any idea please ?
$cond = $st ? "= '1'":" > '0'";
$sql = "SELECT username FROM users WHERE active_code $cond";
Just define a variable that will hold the condition:
if ($st) {
$condition = "active_code = '1'";
} else {
$condition = "active_code > '0'";
}
$sql = "SELECT username FROM users WHERE $cond";
I have a problem with an query that won't work.
The one that needs to set the rank to 2 works, but the one that needs to set vip to 1 doesn't work.
I just get an white page.
What is the problem?
<?php
session_start();
include ("includes/config.php");
$lid = $_SESSION['lid'];
$uQuery = mysql_query("SELECT * FROM users WHERE id = '".$lid."'");
while($uFetch = mysql_fetch_array($uQuery)){
$uuser = $uFetch['username'];
$umotto = $uFetch['motto'];
$ucredits = $uFetch['credits'];
$upixels = $uFetch['activity_points'];
$ubelcr = $uFetch['belcredits'];
$urank = $uFetch['rank'];
$ufigure = $uFetch['look'];
}
if($urank < '2'){
mysql_query("UPDATE users SET rank = 2 WHERE id = '".$lid."'");
mysql_query("UPDATE users SET vip = 1 WHERE id = '".$lid."'");
}
Header("vip.php?succes=1");
?>
mysql_query("UPDATE users SET rank = 2,vip = '1' WHERE id = '".$lid."'");
Not exactly what you're looking for, but looking at the queries you could simplify to:
session_start();
include ("includes/config.php");
if(isset($_SESSION['lid'])){
$lid = $_SESSION['lid'];
$query = "UPDATE users SET rank = 2, vip = '1' WHERE rank < 2 AND id = ".intval($lid);
$result = mysql_query($query) or die(mysql_error());
header('Location: vip.php?succes=1');
exit;
}
i'm using php to make some mods on my database, i have two identical tables and i want to move one row from the first table to the second one how can i do that using pure php and mysql.
that is how my tables looks like
CREATE TABLE users (
username varchar(30) primary key,
password varchar(32),
userid varchar(32),
userlevel tinyint(1) unsigned not null,
email varchar(50),
timestamp int(11) unsigned not null
);
and here is my php code so far
function procMoveUser(){
global $session, $database, $form;
/* Username error checking */
$subuser = $this->checkUsername("user");
/* Errors exist, have user correct them */
if($form->num_errors > 0){
$_SESSION['value_array'] = $_POST;
$_SESSION['error_array'] = $form->getErrorArray();
header("Location: ".$session->referrer);
}
/* move the user */
else{
$q = "SELECT * FROM ".TBL_USERS." WHERE username = '$subuser'";
$result = $database->query($q);
if($result && $result->num_rows == 1){
while($array = $result->fetch_assoc() ){
$second_query = "INSERT INTO".TBL_USERSDONT."VALUES ($array['user'], $array['password'], $array['userid'] , $array['userlevel'] , $array['email'] , $array['timestamp'])";
$second_result = $mysqli->query($second_query);
if($second_result){
// it worked!
$q = "DELETE FROM ".TBL_USERS." WHERE username = '$subuser'";
$database->query($q);
}
}
}
}
}
First, SELECT * FROM the first table for the row that you want to move. Then, as suggested above, run an INSERT statement with the values from the first table.
$q = "SELECT * FROM ".TBL_USERS." WHERE username = '$username'";
$result = $mysqli->query($q);
if($result && $result->num_rows == 1){
while($array = $result->fetch_assoc() ){
$second_query = "INSERT INTO second_table VALUES ($array['user'], $array['something'])";
$second_result = $mysqli->query($second_query);
if($second_result){
// it worked!
}
}
}
Maybe this will help: Copy an existing MySQL table to a new table