Martik Martik - 1 month ago 14
PHP Question

Read value from database, echo

Very basic but as a rookie I am struggling. The echo doesnt show any value, just the text. What am I doing wrong?

Connect.php:

<?php
$connection = mysqli_connect('test.com.mysql', 'test_com_systems', 'systems');
if (!$connection){
die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, 'swaut_com_systems');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($connection));
}
?>


Get.php:

<?php
require('connect.php');
$query2 = "SELECT systemid FROM user WHERE username=test";
$result2 = mysqli_query($connection, $query2);

echo ( 'SystemID: '.$result2);

?>

Answer

Assuming you have connected to the database successfully then the query is incorrect. You must wrap all text values in quotes like this

<?php
    require('connect.php');
    $query2 = "SELECT systemid FROM user WHERE username='test'";
    $result2 = mysqli_query($connection, $query2);

Now the mysqli_query submits the query to the database where it is run and a result set built. To see the result set you need to read the result set back from the database using one of the fetch functions for example

    $row = mysqli_fetch_assoc($result2);

    echo 'SystemID: ' . $row['systemid'];

If there are more than one rows in the result set you must do that in a loop like this

    while ($row = mysqli_fetch_assoc($result2)){
        echo 'SystemID: ' . $row['systemid'];
    }