从数据库中读取值,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);

    ?>

非常基本但作为一个新手,我正在努力。 回声不显示任何值,只显示文本。 我做错了什么? p>

Connect.php: p>

 &lt;?php 
 $ connection = mysqli_connect('test.com  .mysql','test_com_systems','systems'); 
if(!$ connection){
 die(“数据库连接失败”.mysqli_error($ connection)); 
} 
 $ select_db = mysqli_select_db($ connection)  ,'swaut_com_systems'); 
if(!$ select_db){
 die(“数据库选择失败”.mysqli_error($ connection)); 
} 
?&gt; 
  code>  pre> \  n 
 

Get.php: p>

 &lt;?php 
 require('connect.php'); 
 $ query2 =“SELECT systemid FROM user WHERE  username = test“; 
 $ result2 = mysqli_query($ connection,$ query2); 
 
 echo('SystemID:'。$ result2); 
 
?&gt; 
  code>  pre  > 
  div>

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'];
    }

You are printing the mysqli result object. In order to printthe result you have to use:

$row = mysqli_fetch_assoc($result2);
print_r($row);

You need to collect the results of the mysqli_query using the following:

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

while ($row = mysqli_fetch_assoc($result2)) 
{
   echo "System ID is: " .  $row['systemid'];
}