简体   繁体   English

如何使用php从mysql数据库中获取所有匹配的记录?

[英]how to get all matching records from mysql database using php?

I have the following table set in my mysql database 我在mysql数据库中设置了下表

mem_id | pid | content
     0 |   1 | All the content is here
     0 |   2 | All the content is here
     0 |   3 | All the content is here

Now the problem is to get all matching mem_id values and store it in a array in php. 现在的问题是获取所有匹配的mem_id值并将其存储在php中的数组中。

Example: A variable called $id has value 0 示例:名为$id的变量的值为0

So now I have to get all values under the column content but only those which matches the mem_id of the user. 因此,现在我必须获取列内容下的所有值,但只有与用户的mem_id匹配的mem_id

Could anyone help me with this, I need it in php and using mysql query to get all the values. 谁能帮我这个忙,我需要在php中使用mysql查询来获取所有值。

My current code: 我当前的代码:

$con=mysqli_connect("localhost","*****","******","*****");

// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM user_friends WHERE mem_id = '$_SESSION[SESS_MEMBER_ID]' LIMIT 1");

while($row = mysqli_fetch_array($result)) 
{ 
    $friends = $row['fid'];
} 
SELECT * FROM yourTABLE WHERE mem_id=0   // or 1 or 2 or 3 etc

Using PHP you could query it like: 使用PHP,您可以像这样查询它:

<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$id = intval($id);  // Put your ID here
$query = "SELECT * FROM yourTABLE WHERE mem_id=$id";
if ($result = mysqli_query($link, $query)) {
    while ($row = mysqli_fetch_assoc($result)) {
     print_r($row); 
    }
    mysqli_free_result($result);
}
?>
$query="select * from TABLE_NAME where `mem_id`='$id'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM