2013-05-16 24 views
1

嗨我想显示数据库中项目的数量。以下是PHP代码:计数并显示数据库中的项目数

$jobid = $_SESSION['SESS_MEMBER_JOB']; 
$data = "SELECT * FROM attributes WHERE jobid = $jobid"; 
$attribid = mysql_query($data) or die(mysql_error); 

$count = "SELECT count(*) FROM attributes WHERE jobid = $jobid"; 
$database_count = mysql_query($count); 
//Declare the Array 
$DuetiesDesc = array(); 

print_r ($database_count); 

而不是得到所期望的结果,但是,我得到:

资源ID#14

请协助

+0

['MySQL'](http://php.net/manual/en/book.mysql.php)('mysql_ *'函数)扩展名是[*** deprecated ***](http://php.net) /manual/en/function.mysql-connect.php)。我建议使用['MySQLi'](http://php.net/manual/en/book.mysqli.php)('mysqli_ *'函数)或['PDO'](http://php.net/手册/ en/book.pdo.php)。 – BlitZ

回答

2

应该把它弄出来的,你不应该使用mysql_方式*参见Why shouldn't I use mysql_* functions in PHP?

请参见下面的代码...解释是在评论

$jobid = $_SESSION['SESS_MEMBER_JOB']; 
// escape variables using mysql_real_escape_string 
$data = "SELECT * FROM attributes WHERE jobid =".mysql_real_escape_string($jobid); 

$attrRes = mysql_query($data) or die(mysql_error()); 

// I'm assuming you want all of the attributes return in this query in an array 
$attributes = array(); 
while($row = mysql_fetch_assoc($attrRes)){ 
    $attributes[] = $row; 
} 

// Now if you want the count we have all of the records in the attributes array; 

$numAttributes = count($attributes); 


// here is an example of how you can iterate through it.. 
print "<p>Found ".$numAttributes." attributes</p>"; 
print "<table>"; 
foreach($attributes as $row){ 
    print "<tr>"; 
    foreach ($row as $cell){ 
     print "<td>".$cell."</td>"; 
    } 
    print "</tr>"; 
} 
print "</table>"; 
1

试试这个

<?php 
$jobid = $_SESSION['SESS_MEMBER_JOB']; 
$data = "SELECT * FROM attributes WHERE jobid =$jobid"; 
$attribid = mysql_query($data) or die(mysql_error); 
$count=mysql_num_rows($attribid); 
echo $count; 
?> 
1

试试这个

$jobid = $_SESSION['SESS_MEMBER_JOB']; 
$data = "SELECT *FROM attributes WHERE jobid =$jobid"; 
$attribid = mysql_query($data) or die(mysql_error); 

$count = "SELECT count(*) FROM attributes WHERE jobid = $jobid"; 
$database_count = mysql_query($count); 
//Declare the Array 
$DuetiesDesc = array(); 
$database_count=mysql_fetch_assoc($database_count); 
echo $database_count['count(*)']; 
相关问题