简体   繁体   English

试图计算数据库中的条目

[英]trying to count entries in a database

I'm trying to count entries in a database based on 2 basic criteria. 我正在尝试基于2个基本条件对数据库中的条目进行计数。 It is returning a blank result, even though there are results to be found. 即使找到结果,它也会返回空白结果。 Anyone have any idea what I am doing wrong here? 有人知道我在这里做错了吗? I have tried it so many different ways and they all return no result. 我已经尝试了许多不同的方法,但它们均未返回任何结果。 (If I enter the query directly in phpmyadmin it returns a result.) (如果我直接在phpmyadmin中输入查询,它将返回结果。)

$sql = "SELECT count(*) as total_count from orderOption3Detail WHERE orderDate='$orderDate' AND studentID='$studentID'"; 
$numericalResult = mysql_query($sql, $con);
$row = mysql_fetch_object($numericalResult);
$totalOrders1 =  $row->total_count;
echo "My orders:" . $totalOrders1; 

As others stated, make sure you sanitize variables before they go into query. 正如其他人所述,请确保在查询变量之前先对其进行清理。

$sql = "SELECT * FROM orderOption3Detail WHERE orderDate = '" . $orderDate . "' AND studentID = '" . $studentID . "'";

$sql_request_data = mysql_query($sql) or die(mysql_error());
$sql_request_data_count = mysql_num_rows($sql_request_data);

echo "Number of rows found: " . $sql_request_data_count;

That's all you need. 这就是您所需要的。

Edited: providing full code corrected: 编辑:提供完整的代码更正:

$con=mysqli_connect($db_host,$db_user,$db_pass,$db_name); // Check connection 
if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } //global option 1 
$sql = "SELECT count(*) as total_count from orderOption3Detail WHERE orderDate='$orderDate' AND studentID='$studentID'"; 
//echo $sql; 
$numericalResult = $con->query($sql); 
$row = mysqli_fetch_object($numericalResult); 
echo $row->total_count; //echo (int) $row->total_count;

Please test this and let me know. 请对此进行测试,并让我知道。 Good luck! 祝好运! ----- End Editing ---- -----结束编辑----

Have you tested assigning values directly as a test in your SQL string, like: 您是否测试过直接在SQL字符串中将值分配作为测试,例如:

$sql = "SELECT count(*) as total_count from orderOption3Detail WHERE orderDate='05/23/2012' AND studentID='17'";

Also, did you check if the date's format is correct, reading that $orderdate variable and testing it in PHPMyAdmin? 另外,您是否检查日期的格式是否正确,读取了$ orderdate变量并在PHPMyAdmin中对其进行了测试?

Did you read the $sql with values inserted and test in PHPMyAdmin and worked? 您是否读过带有插入值的$ sql并在PHPMyAdmin中进行了测试并正常工作?

Also, check the connection to assure there is no problem there. 另外,检查连接以确保那里没有问题。

One more thing, sorry. 另一件事,对不起。 You seem to be using the wrong syntax in your mysql_query statement. 您似乎在mysql_query语句中使用了错误的语法。 That way works for mysqli_query, and the parameters would be inverted. 这种方式适用于mysqli_query,并且参数将被反转。 Try only: 仅尝试:

$numericalResult = mysql_query($sql);

Provided you made the connection and database selection previously, like in: 前提是您之前进行了连接和数据库选择,例如:

$connection=mysql_connect($db_host, $db_username, $db_password);
if (!$connection)
          {
            $result=FALSE;
            die('Error connecting to database: ' . mysql_error());
          }

     // Selects database
     mysql_select_db($db_database, $connection);

Best wishes, 最好的祝愿,

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

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