PHP:显示MySQL中包含

时间:2016-11-19 10:48:03

标签: php mysql

我正在尝试使用Mysql数据库在PHP中编写代码,问题是我想显示具有相同列值的所有行。例如:

id  |   Name   | age | Location | type
----+----------+-----+----------+------
 1  | Ane      | 22  |  SG      |  1
 2  | Angi     | 19  |  IND     |  2
 3  | Bobby    | 23  |  PH      |  1
 4  | Denis    | 26  |  IND     |  1
 5  | Jerry    | 21  |  SG      |  1
 6  | Mikha    | 25  |  JP      |  2

我只想显示列类型= 1的值或列位置中的值,并在html视图中显示为表。

我想要的结果是这样的:

id  |   Name   | age | Location | type
---+----------+-----+----------+------
 1  | Ane      | 22  |  SG      |  1
 3  | Bobby    | 23  |  PH      |  1
 4  | Denis    | 26  |  IND     |  1
 5  | Jerry    | 21  |  SG      |  1

这是我的代码:

<?php
$con = mysqli_connect("localhost","root","","testuser");
$query = mysqli_query("SELECT * FROM `usersdata` WHERE `type`='1'");
$result = mysqli_query($con,$query);

echo "<table class='tmaintable' border='0' cellpadding='3' width='99%' align='center' cellspacing='1'>
<tr class='theader'>
<td>ID</td>
<td>Name</td>
<td>Age</td>
<td>Location</td>
<td>Type</td>
</tr>";

while($row = mysqli_fetch_array($result)){              
echo "<tr class='todd'>";
echo "<td style='text-align:center;' >" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>

但是我遇到了这样的错误:

  

警告:mysqli_query()需要至少2个参数,1在第94行的www.myweb.com \ users_list \ type.php中给出&lt;&lt;这一点“$查询”行
  警告:mysqli_query():第95行的www.myweb.com \ users_list \ type.php中的空查询&lt;&lt;这一点“$ result”行
  警告:mysqli_fetch_array()要求参数1为mysqli_result,在第109行的www.myweb.com \ users_list \ type.php中给出布尔值&lt;&lt;这一点“while($ row =”line

我正在努力理解但仍然没有得到它,任何人都可以帮助我吗?!感谢。

3 个答案:

答案 0 :(得分:0)

更改
$query = mysqli_query("SELECT * FROM usersdata WHERE type='1'");

$query = "SELECT * FROM usersdata WHERE type='1'";

修改
仅供参考:
mysqli_query有两个参数:连接和查询。我假设您只想在此行中创建查询字符串,此错误会引起此问题,因为它使用了一行作为查询字符串。

答案 1 :(得分:0)

问题在于引人注目的问题。     $ query = mysqli_query(&#34; SELECT * FROM usersdata WHERE type =&#39; 1&#39;&#34;);     $ result = mysqli_query($ con,$ query);

您有2种方法可以解决此问题。

1. $query = "SELECT * FROM `usersdata` WHERE `type`='1'";
$result = mysqli_query($con,$query);

2. $query = mysqli_query($con,"SELECT * FROM `usersdata` WHERE `type`='1'");

答案 2 :(得分:0)

试试这个。它应该工作正常

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "test";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

    $sql = "SELECT * FROM `usersdata` WHERE `type`='1'";
    $result = mysqli_query($conn, $sql);

    echo "<table class='tmaintable' border='0' cellpadding='3' width='99%' align='center' cellspacing='1'>
    <tr class='theader'>
    <td>ID</td>
    <td>Name</td>
    <td>Age</td>
    <td>Location</td>
    <td>Type</td>
    </tr>";

    while($row = mysqli_fetch_array($result)){
        echo "<tr class='todd'>";
        echo "<td style='text-align:center;' >" . $row['id'] . "</td>";
        echo "<td>" . $row['name'] . "</td>";
        echo "<td>" . $row['age'] . "</td>";
        echo "<td>" . $row['location'] . "</td>";
        echo "<td>" . $row['type'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
    ?>