【发布时间】:2014-04-05 04:19:24
【问题描述】:
我不断收到错误“未定义的变量:myquery - 查询为空”我已经尝试了几个小时的许多不同的东西,但仍然无法让它工作。我对php很陌生,发现它很混乱。因此,请详细解释或将我链接到有用的东西,以便我能更多地理解它。 这是代码:
<?php
include_once("scripts/connect_db.php");
$totals = "";
$rating = "";
$sql = mysql_query("SELECT `ratings` FROM `blog_posts` WHERE `id`='1'");
$result = mysql_query($myquery) or die(mysql_error());
while($row = mysql_fetch_array($sql)){
$myNums = $row["ratings"];
$kaboom = explode(",", $myNums);
$result = array_count_values($kaboom);
foreach($result as $key => $value){
if ($value =="1"){
$howMany = "person";
}else{
$howMany = "people";
}
if($key ==""){
$pic = "images/starsNorm.png";
}
else if($key == "1"){
$stars = "star";
$pic = "images/1lit.png";
}else if($key == "2"){
$stars = "stars";
$pic = "images/2lit.png";
}else if($key == "3"){
$stars = "stars";
$pic = "images/3lit.png";
}else if($key == "4"){
$stars = "stars";
$pic = "images/4lit.png";
}else if($key == "5"){
$stars = "stars";
$pic = "images/5lit.png";
}
$totals .= '<p class="small" style="color:#32CD32;">' . $key . ' ' . $stars . ': <img src="' . $pic . '" alt="stars" />
' . $value . ' ' .$howMany . '</p>';
}
$count = count($kaboom);
$sum = array_sum($kaboom);
$avg = $sum / $count;
$roundit = floor($avg);
if($roundit == 0) {
$rating = '<p class="small" style="color:#32CD32;">This ... has not yet been rated. You can be first!</p>';
}else if ($count == 1) {
$rating = '<p class="small" style="color:#32CD32;">Current Article Rating: ' . $roundit . '/5 stars <img id="myStars" src="images/starsNorm.png"
alt="stars"/></p>';
}else if($count > 1) {
$rating = '<p class="small" style="color:#32CD32;">Current Article Rating: ' . $roundit . '/5 stars <img id="myStars" src="images/starsNorm.png"
alt="stars"/></p>';
}else{
$rating = "sorry there is an error in the system... please try refreshing the page";
}
}
?>
【问题讨论】: