0

How to get the data of a specific user in php?

Now I have created a login form with a session what I need now is to get her information like her balance points, her score in her welcome page. A sample script will help.

23rd Apr 2018, 7:05 AM
Muthiah Abbhirami
Muthiah Abbhirami - avatar
9 Réponses
+ 1
I assume you have a database in which you have this information stored. This example will be for MySQL. On login you only know the username and password of the person, both of which should be in the user table. So when the login form submits: $mysql = new mysqli("host url", "username", "password", "database"); $query = "SELECT * FROM `users` WHERE `username` = '".$_POST['username']."' AND `password` = '".$_POST['password']."'"; $result = $mysql->query($query); if($result->num_rows > 0) { session_start(); $_SESSION['user'] = $result->fetch_assoc(); // do something like send to dashboard or whatever } else { // no match for username and/or password } that's it. ideally the password would be encrypted and you'd sanitize the inputs for injection before submitting to the database, but now you can access all the info stored about the user by hitting $_SESSION['user']['balancePoints'] or $_SESSION['user']['welcomeScore']
23rd Apr 2018, 7:54 AM
Adam
Adam - avatar
+ 1
@Adam Thank you Sir! :)
23rd Apr 2018, 9:44 AM
Muthiah Abbhirami
Muthiah Abbhirami - avatar
+ 1
@Adam Can you let me know what is the error is this code?? html> <?php $query= mysql_query("SELECT Credit, Balance FROM tbl_Members WHERE `MemberId` = '".$_SESSION['MemberId']."' ")or die(mysql_error()); $arr = mysql_fetch_array($query); $num = mysql_numrows($query); ?>  //... <?php if($num > 0){ ?> <table border="1" cellpadding="3"> <tr><td colspan="2" align="center">Your Info</td></tr> <tr>  <td>Credit: <?php echo $arr['Credit']; ?></td> </tr> <tr>  <td>Balance: <?php echo $arr['Balance']; ?></td> </tr> </table> <?php }else{ ?>  User not found. <?php } ?>  //... </html>
23rd Apr 2018, 4:17 PM
Muthiah Abbhirami
Muthiah Abbhirami - avatar
+ 1
You have mysql_numrows. Should be mysql_num_rows
23rd Apr 2018, 6:59 PM
Adam
Adam - avatar
+ 1
I like to add 2 lines to my php files while doing development ini_set('display_errors', 1); error_reporting(E_ALL);
23rd Apr 2018, 10:11 PM
Adam
Adam - avatar
+ 1
Connection string needs to change: $serverName = "serverName\instanceName"; $connectionInfo = array("Database" => "dbName", "UID" => "userName", "PWD" => "password"); $conn = sqlsrv_connect($serverName, $connectionInfo); How you query changes also $query = "query string"; $result = sqlsrv_query($conn, $query); // if multiple rows while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) { //do stuff with rows here } // if single row $row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC); PHP Documentation is excellent and full of examples from the community. Here are some of the doc pages relating to this: http://php.net/manual/en/function.sqlsrv-connect.php http://php.net/manual/en/function.sqlsrv-query.php http://php.net/manual/en/function.sqlsrv-fetch-array.php More related sqlsrv php functions can be found in the list on the right side nav column.
24th Apr 2018, 3:54 PM
Adam
Adam - avatar
0
@Adam Can you kindly let me know how to code this if the DB is MS SQL? Please help. I'm stuck. I tried but I am not able to retrieve the data.
24th Apr 2018, 12:52 PM
Muthiah Abbhirami
Muthiah Abbhirami - avatar
0
I al not an MSSQL guy. I'll have to Google it and get back to you.
24th Apr 2018, 2:22 PM
Adam
Adam - avatar
0
@Adam Thank you! I'm trying to find this as well but I know nothing about. Hope you can help.
24th Apr 2018, 2:24 PM
Muthiah Abbhirami
Muthiah Abbhirami - avatar