我希望能够在我的数据库的特定表中获得搜索结果的数量(让我们将其命名为wp_postmeta)。例如,如果我搜索确切的短语"星期三(9月3日)",我希望能够通过php获得搜索结果的数量。
数据库示例
meta_id|post_id|meta_key |meta_value
1 |123 |delivery-schedule |Wednesday (3 Sep)
2 |124 |delivery-schedule |Wednesday (3 Sep)
3 |125 |delivery-schedule |Thursday (4 Sep)
4 |126 |delivery-schedule |Friday (5 Sep)
5 |127 |delivery-schedule |Wednesday (3 Sep)
所以在这种情况下,我应该得到3
的结果。对不起,如果听起来太容易了。但我无法在任何地方找到答案。谢谢!
< - 这是我根据gihanmu建议尝试的 - >
function test1() {
$variable = "Wednesday(3 Sep)";
$sql = "SELECT COUNT(*) FROM wp_postmeta WHERE meta_value='".$variable."'";
//In your example $variable is Wednesday(3 Sep)
$result=mysqli_query($connection,$sql);
$count_array=mysqli_fetch_array($result);
$exact_count=array_shift($count);
echo $exact_count;
}
add_action( 'woocommerce_before_order_notes', 'test1');
答案 0 :(得分:2)
$sql = "SELECT COUNT(*) FROM wp_postmeta WHERE meta_value='".$variable."'";
//In your example $variable is Wednesday(3 Sep)
$result=mysqli_query($connection,$sql);
$count_array=mysqli_fetch_array($result);
$exact_count=array_shift($count);
$ count_array是一个数组,所以你必须使用array_shift()函数拉出第一个元素以获得$ exact_count
答案 1 :(得分:1)
SELECT COUNT(meta_id) FROM wp_postmeta WHERE meta_value = 'Wednesday (3 Sep)';
但是既然你正在使用PHP,那么你将需要mysqli;也许是这样的
$mysqli = new mysqli("localhost", "user", "password", "database");
$mydate = 'Wednesday (3 Sep)';
$stmt = $mysqli->prepare("SELECT COUNT(meta_id) FROM wp_postmeta WHERE meta_value = ?");
$stmt->bind_param('s', $mydate);
$result = $stmt->execute();
$stmt->bind_result($col1);
while ($stmt->fetch()) {
echo "you have {$col1} rows matching $mydate\n";
}