简体   繁体   English

获取特定行的列值

[英]fetch column value of particular row

I need to fetch a particular column value of a particular row using PHP in MySQL. 我需要在MySQL中使用PHP来获取特定行的特定列值。 Here is an example, check this imag I want to fetch the mostOnline and its value, I've tried this below code but doesn't work 这是一个示例,检查此图像,我想获取mostOnline及其值,我已经在下面的代码中尝试过此方法,但是不起作用

<?php
    $query = $forumdb->prepare("SELECT variable FROM smf_settings WHERE Value = 'mostOnline' LIMIT 1");
    $query->execute();
    $result = $query->fetch();
?>
<div class="media-body">
<p id="greet" align="left">Total Members: <?php echo $result ?> <p>
</div>

I'm pretty sure I've done some mistakes, how could that be fixed and be shown? 我很确定自己犯了一些错误,该如何解决并显示出来? I'm using PDO by the way. 我正在使用PDO。

try this 尝试这个

$query = $forumdb->prepare("SELECT variable FROM smf_settings WHERE variable = 'mostOnline' LIMIT 1");
$result = $query->fetch();
print_r($result);
echo $result['value'];//this should echo 211

You are trying to fetch data from Value column where the value is mostOnline . 您正在尝试从其中值为 mostOnline值”列中获取数据。 Which is definitely not correct. 这绝对是不正确的。

 "SELECT variable FROM smf_settings WHERE Value = 'mostOnline' LIMIT 1"

Try this: 尝试这个:

$query = $forumdb->prepare("SELECT variable, value FROM smf_settings WHERE variable = 'mostOnline' LIMIT 1");

In your HTML: 在您的HTML中:

<p id="greet" align="left">Total Members: <?php echo $result['value'];?> </p>

Note the improper closing </p> tag in your code too. 还要注意代码中不正确的</p>标记。

Output: 输出:

Total Members: 211

your code is incomplete because you have to bind the result like this: 您的代码不完整,因为您必须这样绑定结果:

<?php
    $data = 'mostOnline';  
    $result = 0; // if database connection or error default is 0 
        if ($query = $forumdb->prepare("SELECT variable FROM smf_settings WHERE Value=?  LIMIT 1")) {
        $query->bind_param('s', $data);
        $query->execute();
        $query->bind_result($variable);
        $query->store_result();
        $selected_rows = $query->num_rows;

        if ($selected_rows > 0) {
           while($query->fetch()) {
           $result = $variable;
           }
        } else {
        $result = 0;
        }
        $query->free_result();
        $query->close();
        }

    @$forumdb->close();
?>

<div class="media-body">
<p id="greet" align="left">Total Members: <?php echo $result; ?> <p>
</div>

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

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