我一直收到错误“未定义的变量: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";
}
}
?>
答案 0 :(得分:0)
此行无效:
$result = mysql_query($myquery) or die(mysql_error());
您没有在任何地方定义$ myquery,这正是错误消息告诉您的内容。
我猜它上面的那一行应该只是一个字符串然后那是你要传入的变量。
E.g。
$sql = "SELECT `ratings` FROM `blog_posts` WHERE `id`='1'";
$result = mysql_query( $sql ) or die(mysql_error());
答案 1 :(得分:0)
你没有定义$ myquery变量。你有$ sql。试试这个:
$ result = mysql_query($ sql)或die(mysql_error());
答案 2 :(得分:0)
试试这个
$result = mysql_query("SELECT `ratings` FROM `blog_posts` WHERE `id`='1'");
while ($row = mysql_fetch_array($result)) {
}
答案 3 :(得分:0)
如果你确定连接,查询,获取代码是正确的那么可能是mysql_connect正在连接但没有看到你的数据库。运行此代码以确保找到您的数据库
mysql_connect('localhost') or die ("Connect error");
$res = mysql_query("SHOW DATABASES");
while ($row = mysql_fetch_row($res)) {
echo $row[0], '<br/>';
}
如果找不到,那么您需要修改数据库的权限。例如,进入相关数据库的“权限”选项卡,将“用户名”设置为“任何用户”并检查适用的权限。