FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Error with outputing with echo.





Mememe
Code:

$selectRank = "SELECT rank FROM Members WHERE loginName='{$_SESSION['user']}'";
$_SESSION['rank'] = mysqli_query($con,$selectRank);
  echo 'RANK: '.$_SESSION['rank'].'<br />';


This is a code just outputing something from a Database. But I get this error

Quote:

Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\Program Files\EasyPHP 2.0b1\www\MEMEME2\index.php on line 44


Any ideas why?
AftershockVibe
This is because the $_SESSION['rank'] is the whole result set for the query you entered. So, echo is expecting a number or string to print and you're giving it a result set. Most importantly it doesn't know what to print anyway because your result set might have one row (like it does here) or 50,000 (if you removed the WHERE... part)!

What you need to do something like this:
Code:
$selectRank = "SELECT rank FROM Members WHERE loginName='{$_SESSION['user']}'";
/* The query you want to execute */

$result= mysqli_query($con,$selectRank);
/* Run the query and get the result set */

if(mysqli_num_rows($result) == 0)
{
die "Error: The specified user does not exist";
}

if(mysqli_num_rows($result) != 1)
{
die "Error: The specified user is has multiple records associated with it. Login names should be unique";
}


while($currentRow = mysqli_fetch_assoc($result))
{
$_SESSION['rank'] = $currentRow['rank'];
echo 'RANK: ' . $_SESSION['rank'] . '<br />';
}


Hope this helps
Cool
Mememe
Hmmm..testing

[EDIT]
Well the point for this is when the user logs in, if the password and user are right, then it finds what rank the user is, for example Admin, Member. Then it goes back to index.php On the side is the User's control panel, and it outputs the rank of the user. When I tested the code, the rank doesn't show, meaning$_SESSION['rank'] contains nothing. But I'm sure in the database, it has the rank...
JayBee
mysqli_query returns some object which is not a string. If you want to know if it works, just use print_r or vardump insted of echo.

If you want to know only the rank of one user, you can use this code
Code:
<?php
// prepare statement
$stmt = mysqli_prepare($con, "SELECT rank FROM Members WHERE loginName=? LIMIT 1");

// bind parameter loginName as string "s"
mysqli_stmt_bind_param($stmt,"s", $_SESSION['user']);

// execute statement
mysqli_stmt_execute($stmt);

// bind result variable
mysqli_stmt_bind_result($stmt, $_SESSION['rank']);

// fetch result
mysqli_stmt_fetch($stmt);

// close statement
mysqli_stmt_close($stmt);

echo 'RANK: '.$_SESSION['rank'].'<br />';
?>
Related topics
[PHP] exit;
php
Sending Email from localhost
[RESOLVED] RETARDED ERROR!
[PHP] Error link
Weird "Failure to Open Stream" Error
Php parsing error! Need help
php error ?[parse error]
Link Add Error
Need help with an error
whats the error?
PHP - Error
MySQL error
Forum Avatar error
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.