MySQL array needs string just simply presented

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • loginNorm
    New Member
    • Aug 2010
    • 11

    MySQL array needs string just simply presented

    Here is the code:

    <body>
    <?php
    session_start() ;
    $connection=mys ql_connect("mys ql506.opentrans fer.com","C2609 87_??????","??? ???");
    $db=mysql_selec t_db("C260987_l oginsv2",$conne ction);
    $users_table = 'members_md5';

    $username = $_POST['myusername'];

    $result3 = mysql_query("SE LECT `nickname` from `C260987_logins v2`.`members_md 5` WHERE `members_md5`.` username` = '$myusername'") ;
    $row3 = mysql_fetch_arr ay($result3);
    print_r ($row3);
    print "\n<ul>\n";
    while ( $row3 = mysql_fetch_arr ay($result3) ) {
    print "<li>" . $row3["nickname"] . "</li>\n";}
    print "</ul>\n";



    $result4 = mysql_query("SE LECT `lastlogin` from `C260987_logins v2`.`members_md 5` WHERE `members_md5`.` username` = '$myusername'") ;
    $row4 = mysql_fetch_arr ay($result4);
    print_r ($row4);
    print "\n<ul>\n";
    //while ( $row4 = mysql_fetch_arr ay($result4) ) {
    //print "<li>" . $row4["lastlogin"] . "</li>\n";}
    //print "</ul>\n";
    exit();

    I want it returned as a sentence without duplication and without any array or field tags. Can you help?
  • JKing
    Recognized Expert Top Contributor
    • Jun 2007
    • 1206

    #2
    Here try this.

    Code:
    <body>
    <?php
    session_start();
    $connection=mysql_connect("mysql506.opentransfer.c om","C260987_??????","??????");
    $db=mysql_select_db("C260987_loginsv2",$connection );
    $users_table = 'members_md5';
    
    $username = $_POST['myusername'];
    
    $result3 = mysql_query("SELECT `nickname` from `C260987_loginsv2`.`members_md5` WHERE `members_md5`.`username` = '$myusername'");
    while ($row = mysql_fetch_assoc($result3))
    {
    	echo $row['nickname'];
    }
    
    
    $result4 = mysql_query("SELECT `lastlogin` from `C260987_loginsv2`.`members_md5` WHERE `members_md5`.`username` = '$myusername'");
    while ($row = mysql_fetch_assoc($result4))
    {
    	echo $row['lastlogin'];
    }
    exit();

    Comment

    • loginNorm
      New Member
      • Aug 2010
      • 11

      #3
      Thanks but I got no results (blank screen):

      Current code has other stuff commented out and yours included.

      <body>
      <?php
      session_start() ;
      $connection=mys ql_connect("mys ql506.opentrans fer.com","C2609 87_?????","???? ?");
      $db=mysql_selec t_db("C260987_l oginsv2",$conne ction);
      $users_table = 'members_md5';

      $username = $_POST['myusername'];

      $result3 = mysql_query("SE LECT `nickname` from `C260987_logins v2`.`members_md 5` WHERE `members_md5`.` username` = '$myusername'") ;
      $row3 = mysql_fetch_arr ay($result3);
      //print_r ($row3);
      //print "\n<ul>\n";
      //while ( $row3 = mysql_fetch_arr ay($result3) ) {
      //print "<li>" . $row3["nickname"] . "</li>\n";}
      //print "</ul>\n";



      $result4 = mysql_query("SE LECT `lastlogin` from `C260987_logins v2`.`members_md 5` WHERE `members_md5`.` username` = '$myusername'") ;
      $row4 = mysql_fetch_arr ay($result4);
      //print_r ($row4);
      //print "\n<ul>\n";
      //while ( $row4 = mysql_fetch_arr ay($result4) ) {
      //print "<li>" . $row4["lastlogin"] . "</li>\n";}
      //print "</ul>\n";

      while ($row = mysql_fetch_ass oc($result3))
      {
      echo $row['nickname'];
      }

      while ($row = mysql_fetch_ass oc($result4))
      {
      echo $row['lastlogin'];
      }
      exit();

      Comment

      • loginNorm
        New Member
        • Aug 2010
        • 11

        #4
        Allow me to quickly add that echoing the row in previous attempts seemed to include the duplication and the array and fieldname tags. I could be wrong in my recollection.

        Comment

        • loginNorm
          New Member
          • Aug 2010
          • 11

          #5
          I just corrected row tags but with no results on screen: Here is code:
          <body>
          <?php
          session_start() ;
          $connection=mys ql_connect("mys ql506.opentrans fer.com","C2609 87_?????","???? ?");
          $db=mysql_selec t_db("C260987_l oginsv2",$conne ction);
          $users_table = 'members_md5';

          $username = $_POST['myusername'];

          $result3 = mysql_query("SE LECT `nickname` from `C260987_logins v2`.`members_md 5` WHERE `members_md5`.` username` = '$myusername'") ;
          $row3 = mysql_fetch_arr ay($result3);
          //print_r ($row3);
          //print "\n<ul>\n";
          //while ( $row3 = mysql_fetch_arr ay($result3) ) {
          //print "<li>" . $row3["nickname"] . "</li>\n";}
          //print "</ul>\n";



          $result4 = mysql_query("SE LECT `lastlogin` from `C260987_logins v2`.`members_md 5` WHERE `members_md5`.` username` = '$myusername'") ;
          $row4 = mysql_fetch_arr ay($result4);
          //print_r ($row4);
          //print "\n<ul>\n";
          //while ( $row4 = mysql_fetch_arr ay($result4) ) {
          //print "<li>" . $row4["lastlogin"] . "</li>\n";}
          //print "</ul>\n";

          while ($row3 = mysql_fetch_ass oc($result3))
          {
          echo $row3['nickname'];
          }

          while ($row4 = mysql_fetch_ass oc($result4))
          {
          echo $row4['lastlogin'];
          }
          exit();


          ?>

          Comment

          • JKing
            Recognized Expert Top Contributor
            • Jun 2007
            • 1206

            #6
            I just noticed you are setting the variable $username but in your query you reference $myusername. Do you have register_global s turned on?

            Also while testing are you using a username that you know exists in the database? Otherwise you may be producing an empty query result.

            Comment

            • JKing
              Recognized Expert Top Contributor
              • Jun 2007
              • 1206

              #7
              mysql_fetch_arr ay() by default returns both a numeric array and an associative array which is what would be giving you duplicate results.

              mysql_fetch_ass oc() returns only the associative array.

              Also changing the $row variable(tags as you call them) to $row3, and $row4 is doing the same thing just using an extra variable.

              Comment

              • loginNorm
                New Member
                • Aug 2010
                • 11

                #8
                Well, let's see, newbie that I am. myusername came from a login form and appears to resolve fine when I do a query for a FIELD named username: here is that code which links to the last set of code after the asterisks:
                *************** *************** ********
                Maybe the simpler question is whether I can simply extract the **values** rather than array structure and content of two fields because the queries work in other modules I have. That would entirely mitigate the need to adjust any code that is working now. Even commands that adjust the strings that come to me as the users nickname and lastlogin would solve the problem. I am successfully generating this to the screen:

                =============== =============== =============== ===========
                Array ( [0] => Hi Norm, your last login was on : [nickname] => Hi Norm, your last login was on : )

                Array ( [0] => 2010-08-11 [lastlogin] => 2010-08-11 ) =============== =============== =============== ===========
                I just want to eliminate the garbage and make it one sentence.
                =============== =============== =============== ===========
                HTML form for login
                <title>checklog in & reporting</title>
                <style type="text/css">
                <!--
                .style1 {
                font-family: Calibri, "Comic Sans MS";
                font-weight: bold;
                font-style: italic;
                }
                .style2 {
                font-family: Calibri, "Comic Sans MS";
                font-weight: bold;
                font-style: italic;
                font-size: 24px;
                color: #000066;
                }
                -->
                </style>
                <body>
                <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCC C">
                <tr>
                <form name="form1" method="post" action="checklo gin.php">
                <td>
                <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFF F">
                <tr bgcolor="#EAF2F 7">
                <td colspan="3"><p class="style2"> Acunis History Login </p>
                <p align="center" class="style1"> Please enter your username and password </p></td>
                </tr>
                <tr>
                <td width="78" bgcolor="#CCCCC C"><span class="style1"> Username</span></td>
                <td width="6" bgcolor="#EAF2F 7">:</td>
                <td width="180" bgcolor="#EAF2F 7"><input name="myusernam e" type="text" id="myusername" ></td>
                </tr>
                <tr>
                <td bgcolor="#CCCCC C"><span class="style1"> Password</span></td>
                <td bgcolor="#EAF2F 7">:</td>
                <td bgcolor="#EAF2F 7"><input name="mypasswor d" type="password" id="mypassword" ></td>
                </tr>
                <tr>
                <td height="38" bgcolor="#EAF2F 7">&nbsp;</td>
                <td bgcolor="#EAF2F 7">&nbsp;</td>
                <td bgcolor="#EAF2F 7"><input type="submit" name="Submit" value="Login"></td>
                </tr>
                </table>
                </td>
                </form>
                </tr>
                </table>
                *************** *************** *************** ****
                //following module is called checklogin and is the action from the form.

                <body>
                <?php
                session_start() ;
                $host="mysql506 .opentransfer.c om"; // Host name
                $username="C260 987_?????"; // Mysql username
                $password="???? ?"; // Mysql password
                $db_name="C2609 87_loginsv2"; // Database name
                $tbl_name="memb ers_md5"; // Table name

                // Connect to server and select databse.
                mysql_connect(" $host", "$username" , "$password" )or die("cannot connect");
                mysql_select_db ("$db_name") or die("cannot select DB");

                // username and password sent from form
                $myusername=$_P OST['myusername'];
                $mypassword=$_P OST['mypassword'];

                // To protect MySQL injection (more detail about MySQL injection)
                $myusername = stripslashes($m yusername);
                $mypassword = stripslashes($m ypassword);
                $myusername = mysql_real_esca pe_string($myus ername);
                $mypassword = mysql_real_esca pe_string($mypa ssword);

                $sql="SELECT * FROM $tbl_name WHERE username='$myus ername' and password=MD5('$ mypassword')";
                $result=mysql_q uery($sql);

                // Mysql_num_row is counting table row
                $count=mysql_nu m_rows($result) ;

                // write user record to count logins
                $File = "loginstats1.tx t";
                $Handle = fopen($File,'a+ ');
                fwrite($Handle, "$myusername,\n ");
                fclose($Handle) ;

                // If result matched $myusername and $mypassword, table row must be 1 row
                // Register $myusername, $mypassword and redirect to file "login_success. php"
                if($count==1){

                $_SESSION["myusername "] = $myusername;
                $_SESSION["mypassword "] = $mypassword;


                header("locatio n:login_success .php");
                }
                else {
                echo "Wrong Username or Password";
                }
                ?>

                </body>
                </html>

                </body>
                </html>

                Comment

                • JKing
                  Recognized Expert Top Contributor
                  • Jun 2007
                  • 1206

                  #9
                  Try this... I commented out a few more of your lines because I think they are adversely affecting the code which I provided for you.
                  Code:
                  <body>
                  <?php
                  session_start();
                  $connection=mysql_connect("mysql506.opentransfer.c om","C260987_?????","?????");
                  $db=mysql_select_db("C260987_loginsv2",$connection );
                  $users_table = 'members_md5';
                  
                  $username = $_POST['myusername'];
                  
                  $result3 = mysql_query("SELECT `nickname` from `C260987_loginsv2`.`members_md5` WHERE `members_md5`.`username` = '$myusername'");
                  //$row3 = mysql_fetch_array($result3);
                  //print_r ($row3);
                  //print "\n<ul>\n";
                  //while ( $row3 = mysql_fetch_array($result3) ) {
                  //print "<li>" . $row3["nickname"] . "</li>\n";}
                  //print "</ul>\n";
                  
                  
                  
                  $result4 = mysql_query("SELECT `lastlogin` from `C260987_loginsv2`.`members_md5` WHERE `members_md5`.`username` = '$myusername'");
                  //$row4 = mysql_fetch_array($result4);
                  //print_r ($row4);
                  //print "\n<ul>\n";
                  //while ( $row4 = mysql_fetch_array($result4) ) {
                  //print "<li>" . $row4["lastlogin"] . "</li>\n";}
                  //print "</ul>\n";
                  
                  while ($row3 = mysql_fetch_assoc($result3))
                  {
                  echo $row3['nickname'];
                  }
                  
                  while ($row4 = mysql_fetch_assoc($result4))
                  {
                  echo $row4['lastlogin'];
                  }
                  exit();
                  ?>
                  Last edited by JKing; Aug 12 '10, 04:40 PM. Reason: typo

                  Comment

                  • loginNorm
                    New Member
                    • Aug 2010
                    • 11

                    #10
                    Believe it or not, just a blank screen as opposed to code that is sending the array format back.

                    Comment

                    • JKing
                      Recognized Expert Top Contributor
                      • Jun 2007
                      • 1206

                      #11
                      Could you post the current code you have.

                      Comment

                      • loginNorm
                        New Member
                        • Aug 2010
                        • 11

                        #12
                        Here you go, and thanks!

                        <body>
                        <?php
                        session_start() ;
                        $connection=mys ql_connect("mys ql506.opentrans fer.com","C2609 87_??????","??? ????????");
                        $db=mysql_selec t_db("C260987_l oginsv2",$conne ction );
                        $users_table = 'members_md5';

                        $username = $_POST['myusername'];

                        $result3 = mysql_query("SE LECT `nickname` from `C260987_logins v2`.`members_md 5` WHERE `members_md5`.` username` = '$myusername'") ;
                        //$row3 = mysql_fetch_arr ay($result3);
                        //print_r ($row3);
                        //print "\n<ul>\n";
                        //while ( $row3 = mysql_fetch_arr ay($result3) ) {
                        //print "<li>" . $row3["nickname"] . "</li>\n";}
                        //print "</ul>\n";

                        $result4 = mysql_query("SE LECT `lastlogin` from `C260987_logins v2`.`members_md 5` WHERE `members_md5`.` username` = '$myusername'") ;
                        //$row4 = mysql_fetch_arr ay($result4);
                        //print_r ($row4);
                        //print "\n<ul>\n";
                        //while ( $row4 = mysql_fetch_arr ay($result4) ) {
                        //print "<li>" . $row4["lastlogin"] . "</li>\n";}
                        //print "</ul>\n";

                        while ($row3 = mysql_fetch_ass oc($result3))
                        {
                        echo $row3['nickname'];
                        }

                        while ($row4 = mysql_fetch_ass oc($result4))
                        {
                        echo $row4['lastlogin'];
                        }
                        exit();
                        ?>

                        Comment

                        • JKing
                          Recognized Expert Top Contributor
                          • Jun 2007
                          • 1206

                          #13
                          Okay give this a go and tell me what output you get if any.

                          Code:
                          <body>
                          <?php
                          session_start();
                          $connection=mysql_connect("mysql506.opentransfer.c om","C260987_??????","???????????");
                          $db=mysql_select_db("C260987_loginsv2",$connection );
                          $users_table = 'members_md5';
                          
                          $username = $_POST['myusername'];
                          
                          $result3 = mysql_query("SELECT `nickname` from `C260987_loginsv2`.`members_md5` WHERE `members_md5`.`username` = '$myusername'");
                          //$row3 = mysql_fetch_array($result3);
                          //print_r ($row3);
                          //print "\n<ul>\n";
                          //while ( $row3 = mysql_fetch_array($result3) ) {
                          //print "<li>" . $row3["nickname"] . "</li>\n";}
                          //print "</ul>\n";
                          
                          $result4 = mysql_query("SELECT `lastlogin` from `C260987_loginsv2`.`members_md5` WHERE `members_md5`.`username` = '$myusername'");
                          //$row4 = mysql_fetch_array($result4);
                          //print_r ($row4);
                          //print "\n<ul>\n";
                          //while ( $row4 = mysql_fetch_array($result4) ) {
                          //print "<li>" . $row4["lastlogin"] . "</li>\n";}
                          //print "</ul>\n";
                          
                          $num_rows = mysql_num_rows($result3);
                          echo "Result 3 rows: " .$num_rows . "<br />";
                          while ($row3 = mysql_fetch_assoc($result3))
                          {
                          echo $row3['nickname'];
                          }
                          
                          $num_rows = mysql_num_rows($result4);
                          echo "Result 4 rows: " . $num_rows;
                          while ($row4 = mysql_fetch_assoc($result4))
                          {
                          echo $row4['lastlogin'];
                          }
                          exit();
                          ?>

                          Comment

                          • loginNorm
                            New Member
                            • Aug 2010
                            • 11

                            #14
                            I suspect you'll think I got this already:

                            Result 3 rows: 0
                            Result 4 rows: 0

                            I think that's pretty interesting since I was obviously getting a match of some sort with the other technique.

                            Norm

                            Comment

                            • loginNorm
                              New Member
                              • Aug 2010
                              • 11

                              #15
                              Check this out, with your code and logging in with another ID I did get the following returned:
                              Result 3 rows: 1
                              Hello My Queen, your last logout was on Result 4 rows: 12010-08-11

                              It makes sense since I was doing the code standalone without a login!. So now, I'll try one of your other revisions of code and let you know!

                              Comment

                              Working...