0

I have this tiny table in my database:

----------------------
id | name      | value
----------------------
1  | test.flag | 0
----------------------
2  | username  | franz
----------------------

I simply try to read the value of test.flag and store the result into a variable.

<?php
    
    $servername = "127.0.0.1";
    $username   = "test";
    $password   = "test123";
    $dbname     = "testdb";

    $conn = mysqli_connect($servername, $username, $password, $dbname);
    
    if (!$conn) { 
        die("Connection to database failed with error#: " . mysqli_connect_error()); 
    }   
    
    $sql = "SELECT value FROM mytable WHERE name='test.flag';";
    $result = mysqli_query($conn, $sql);

    echo "<p>".$result."</p><br>";

    if (mysqli_query($conn, $sql)) {
        echo "<p>Sucessfully</p><br>";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
    
    echo "<p>".$sql."</p><br>";
?>

But all it does is loading a blank page after I execute this script.

2 Answers 2

5

You need to fetch the row and then echo it.

$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
echo "Result: " . $row['value'];
Sign up to request clarification or add additional context in comments.

Comments

2

The value of the query is not stored in the $sql variable.

Like @user5173426 answered, you have to fetch it first.

Example (http://www.w3schools.com/php/php_mysql_select.asp):

while($row = $result->fetch_assoc())
   $toEcho = $row["value"];
echo $toEcho;

1 Comment

Ahh, I love it when I see my name being used as a reference. ;-)

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.