www.webdeveloper.com
Results 1 to 3 of 3

Thread: member log in script works in localhost but reads "Resource id #4" on the live site

  1. #1
    Join Date
    Jul 2012
    Posts
    14

    member log in script works in localhost but reads "Resource id #4" on the live site

    I used this great log in PHP tutorial script from sourcecodester.com
    http://www.sourcecodester.com/tutori...-phpmysql.html

    It works perfectly on my local host but when I moved to the live server it just echoes "Resource id #4" instead of logging in and directing to menu.php

    PHP Code:
    <?php

         
    //Start session
         
    session_start();

         require_once
    'db_connect.php';

         
    //db connection details
         
    $mysql_hostname $db_host;
         
    $mysql_user     $db_username;
         
    $mysql_password $db_pass;
         
    $mysql_database $db_name;
         
    $prefix "";
         
    $bd mysql_connect($mysql_hostname$mysql_user$mysql_password) or die("Could not connect to the database");
         
    mysql_select_db($mysql_database$bd) or die("Could not select the database");
     
         
    //Array to store validation errors
         
    $errmsg_arr = array();
     
         
    //Validation error flag
         
    $errflag false;
     
         
    //Function to sanitize values received from the form. Prevents SQL injection
         
    function clean($str) {
              
    $str = @trim($str);
              if(
    get_magic_quotes_gpc()) {
                   
    $str stripslashes($str);
              }
              return 
    mysql_real_escape_string($str);
         }
     
         
    //Sanitize the POST values
         
    $signin_email clean($_POST['signin_email']);
         
    $signin_pass clean($_POST['signin_pass']);
     
         
    //Input Validations
         
    if($signin_email == '') {
              
    $errmsg_arr[] = 'Username missing';
              
    $errflag true;
         }
         if(
    $signin_pass == '') {
              
    $errmsg_arr[] = 'Password missing';
              
    $errflag true;
         }
     
         
    //If there are input validations, redirect back to the login form
         
    if($errflag) {
              
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
              
    session_write_close();
              
    header("location: index.php");
              exit();
         }
     
         
    //Create query
         
    $qry="SELECT * FROM members_tbl WHERE email='$signin_email' AND pass='$signin_pass'";
         
    $result=mysql_query($qry);
      
         echo 
    $result;
     
         
    //Check whether the query was successful or not
         
    if($result) {
              if(
    mysql_num_rows($result) > 0) {
                   
    //Login Successful
                   
    session_regenerate_id();
                   
    $member mysql_fetch_assoc($result);
                   
    $_SESSION['SESS_MEMBER_ID'] = $member['user_id'];
                   
    $_SESSION['SESS_FIRST_NAME'] = $member['username'];
                   
    session_write_close();
                   
    header("location: menu.php");
                   exit();
              }else {
                   
    //Login failed
                   
    $errmsg_arr[] = 'user name and password not found';
                   
    $errflag true;
                   if(
    $errflag) {
                        
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
                        
    session_write_close();
                        
    header("location: index.php");
                        exit();
                   }
              }
         }else {
              die(
    "Query failed");
         }
    ?>
    ?>
    I have googled the issue and found the comment below but I'm not sure how I can relate it to my code.

    "You are trying to print a mysql resource variable instead of the values contained within the resource it references. You must first try to extract the values you have gotten by using a function such as mysql_fetch_assoc()."

    Any help would be much appreciated. Thanks.
    Last edited by conor909; 01-11-2014 at 08:38 AM.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,536
    Well, it certainly makes not sense to do...
    PHP Code:
        echo $result
    ...since $result will either be Boolean false or a MySQL query result resource (which is what you are seeing). I have no idea why you are seeing different outputs between the environments, but that line probably does not belong in either case.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jul 2012
    Posts
    14
    Thanks for the reply NogDog.

    I'm very new to PHP but I managed to solve this issue by placing
    Code:
    ob_start();
    at the top of my file, under
    Code:
    session_start();
    I found this "quick fix" on a forum thread, but I don't understand why it fixes the issue. Below is what the PHP documentation had to say on ob_start();

    This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.

    The contents of this internal buffer may be copied into a string variable using ob_get_contents(). To output what is stored in the internal buffer, use ob_end_flush(). Alternatively, ob_end_clean() will silently discard the buffer contents.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles