dongzhi6146 2017-05-21 00:36 采纳率: 100%
浏览 29
已采纳

无法获得简单的面向对象的行数来显示

Been struggling with this for two days (and the rest). Read a couple of dozen posts from this site, been reading lots from w3 schools and lots of other resources online.

All I'm trying to do is show how many people have signed a petition. After many failures, I wiped what I had and started from scratch. I tried a few bits of code from w3 to check my connection to my database. The PDO didn't work at all, but object oriented worked fine. (Showed "connection successful" on my page.) So then tried the code below, which I took from the PHP manual and still can't get it to work. Would really appreciate some help.

<?php
$link = mysqli_connect("localhost", "my user", "my password", "my db");

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

if ($result = mysqli_query($link, "SELECT Code, Name FROM 'wp_rm_submissions' ORDER BY Name")) {

    /* determine number of rows result set */
    $row_cnt = mysqli_num_rows($result);

    printf("So far %d people have signed the petition.
", $row_cnt);

    /* close result set */
    mysqli_free_result($result);
}

/* close connection */
mysqli_close($link);
?>

I've also tried it without the single quotes around the table name. My website is here. It's a WordPress site, if that matters.

  • 写回答

1条回答 默认 最新

  • dongse7261 2017-05-21 01:55
    关注

    If you only need to count the records use select count:

    
      SELECT count(Code) as count FROM wp_rm_submissions
    
    

    This query will return a resultset with one record, the record will have a field called count and it's value will be the number of records stored in the wp_rm_submissions table.

    A very very very simple example in php, using mysqli, would be:

      
     <?php 
        // connect to mysql
        $mysqli = new mysqli('host','user','password','schema');
        // execute the query
        $result = $mysqli->query('SELECT count(Code) as count FROM wp_rm_submissions');
        // fetch the record as an associative array
        $record = $result->fetch_assoc();
        // get the value 
        $count  = (int)$record['count'];
    
        $mysqli->close(); 
    
        printf("So far %d people have signed the petition.
    ", $count);
    
      
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像