我一直在研究一些代码,根据他们上传的与同事和学生分享的项目,向教师颁发徽章。
当数组为空时我遇到了问题。什么都没有出现。
我尝试了一个空()函数,但我必须把它放在错误的位置,因为它开始出现在上传的用户身上。
所以基本上在下面的代码中,它是“if $ uploads< 1”部分,我遇到了困难。
..由此产生的问题是如何在mysql_fetch_array($ upload_count)为空的情况下收到消息?
<?php
$total_uploads = mysql_query("SELECT members.member_id, members.member_firstname, members.member_lastname, COUNT(*)
FROM uploads
JOIN members
ON uploads.member_id = members.member_id
GROUP BY uploads.member_id ");
$total_uploads_count = @mysql_num_rows($total_uploads);
$upload_count = mysql_query("SELECT members.member_id, members.member_firstname, members.member_lastname, COUNT(*)
FROM uploads
JOIN members
ON uploads.member_id = members.member_id AND members.member_id = ". $_SESSION['member_id'] . "
GROUP BY uploads.member_id ");
while($row_count=mysql_fetch_array($upload_count))
{
$uploads = $row_count['COUNT(*)'];
$unlock2 = 5 - $row_count['COUNT(*)'] ;
$unlock3 = 20 - $row_count['COUNT(*)'] ;
$unlock4 = 50 - $row_count['COUNT(*)'] ;
$unlock5 = 100 - $row_count['COUNT(*)'] ;
$unlock6 = 200 - $row_count['COUNT(*)'] ;
echo " you have <span class =\"blue_inherit\"> $uploads </span> uploads.<br/><br/>";
if ($uploads < 1 )
{
echo "Upload a resource to share in order to unlock your novice uploader badge!<br/>
<img src=\"images/badges/badge_e_share_1_locked.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" />";
}
else if ($uploads >= 1 && ($uploads < 5 ))
{
echo "Well done, you've unlocked your novice sharer's badge. <br/>
<img src=\"images/badges/badge_e_share_1.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/>
If you upload $unlock2 more resources, you can unlock your E-sharing advanced beginner's badge. <br/>
<img src=\"images/badges/badge_e_share_2_locked.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/>
";
}
else if ($uploads >= 5 && ($uploads < 20 ))
{
echo "Well done, you've unlocked your E-sharing advanced beginner's badge. <br/>
<img src=\"images/badges/badge_e_share_2.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/>
If you upload $unlock3 more resources, you can unlock your E-sharing mini-hero badge. <br/>
<img src=\"images/badges/badge_e_share_3_locked.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/> ";
}
else if ($uploads >= 20 && ($uploads < 50 ))
{
echo "Well done, you've unlocked your E-sharing mini-hero badge. <br/>
<img src=\"images/badges/badge_e_share_3.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/>
If you upload $unlock4 more resources, you can unlock your E-sharing hero badge. <br/>
<img src=\"images/badges/badge_e_share_4_locked.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/> ";
}
else if ($uploads >= 50 && ($uploads < 100 ))
{
echo "Well done, you've unlocked your E-sharing hero badge. <br/>
<img src=\"images/badges/badge_e_share_4.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/>
If you upload $unlock5 more resources, you can unlock your E-sharing super-hero badge. <br/>
<img src=\"images/badges/badge_e_share_5_locked.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/> ";
}
else if ($uploads >= 100 && ($uploads < 200 ))
{
echo "Well done, you've unlocked your E-sharing super-hero badge. <br/>
<img src=\"images/badges/badge_e_share_5.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/>
If you upload $unlock6 more resources, you can unlock your E-sharing Guru badge. <br/>
<img src=\"images/badges/badge_e_share_6_locked.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/> ";
}
else
{
echo "You've now a Guru!! <br/>
<img src=\"images/badges/badge_e_share_6.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" /><br/> ";
}
}
?>
答案 0 :(得分:2)
好吧..你也可以将你的变量名改为
$ please_inject_me,$ please_inject_me1等等......
第一课是请使用PDO
现在问题(根据我对你的问题的理解):
// Do your first query prepare it and then execute it, and then do a fetch and count the number of rows returned by your $query sql query
$sql = "SELECT members.member_id, members.member_firstname, members.member_lastname, COUNT(*) FROM uploads JOIN members ON uploads.member_id = members.member_id GROUP BY uploads.member_id ";
$query = $pdo->prepare($sql);
$query->execute();
$total_uploads_count = $query->fetch(PDO::FETCH_NUM);
// I guess this query you have going is counting how far the member / teacher is away from the unlock goals here we set the variable that has the num row count
$sql2 = "SELECT members.member_id, members.member_firstname, members.member_lastname, COUNT(*) as 'total' FROM uploads JOIN members ON uploads.member_id = members.member_id AND members.member_id = :username GROUP BY uploads.member_id";
$query2 -> $pdo->prepare($sql2);
$query2->bindParam(':username', $_SESSION['member_id']); // Do not forget to bind your parameters either in a array or hard code it out like I just did
$query2->execute();
$row = $query2->fetch();
$upload_count = $row['total']; // Alias of COUNT(*);
Now that you PDO queries are set up now you have to create the PDO instance like so :
You could make a file called connec.php
具有以下内容:
//We connect to the database
$host="xxxxxx"; // Host name
$username="xxxxxxxxx"; // Mysql username
$password="xxxxxxx"; // Mysql password
$db_name="xxxxxxx"; // Database name
// Connect to server via PHP Data Object
$pdo = new PDO("mysql:host=xxxxx;dbname=xxxxxxxx", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
?>
一旦你这样做,然后在任何有数据库交互(查询)的页面的顶部,你就可以这样做:
要求('..................(w / e你的路径是)/connect.php');
既然您可以使用单个变量($ upload_count)计算查询中的所有行,请执行以下操作:
// get the differences in the counts
$unlock2 = 5 - $upload_count;
$unlock3 = 20 - $upload_count;
$unlock4 = 50 - $upload_count;
$unlock5 = 100 - $upload_count;
$unlock6 = 200 - $upload_count;
现在你说问题是如果结果是0 /没有结果,如果返回的结果是空的,或者就数据库内容而言,如何回显。
然后只做`
if($upload_count == 0) { // This is empty
....................
} else { // This is not empty have fun :D
...........
}
我想我回答了你的问题......如果不让我知道的话。
答案 1 :(得分:-2)
我相信如果mysql计数返回空而不是0
试试这个
if (empty($uploads) === true) {
echo "Upload a resource to share in order to unlock your novice uploader badge!<br/>
<img src=\"images/badges/badge_e_share_1_locked.png\" width=\"200\" height=\"200\" alt=\"Sharing Badge\" />";
}