SitePoint Sponsor |
|
User Tag List
Results 1 to 15 of 15
Thread: Strange SQL error
-
Oct 11, 2009, 16:37 #1
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. } ?>
UGH!!
Any idea what is wrong??
Amy
-
Oct 11, 2009, 16:50 #2
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));
Amy
-
Oct 11, 2009, 17:33 #3
- Join Date
- Jul 2002
- Location
- Toronto, Canada
- Posts
- 39,347
- Mentioned
- 63 Post(s)
- Tagged
- 3 Thread(s)
wish i could help, but there's nothing here that looks like a mysql error
-
Oct 11, 2009, 17:41 #4
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
-
Oct 11, 2009, 17:57 #5
- Join Date
- Jul 2002
- Location
- Toronto, Canada
- Posts
- 39,347
- Mentioned
- 63 Post(s)
- Tagged
- 3 Thread(s)
-
Oct 11, 2009, 18:05 #6
-
Oct 11, 2009, 18:20 #7
- Join Date
- Jul 2008
- Posts
- 5,757
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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'];
PHP Code:list($name) = mysqli_fetch_row($r);
// or
extract(mysqli_fetch_assoc($r));
-
Oct 11, 2009, 18:46 #8
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
-
Oct 11, 2009, 19:04 #9
- Join Date
- Jul 2002
- Location
- Toronto, Canada
- Posts
- 39,347
- Mentioned
- 63 Post(s)
- Tagged
- 3 Thread(s)
-
Oct 11, 2009, 19:17 #10
- 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);
-
Oct 11, 2009, 19:25 #11
-
Oct 11, 2009, 19:26 #12
-
Oct 11, 2009, 19:42 #13
- 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.
-
Oct 11, 2009, 20:13 #14
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
-
Oct 11, 2009, 21:41 #15
- 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);
PHP Code:$name = mysql_result(mysql_query("SELECT name..."),0);
Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
Bookmarks