我有一个名为mySQL的palettes表,我想返回表中的链接数。基本上,我希望返回link列中的值数。(在本例中为6)。
我试过使用这段代码,但没有起作用。我是一个前端开发人员,不了解php或任何东西。
include "mysql.php";
$select_rows = $mysqli->query("SELECT COUNT(link) FROM palettes");
$rows = mysqli_fetch_array($select_rows);
$total = $rows[0];
echo $total;上面的代码应该或者回显6,对吗?从列link中选择
我的桌子是这样的:

发布于 2014-08-02 04:29:41
使用WHERE子句,并使用LIKE 'http%‘,并将查询更改为计数id:
首先,我假设您的文件“mysql.php”与文件中的某个地方有一个连接,如下所示:
<?php
if (session_status() !== PHP_SESSION_ACTIVE) {session_start();}
$hostname = 'localhost';
$dbname = 'myDatabaseName';
$username = 'admin';
$password = 'myPassword';
$cxn = mysqli_connect($hostname, $username, $password, $dbname) or DIE('Connection to host is failed, perhaps the service is down!');
?>//End connect.php现在你可以试试这个:
include 'mysql.php';
$select_rows = mysqli_num_rows(mysqli_query($cxn, "SELECT id FROM palettes WHERE link LIKE 'http%'"));
echo $select_rows; //Should = 6发布于 2014-08-02 04:59:55
如果您没有可用的链接,我会说place NULL:
您可以通过使Link列允许NULL作为缺省值来实现。在这种情况下,您可以使用计数(Link)。这是推荐的。
如果你必须用空字符串代替空字符串。
select sum(if(Link != "",1,0)) counting from palettes;或者确保所有链接都以http/https开头
select sum(if(Link like "http%",1,0)) counting from palettes;https://stackoverflow.com/questions/25091582
复制相似问题