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

时间:2017-05-21 00:36:08

标签: php mysql wordpress

这两天(和其他人)一直在努力。阅读本网站上的几十篇帖子,从w3学校阅读很多帖子,在线阅读大量其他资源。

我试图做的只是表明有多少人签署了请愿书。 经过多次失败后,我擦掉了我的东西并从头开始。 我尝试了w3中的一些代码来检查我与数据库的连接。 PDO根本不起作用,但面向对象工作正常。 (显示"连接成功"在我的页面上。) 然后尝试下面的代码,我从PHP手册中获取并仍然无法使其工作。 真的很感激一些帮助。

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

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", 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.\n", $row_cnt);

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

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

我还尝试过没有表名周围的单引号。 我的网站是here。 它是一个WordPress网站,如果重要的话。

1 个答案:

答案 0 :(得分:1)

如果您只需要使用select count计算记录:


  SELECT count(Code) as count FROM wp_rm_submissions

此查询将返回带有一条记录的结果集,该记录将包含一个名为count的字段,其值将是wp_rm_submissions表中存储的记录数。

在php中使用mysqli的一个非常非常简单的例子是:

  
 <?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.\n", $count);