php获取数据库表的搜索结果号

php获取数据库表的搜索结果号

问题描述:

i would like to be be able to get the number of search results in a particular table of my database (let's name it wp_postmeta). For example, if i search for the exact phrase "Wednesday(3 Sep)", i want to be able to get the number of search results via php.

database example

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)

So in this case, i should get the result as 3. sorry if this sounds too easy. but i cant find my answer anywhere. thanks!

<--this is what i have tried based on gihanmu suggestion-->

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');

我希望能够在我的数据库的特定表中获取搜索结果的数量(让我们说出来) 它wp_postmeta)。 例如,如果我搜索确切的短语“星期三(9月3日)”,我希望能够通过php获得搜索结果的数量。 p>

数据库示例 p>

  meta_id | post_id | meta_key | meta_value 
 
1 | 123 | delivery-schedule |星期三(3月3日)
2 | 124 | delivery-schedule |星期三(3月3日)
3 |  125 |交货时间表|星期四(9月4日)
4 | 126 |交货时间表|星期五(9月5日)
5 | 127 |交货时间表|星期三(9月3日)
  code>  pre> \  n 
 

所以在这种情况下,我应该得到 3 code>的结果。 对不起,如果听起来太容易了。 但我无法在任何地方找到答案。 谢谢! p>

&lt; - 这是我根据gihanmu建议尝试的 - &gt; p>

  function test1(){  
 $ variable =“Wednesday(9 Sep)”; 
 $ sql =“SELECT COUNT(*)FROM wp_postmeta WHERE meta_value ='”。$ variable。“'”; 
 //在您的示例中$ variable是星期三 (9月3日)
 $ result = mysqli_query($ connection,$ sql); 
 $ count_array = mysqli_fetch_array($ result); 
 $ exact_count = array_shift($ count); 
 echo $ exact_count; 
} \  nadd_action('woocommerce_before_order_notes','test1'); 
  code>  pre> 
  div>

 $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 is an array, so you have to pull the first element out using array_shift() function to get the $exact_count

SELECT COUNT(meta_id) FROM wp_postmeta WHERE meta_value = 'Wednesday (3 Sep)';  

But since you're using PHP, you're going to need mysqli; maybe something like this

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