【问题标题】:How do I display the result of this SQL using PHP [closed]如何使用 PHP 显示此 SQL 的结果 [关闭]
【发布时间】:2018-01-15 03:54:27
【问题描述】:

我不知道如何在 PHP/MySql 中显示此查询的结果。如果可能,我想使用准备好的语句。

SELECT count( DISTINCT(video_view_ip) ) FROM video_views

【问题讨论】:

  • 有很多关于如何在PHP中显示SQL查询结果的教程。

标签: php mysql sql count distinct


【解决方案1】:

要从sql中获取数据并使用php打印,请使用这种方式

首先连接到数据库

                $con = mysqli_connect("localhost","username","password");
                if (!$con)
                {
                     die('Could not connect: ' . mysql_error());
                }
                mysqli_select_db($con,"db_name");

连接后,从表中检索数据

                $sql_fetch_vide_count = "SELECT count( DISTINCT(video_view_ip) ) as video_count FROM video_views ";
                $video_result = $con->query($sql_fetch_vide_count);
                if (!$video_result) {
                    die('Could not enter data: ' . mysql_error());
                }

                //getting the date to array
                $row_video = $video_result->fetch_assoc();

                //Fetch the count result
                $video_count = isset($row_video ['video_count ']) ? $row_video ['video_count '] : 0;

如果你从数据库中获取所有数据然后使用

                $array_videos = array();
                while ($row_video = $video_result->fetch_assoc()) {
                    $array_videos [] = $row_video ;
                }

您可以将所有数据作为数组放入 $array_videos

【讨论】:

  • 谢谢,在 sql 中使用“as video_count”是我需要知道的!
【解决方案2】:

首先,我们需要获取到数据库的连接。为此,我们将使用 PDO:

<?php

$config = [
  'driver'   => 'mysql',
  'host'     => 'localhost',     // replace this with the actual mysql host if needed
  'port'     => 3306,            // default mysql port is 3306
  'database' => 'my_database',   // replace this with the database you are using
  'username' => 'admin',         // replace this with your username
  'password' => 'password',      // replace this with your password
];

$connection = new PDO(
  vsprintf('%s:Server=%s,%s;Database=%s', [
    $config['driver'],
    $config['host'],
    $config['port'],
    $config['database'],
  ]),
  $config['username'],
  $config['password']
);

为了保持配置的私密性,您可以将其存储在一个单独的只读文件中。然后您可以使用parse_ini_file 阅读此配置。

要运行查询并存储结果,我们必须执行以下操作:

// in this case, we can use fetchColumn to retrieve the count, since it is a single value. However, we will usually want to use fetch() for most cases.
$count = $connection
  ->query('SELECT count( DISTINCT(video_view_ip) ) FROM video_views')
  ->fetchColumn();

要显示结果,我们可以简单地echo 他们出来:

echo $count;

【讨论】:

    【解决方案3】:

    检查这个例如关于如何在 php 页面上产生你的输出 已编辑:在 OP 请求之前添加了准备好的语句

    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    
    // prepare and bind
    $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
    $stmt->bind_param("sss", $firstname, $lastname, $email);
    
    // set parameters and execute
    $firstname = "John";
    $lastname = "Doe";
    $email = "john@example.com";
    $stmt->execute();
    
    $firstname = "Mary";
    $lastname = "Moe";
    $email = "mary@example.com";
    $stmt->execute();
    
    $firstname = "Julie";
    $lastname = "Dooley";
    $email = "julie@example.com";
    $stmt->execute();
    
    echo "New records created successfully";
    
    $stmt->close();
    $conn->close();
    ?> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-21
      • 2016-01-06
      • 1970-01-01
      • 1970-01-01
      • 2015-08-31
      • 2013-01-16
      • 1970-01-01
      • 2020-06-02
      相关资源
      最近更新 更多