Android Question reading data from database and evaluating

jchal

Active Member
Licensed User
Longtime User
dear all hi
i am trying to read some data from the msql databse but i canot do it .......
on my data base i have user_id,user_name,password,Online,logins,time_stamp, interval (is integer 1 to ....200 etc),dateexp is a date
what i need to do is to read the interval and the dateexp and store the interval in a viriable eg myinterval and the dateexp i want to cheack against the pass word validation
for example if password is correct and dateexp> to date today then ok else message "your date has expired"
you must also have to know that i have been througth the
https://www.b4x.com/android/forum/threads/licence-and-expired-date.65547/
https://www.b4x.com/android/forum/t...h-a-server-using-httputils2-part-1-php.42442/
https://www.b4x.com/android/forum/t...a-server-using-httputils2-part-2-mysql.42456/
https://www.b4x.com/android/forum/t...httputils2-part-3-php-mysql-json.42663/page-3

my php code is the following
B4X:
<?
$host = "..........";
$db = "......";
$user = ".........";
$pw = ".......";

$con = mysql_connect($host, $user, $pw) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES 'utf8'");

$uid = mysql_real_escape_string($_GET["user_id"]);
$pwd = mysql_real_escape_string($_GET["password"]);

$res = mysql_query("SELECT user_name, status FROM tbl_member WHERE user_id = '$uid' AND pass_word = '$pwd'");
if (!$res) {
    print json_encode("Error");
    echo "<br />" . mysql_error();
    exit;
}
else {
    if (mysql_num_rows($res) == 0) {
        print json_encode("Not Found");
        exit;
    }
    $row = mysql_fetch_row($res);
    if ($row[1] == "M") {
        print json_encode("Not Activated");
    }
    else {  
        print json_encode($row[0]);
        $res = mysql_query("UPDATE tbl_member SET Online = 'N' WHERE now()-time_stamp > 60");
        $res = mysql_query("UPDATE tbl_member SET logins = logins + 1, Online = 'Y', time_stamp = now() WHERE user_id = '$uid'");  
    }
}
?>
and the b4a code is the following
B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: False
#End Region

Sub Process_Globals
    Dim strUserID As String
    Dim strUserName As String
End Sub

Sub Globals
    Dim txtUserID As EditText
    Dim txtPassword As EditText
    Dim lblMessage As Label
    Dim btnforgotpwd As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("frmLogin")
End Sub

Sub Activity_Resume
'    StartActivity("Main")
End Sub

Sub Activity_Pause (UserClosed As Boolean)
 
End Sub

Sub btnLogin_Click
    'Dim strUserID As String = txtUserID.Text.Trim
    lblMessage.Text = ""
    strUserID = txtUserID.Text.Trim
    If strUserID = "" Then
        Msgbox("Please enter User ID", "Error")
        Return
    End If 
    Dim strPassword As String = txtPassword.Text.Trim
    If strPassword = "" Then
        Msgbox("Please enter Password", "Error")
        Return
    End If 
 
    Dim Login As HttpJob
    Login.Initialize("Login", Me) 
    Login.Download2("http://www.mydomain.com/signin.php", _
    Array As String("user_id", strUserID, "password", strPassword))
    ProgressDialogShow("Connecting to server...")
End Sub

Sub JobDone (Job As HttpJob)
    ProgressDialogHide
    If Job.Success = True Then
        Dim ret As String
        Dim act As String     
        ret = Job.GetString      
        Dim parser As JSONParser
        parser.Initialize(ret)     
        act = parser.NextValue
        If act = "Not Found" Then
            ToastMessageShow("Login failed", True)
            lblMessage.Text = "Wrong User ID or Password!"
            lblMessage.TextColor = Colors.Red
        Else If act = "Not Activated" Then
            ToastMessageShow("Login failed", True)
            lblMessage.Text = "Account is not activated!"
            lblMessage.TextColor = Colors.Blue         
        Else If act = "Error" Then
            ToastMessageShow("Login failed", True)
            lblMessage.Text = "An error has occured!"
            lblMessage.TextColor = Colors.Red
        Else
            strUserName = act
            StartActivity("Member")
            Activity.Finish
        End If
    Else
        'Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub
i will apriciate if you can show me how to alter the code
 

KMatle

Expert
Licensed User
Longtime User
but i canot do it

Just do it in small steps to learn. After that optimize the Select statement and your code.

So select ALL columns from that table first

B4X:
Select ..., ..., ..., .... FROM tbl_member WHERE user_id = '$uid'...
 $row = mysql_fetch_row($res);

So you can check the columns with simple IF ... THEN ... ELSE structures

B4X:
if ($row[1] == '$pwd')
{
      print json_encode("Login Successful");
}
ELSE
{
Update Login tries and Exit(json_encode("UserID/PW wrong"));
}

... (next IF THEN ELSE)

As you noticed yet $row[1] contains the 2nd column $row[0] will contain the 1st column.

EXIT leaves the script (so you don't need nested IF THEN ELSE constructs)

Next: check for the date and your other rules inside then next simple IF ... THEN ... ELSE. Here you can Update columns, too. Keep it simple!

Later you can optimize you Select statement like this:

B4X:
$q = mysqli_query($con,"SELECT userroles FROM xxxxxxx Where username = '$username' and userpw='$userpw' and logintries <4 and validfrom <= current_date and voidfrom > current_date") or die(mysqli_error($con));

A very good site with thousands of examples: https://www.w3schools.com/sql/func_date_add.asp
 
Upvote 0
Top