Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql statement - php

Posted on 2016-09-20
5
Medium Priority
?
75 Views
Last Modified: 2016-09-20
Dear Experts,
I'm using Ray Paseur PHP Client Registration, Login, Logout and Easy Access Control pages.

I have created a database for my users and I already set their user name and password for them. However when I want to set uuk value for all the users with below code it set all uuk value the same.
However I want all the uuk codes unique.
$uuk = md5($uid . $pwd . rand());

How can I set every uuk code unique for every user?

my code:

$sql = "SELECT _key,uid,pwd FROM KullaniciBilgileri where uuk='' ";
$result = $conn->query($sql);


    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["_key"]. " - İsim: " . $row["uid"]. " " . $row["pwd"]. "<br>";
            
      $_key= $row["_key"];
      $uid= $row["uid"];
      $pwd= $row["pwd"];
            
      }
      
$uuk = md5($uid . $pwd . rand());

$sql2 = "UPDATE KullaniciBilgileri SET uuk = '$uuk'";
$result2 = $conn->query($sql2);
0
Comment
Question by:Braveheartli
  • 3
5 Comments
 
LVL 9

Accepted Solution

by:
Mukesh Yadav earned 2000 total points
ID: 41806238
Try this ;)

To make it unique you need to put it in while loop:
 while($row = $result->fetch_assoc()) {
     echo "id: " . $row["_key"]. " - İsim: " . $row["uid"]. " " . $row["pwd"]. "<br>";
     $uuk = md5($row["uid"] . $row["pwd"] . rand());

    $sql2 = "UPDATE KullaniciBilgileri SET uuk = '$uuk' WHERE id = '" . $row["id"] . "'";
    $conn->query($sql2);
}


/* make sure you select id column to identify and update a single row, I assumed this column name id you can use whatever you are using with primary key */
So your select query would be like this:

$sql = "SELECT id, _key,uid,pwd FROM KullaniciBilgileri where uuk='' ";
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 41806256
Why not just run this query directly - let the database do the work
$sql = <<< QUERY
UPDATE KullaniciBilgileri 
  SET 
     uuk = MD5(CONCAT(uid,pwd, rand())) 
  WHERE 
      uuk=''
QUERY;
$conn->query($sql);

Open in new window

2
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 41806257
In fact - if all you are doing is creating an unguesssable key then this is better
$sql = <<< QUERY
UPDATE KullaniciBilgileri 
  SET 
     uuk = UUID() 
  WHERE 
      uuk=''
QUERY;
$conn->query($sql);

Open in new window

1
 
LVL 1

Author Closing Comment

by:Braveheartli
ID: 41806312
thank you very much
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 41806324
Just a note to anyone reading this - there is no benefit to using script to do what can be done with a simple SQL query. Where possible let the database do the work and resort to script only when what needs to be done falls outside of what is possible with a query.
2

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question