用于计算两列和回显结果的脚本

时间:2014-11-05 17:53:44

标签: php sql mysqli

我有一个工作的SQL查询,我试图在一个小的PHP脚本中使用,但得到Parse错误,尝试了很多变化。希望你能帮忙。最终结果将是具有“日期”和“通道号”的两个字段形式,然后给出给定日期的“通道”行数的结果计数。抱歉相当新的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;

?>

我重新完成了代码,但得到'警告: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']; 
}

?>

4 个答案:

答案 0 :(得分:2)

你错过了一个引用(Stack的语法高亮显示你),但它应该用一个开头的双引号替换,并以相同的结尾。您不能使用所有单引号。

我用双倍替换了开头的单引号,以及匹配的结束双引号。

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

作为旁注,你回应了错误的变量。

然而,这不是你如何回应结果,而是循环。

类似于,并将Fieldname替换为您要使用的那个:

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

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

}

或使用mssql_fetch_array()

您也可以使用:

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

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

有关Microsoft SQL Server功能的详细信息,请参阅:

答案 1 :(得分:0)

这是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();
  }

答案 2 :(得分:0)

    $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];
    }

答案 3 :(得分: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"]);
}