伯爵&显示数据库中的项目数

时间:2013-05-16 06:19:59

标签: php mysql

您好我想显示数据库中的项目数。以下是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

请协助

3 个答案:

答案 0 :(得分: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 :(得分: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;
?>

答案 2 :(得分: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(*)'];