简体   繁体   English

脚本计算两列并回显结果

[英]Script to count two columns and echo result

I have a working SQL query that I'm trying to use in a small PHP script but getting Parse error, tried many variations. 我有一个正在尝试在小型PHP脚本中使用的SQL查询,但遇到Parse错误,尝试了许多变体。 Hope you can help. 希望能对您有所帮助。 End result would be to have a two field form with 'Date' and 'Channel No' then giving result count of number of 'channel' rows for a given date. 最终结果将是具有“日期”和“通道号”的两个字段形式,然后给出给定日期的“通道”行数的结果计数。 Sorry fairly new PHP/SQL, thanks. 对不起,相当新的PHP / SQL,谢谢。

    <?php
// Connect to MSSQL and select the database
$link = mssql_connect('localhost', 'root', '', 'jm_db');
mssql_select_db('jm_db');

// Select all our records from a table

$mysql_query = mssql_query ('SELECT COUNT(*) FROM asterisk_cdr 
WHERE calldate LIKE '%2014-10-11%'
     AND channel LIKE '%SIP/4546975289%');

echo $sql;

?>

I have re-done the code but getting 'Warning: mysql_fetch_array() expects parameter 1 to be resource' and undefined variable. 我重新完成了代码,但是得到了“警告:mysql_fetch_array()期望参数1为资源”和未定义的变量。

<?php

// Create connection
$mysqli = new mysqli($localhost, $root, $jm_db);

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

$sql = ("SELECT COUNT(*) FROM asterisk_cdr 
    WHERE calldate LIKE '%2014-10-11%'
    AND channel LIKE '%SIP/4546975289%'");

    $results= array();
while ($result = mysql_fetch_array($sql)) {
    $results[]= $result;
}

foreach($results as $result){
    echo $result['calldate'] . " " . $result['channel']; 
}

?>

You're missing a quote (Stack's syntax highlighting shows you), yet it should be replaced with an opening double quote and ending with the same. 您缺少引号(Stack的语法高亮显示了您),但是应将其替换为开头的双引号,并以相同的结尾。 You can't use all single quotes. 您不能使用所有单引号。

I replaced the opening single quote with a double, along with a matching closing double quote. 我用一个双引号替换了开头的单引号,以及一个匹配的关闭的双引号。

$mysql_query = mssql_query ("SELECT COUNT(*) FROM asterisk_cdr 
WHERE calldate LIKE '%2014-10-11%'
     AND channel LIKE '%SIP/4546975289%'");

As a sidenote, you're echoing the wrong variable. 附带说明,您回显了错误的变量。

However, that is not how you would echo out results, but with a loop. 但是,这不是您要回显结果的方式,而是循环执行。

Something like, and replacing Fieldname with the one you want to use: 类似,将Fieldname替换为您要使用的名称:

while ($row = mssql_fetch_assoc($mysql_query)) {

    print $row['Fieldname'] . "\n";

}

or use mssql_fetch_array() 或使用mssql_fetch_array()

You can also use: 您还可以使用:

$results= array();
while ($result = mssql_fetch_array($mysql_query)) {
    $results[]= $result;
}

foreach($results as $result){
    echo $result['calldate'] . " " . $result['channel']; 
}

For more information on Microsoft SQL Server's function, consult: 有关Microsoft SQL Server功能的更多信息,请咨询:

this is a simple example with PDO 这是PDO的简单示例

<?php
  try {
    $dns  = 'mysql:host=localhost;dbname=jm_db';
    $user = 'root';
    $pass = '';

    $options = array(
      PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
      PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    );
    $cnx = new PDO( $dns, $user, $pass, $options );
    $select = $cnx->query("SELECT COUNT(*) as count FROM asterisk_cdr WHERE calldate LIKE '%2014-10-11%' AND channel LIKE '%SIP/4546975289%'");

    $select->setFetchMode(PDO::FETCH_OBJ);

    while( $row = $select->fetch() )
    {
      echo '<h1>', $row->count , '</h1>';
    }
  } catch ( Exception $e ) {
    echo "Connect failed : ", $e->getMessage();
    die();
  }
    $mysql_query = mssql_query ('SELECT COUNT(*) FROM asterisk_cdr 
    WHERE calldate LIKE '%2014-10-11%'
    AND channel LIKE '%SIP/4546975289%');

    while($row=mssql_fetch_array($mysql_query))
    {
    echo $row[0];
    }
    $mysqli = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

$mysql_query = mysqli ("SELECT COUNT(*) FROM asterisk_cdr WHERE calldate LIKE '%2014-10-11%' AND channel LIKE '%SIP/4546975289%'");

while ($row = mysql_fetch_array($mysql_query, MYSQL_ASSOC)) {
   echo ($row["channel"]);
}

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

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