Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to show a value from a database table through PHP echo. The MySQL result is a double (10, 2).

<?php $link = new mysqli('127.0.0.1', '*******', '*******', '*******');
                     if ($link->connect_errno) {
                        die('Failed to connect to MySQL: (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
                     }
                    $user = $_SESSION['user'];
                    $result = $link->query("SELECT * FROM users WHERE username='$user' AND active=1");
                    $numrows = $result->num_rows;
                    if($numrows == 0 || $numrows > 1)
                    {
                        $link->close();
                        session_destroy();
                        echo '<META HTTP-EQUIV="Refresh" Content="0; URL=**************">';
                        exit;   
                    }
                    else if($numrows == 1)
                    {
                        //$sid = $result(8);
                        echo '<strong>this is my string in which i want to show the result in' . $result(8) . 'rest of the string';}?>

Line where the error is show is the echo line (in the end). Can anyone point me out to what I am doing wrong here? Thank you.

share|improve this question
 
$result is a link to the query (resource), you have to fetch the results first. –  Waygood May 1 at 17:58

4 Answers

up vote 1 down vote accepted

The $result variable is a MySQLi Result. You want to get a row from that result set. To do that, use fetch_assoc. This will give you an associative array with all of the fields of the table as keys.

$row = $result->fetch_assoc();
echo $row['username'];
echo $row['whatever'];

EDIT: It may be valuable to note that you are susceptible to the following security risks: SQL injection, cross-site scripting, and Cookie tampering.

share|improve this answer
 
Since OP is using (8) they may want to fetch_row(); –  Waygood May 1 at 18:05
 
Thanks, this worked great! –  Krešimir Čulina May 1 at 18:09

you are calling $result(8) which is a method call in php. I think you meant

$dataRow = $result->fetch_array(MYSQLI_ASSOC);
// collect whatever you need from the array $dataRow array

since PHP is an interpreted language you can do such things as assign a value to a variable and call that variable

$func = 'myFunc';
$func(); // will call the function myFunc
share|improve this answer
 
$result is a resource not an array –  Waygood May 1 at 17:59
 
Fatal error: Cannot use object of type mysqli_result as array –  Krešimir Čulina May 1 at 18:01
1  
php.net/manual/en/mysqli.query.php Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE. –  Waygood May 1 at 18:01
 
changed my answer based on @Waygood comment –  MatthiasLaug May 1 at 18:04

You are trying to access to an array value, you must use:

$result[8] and not $result(8)

Best regards!

share|improve this answer

Look at this - $result(8) (last row). A variable can't have arguments. You probably wanted $result[8] (9th element in array).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.