0

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.

1
  • $result is a link to the query (resource), you have to fetch the results first. Commented May 1, 2013 at 17:58

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

$result is a resource not an array
Fatal error: Cannot use object of type mysqli_result as array
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.
2

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.

1 Comment

Since OP is using (8) they may want to fetch_row();
0

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

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

Best regards!

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.