SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Strange SQL error

    I just ran my Account Activation scripts in PHP, and I haven't changed anything that I can think of since last night, but now got a *weird* SQL error...

    An error occurred in script '/home/content/76/5060376/html/106_ActivateAccount.php' on line 72: Object of class mysqli_result could not be converted to string
    Date/Time: 10-11-2009 15:15:41


    Array
    (
    [GLOBALS] => Array
    *RECURSION*
    [_POST] => Array
    (
    )

    [_GET] => Array
    (
    [x] => test@mail.com
    [y] => f85e70af6307c75284af7b68453856dc
    )

    [_COOKIE] => Array
    (
    [PHPSESSID] => 9ghisl9pfjhks21cvfefo1sjm3
    )

    [_FILES] => Array
    (
    )

    [_SESSION] => Array
    (
    [email] => test@mail.com
    [first_name] => Amy
    [account_results] =>

    Here is my PHP code...

    Code:
    <?php # Script 16.7 - activate.php
      // This page activates the user's account.
    
      // Address error-handling.
      error_reporting(E_ALL & ~E_NOTICE);
    
      // Include Configuration Settings.
      require_once ('includes/config.inc.php');
    
      // Start output buffering.
      ob_start();
    
      // Initialize a session.
      session_start();
    
      // Validate $_GET['x'] and $_GET['y']:
      // Assume value are invalid.
      $x = FALSE;
      $y = FALSE;
    
      // Validate E-mail address.
      if (isset($_GET['x'])
            && preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $_GET['x']) ) {
              $x = $_GET['x'];
      }
    
      // Validate Authorization Code.
      if (isset($_GET['y']) && (strlen($_GET['y']) == 32 ) ) {
              $y = $_GET['y'];
      }
    
      // Validate both URL values.
      if ($x && $y) {
          // Authorization values valid.
    
          // Path to DB Connection Script.
          require_once (MYSQL);
    
          //************************************************************************
          // Determine User's Name based on Email in URL.
          // Build query.
          $name = "SELECT first_name " .
                  "FROM users " .
                  "WHERE (email = '" . mysqli_real_escape_string($dbc, $x) .
                  "') LIMIT 1";
    
          // Run query.
          $r1 = mysqli_query ($dbc, $name)
                  or trigger_error("Query: $name\n<br />MySQL Error: " . mysqli_error($dbc));
    
          //************************************************************************
          // Remove Authorization String from database.
          // Build query.
          $auth = "UPDATE users " .
                  "SET active = NULL " .
                  "WHERE (email = '" . mysqli_real_escape_string($dbc, $x) .
                  "' AND active = '" . mysqli_real_escape_string($dbc, $y) .
                  "') LIMIT 1";
    
          // Run query.
          $r2 = mysqli_query ($dbc, $auth)
                  or trigger_error("Query: $auth\n<br />MySQL Error: " . mysqli_error($dbc));
    
          //************************************************************************
          // Verify record was changed.
          if (mysqli_affected_rows($dbc) == 1) {
              // Insert succeeded.
              //echo "<h3>Your account is now active. You may now log in.</h3>";
    
              // Create "Account Validated" message.
              $_SESSION['validation_results'] =
                "<span id=\"congrats\">Congratulations, " . $r1 .
                ", your account is now active!!</span>" .
                "<span id=\"congrats_msg\">You may log in.</span>";
    
          } else {
              // Insert failed.
              //echo '<p class="error">Your account could not be activated. Please re-check the link or contact the system administrator.</p>';
    
              // Create Error message.
              $_SESSION['validation_results'] =
                "<span id=\"congrats\">Sorry, " . $r1 .
                ", your account could not be actived!!</span>" .
                "<span id=\"congrats_msg\">Please re-check the link or contact the system administrator.</span>";
          }
    
          // Close Database Connection.
          mysqli_close($dbc);
    
      } else {
          // Authorization values invalid.
    
          // Redirect.
          $url = BASE_URL . 'index.php'; // Define the URL:
          ob_end_clean(); // Delete the buffer.
          header("Location: $url");
          exit(); // Quit the script.
    
      }
    ?>
    I don't see any code anywhere near Line 72 that looks wrong?!

    UGH!!

    Any idea what is wrong??


    Amy

  2. #2
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I think I found the error...

    Now I need help fixing it!!!

    How do I assign the results of a query to a variable??

    My SELECT query finds the person of the name activating their account, and I want to assign that name to $name so I can print a customized message.

    Here is my code:
    Code:
          // Determine User's Name based on Email in URL.
          // Build query.
          $name = "SELECT first_name " .
                  "FROM users " .
                  "WHERE (email = '" . mysqli_real_escape_string($dbc, $x) .
                  "') LIMIT 1";
    
          // Run query.
          $r1 = mysqli_query ($dbc, $name)
                  or trigger_error("Query: $name\n<br />MySQL Error: " . mysqli_error($dbc));
    which apparently is wrong!


    Amy

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,340
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    wish i could help, but there's nothing here that looks like a mysql error
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    wish i could help, but there's nothing here that looks like a mysql error
    Sure there is?!

    See my follow-up post.

    I was trying to print out $r1 which is a pointer to my query results (i.e. an Object).

    And in my follow-up post, I was asking if there is a simple way to assign the results set of a simple SELECT query to a single variable?

    For instance, if...

    SELECT name
    FROM users
    WHERE email='somebody@mail.com';

    returned a single value of "Amy" then what is the easiest way to assign "Amy" to a variable like $name ???

    I know there is...

    mysqli_fetch_array ($r, MYSQLI_ASSOC);

    but I don't have an array, per se, just a single value.

    Follow me?


    Amy

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,340
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by amy.damnit View Post
    Follow me?
    not really, sorry

    you're speaking php and this is the mysql forum

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    not really, sorry

    you're speaking php and this is the mysql forum

    You know MySQL, but don't know PHP?!


    Amy

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by amy.damnit View Post
    what is the easiest way to assign "Amy" to a variable like $name ???

    I know there is...

    mysqli_fetch_array ($r, MYSQLI_ASSOC);

    but I don't have an array, per se, just a single value.
    A successful select result conceptually returns 1 or more rows. Even if it's only 1 row, and that row only has the value of a single column, that's the format.

    I tend to usually just fetch things as an associative array because that's the most common format I want, and it's consistent.
    PHP Code:
    $row mysqli_fetch_assoc($r);
    echo 
    $row['name']; 
    But if you like syntax,
    PHP Code:
    list($name) = mysqli_fetch_row($r);
    // or 
    extract(mysqli_fetch_assoc($r)); 

  8. #8
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    A successful select result conceptually returns 1 or more rows. Even if it's only 1 row, and that row only has the value of a single column, that's the format.

    I tend to usually just fetch things as an associative array because that's the most common format I want, and it's consistent.
    PHP Code:
    $row mysqli_fetch_assoc($r);
    echo 
    $row['name']; 
    But if you like syntax,
    PHP Code:
    list($name) = mysqli_fetch_row($r);
    // or 
    extract(mysqli_fetch_assoc($r)); 
    Yah, here is what I ended up doing...

    Code:
          // Determine User's Name based on Email in URL.
          // Build query.
          $qry01 = "SELECT first_name " .
                  "FROM users " .
                  "WHERE (email = '" . mysqli_real_escape_string($dbc, $x) .
                  "') LIMIT 1";
    
          // Run query.
          $result01 = mysqli_query ($dbc, $qry01)
                          or trigger_error("Query: $qry01\n<br />MySQL Error: " . mysqli_error($dbc));
    
          // Assign query results to variable.
          list($name) = mysqli_fetch_row($result01);
    

    Can you highlight the difference between your code...

    $row = mysqli_fetch_assoc($r);
    echo $row['name'];

    and this...

    mysqli_fetch_array ($r, MYSQLI_ASSOC);


    Thanks,



    Amy

  9. #9
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,340
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by amy.damnit View Post
    You know MySQL, but don't know PHP?!
    this is correct

    i've used coldfusion for many years, and see no need to learn an inferior language lol
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  10. #10
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysqli_fetch_assoc($r);
    is equivalent to
    mysqli_fetch_array ($r, MYSQLI_ASSOC);

  11. #11
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    this is correct

    i've used coldfusion for many years, and see no need to learn an inferior language lol
    Trying to expand your circle of friends, eh??

    (I'll be sure not to stand next to you near the PHP forum!)


    Amy

  12. #12
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    mysqli_fetch_assoc($r);
    is equivalent to
    mysqli_fetch_array ($r, MYSQLI_ASSOC);
    So if my query returned 2 rows, what would happen to your code??

    Code:
    $row = mysqli_fetch_assoc($r);
    echo $row['name'];


    Amy

  13. #13
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The first row would be fetched. The result set's hidden internal row pointer would be advanced to the next row.

  14. #14
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    The first row would be fetched. The result set's hidden internal row pointer would be advanced to the next row.
    So if you had a query that returned only ONE Column, but MANY records, then you could use this code/function, but you would just have to use a loop or something, right?

    And, for cases like mine, then you can use your coding approach or the way I did it with List ( ) , right?


    Amy

  15. #15
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $name mysql_result($r,0); 
    Or more simply

    PHP Code:
    $name mysql_result(mysql_query("SELECT name..."),0); 


Bookmarks

Posting Permissions

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