Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据两个表的MYSQL结果显示特定的img。选择,PHP_Php_Mysql_Select_Rating - Fatal编程技术网

根据两个表的MYSQL结果显示特定的img。选择,PHP

根据两个表的MYSQL结果显示特定的img。选择,PHP,php,mysql,select,rating,Php,Mysql,Select,Rating,我试图根据存储在2个MYSQL表中的内容创建一个用户评级系统 一个表称为“imagemarkers”,另一个表称为“markers” 我想检查“authorid”行,它在两个表中都是相同的,它根据用户的“authorid”存储一个数字 我想根据在“markers”和“imagemarkers”表的“authorid”中找到特定用户id号的次数来显示特定的图像 这在页面顶部声明: $theID = $_GET['id']; 到目前为止,我掌握的代码是: <? $img0 = "&

我试图根据存储在2个MYSQL表中的内容创建一个用户评级系统

一个表称为“imagemarkers”,另一个表称为“markers”

我想检查“authorid”行,它在两个表中都是相同的,它根据用户的“authorid”存储一个数字

我想根据在“markers”和“imagemarkers”表的“authorid”中找到特定用户id号的次数来显示特定的图像

这在页面顶部声明:

  $theID = $_GET['id'];
到目前为止,我掌握的代码是:

   <? $img0 = "<img src='../webimages/0star.png'>"; 
  $img1 = "<img src='../webimages/1star.png'>"; 
  $img2 = "<img src='../webimages/2star.png'>";
  $img3 = "<img src='../webimages/3star.png'>";
  $img4 = "<img src='../webimages/4star.png'>";
  $img5 = "<img src='../webimages/5star.png'>"; ?>

  <? $result3 = mysql_query("SELECT * FROM `markers`, `imagemarkers` WHERE authorid = $theID");
$rows = mysql_num_rows($result3);  ?>

 <?php 
while($row = mysql_fetch_array($result3)){

 if ($result3 > 1) {
     echo "$img1";
 } elseif ($result3 == 5) {
     echo "$img2";
 } elseif ($result3 == 10) {
     echo "$img3";
 } elseif ($result3 == 20) {
     echo "$img4";
 } elseif ($result3 == 30) {
     echo "$img5";
 } else {
     echo "$img0";
 }
 ?>

 <? } ?>
请帮我解决这个问题。 非常感谢您抽出时间

更新,这是我的新代码,它仍然显示“$img0”,而它应该显示“$img3”:

 <? $img0 = "<img src='../webimages/0star.png'>"; 
$img1 = "<img src='../webimages/1star.png'>"; 
$img2 = "<img src='../webimages/2star.png'>";
$img3 = "<img src='../webimages/3star.png'>";
$img4 = "<img src='../webimages/4star.png'>";
$img5 = "<img src='../webimages/5star.png'>";   ?>

 <? $row[0] = mysql_query("SELECT * FROM `markers`, `imagemarkers` WHERE authorid = '".    (int)$theID."'");

  if ($row[0] > 1) {
      echo "$img1";
  } elseif ($row[0] > 5) {
      echo "$img2";
  } elseif ($row[0] > 10) {
      echo "$img3";
  } elseif ($row[0] > 20) {
      echo "$img4";
  } elseif ($row[0] > 30) {
      echo "$img5";
  } else {
      echo "$img0";
  }
  ?>


在“
$theID
”周围添加单引号,同时确保选中$theID作为整数或其前面的位置(int):

$result3 = mysql_query("SELECT * FROM `markers`, `imagemarkers` WHERE authorid = '".(int)$theID."'");
如果错误仍然发生,请替换为该错误,并查看您得到的错误:

$result3 = mysql_query("SELECT * FROM `markers`, `imagemarkers` WHERE authorid = '".(int)$theID."'") or die(mysql_error());

因为
$result3
对于mysql\u num\u行来说不是有效的结果资源

首先,如果两个表中都存在行“authorid”,您应该在查询中指定您所指的是哪一行:

 <? 
$result3 = mysql_query("SELECT * FROM `markers`, `imagemarkers` WHERE authorid=".(int)$theID.")";
$rows = mysql_num_rows($result3);  
?>
$theID = intval($_GET['id']); // to avoid SQL-injections
$result3 = mysql_query("SELECT * FROM `markers` m, `imagemarkers` im WHERE m.authorid = $theID AND im.authorid = $theID");
// m and im are just table-aliases for brevity, you can use whatever names you like
第二,在“while”循环中,将$result3替换为$row[0]:

最后,逻辑有问题


你能解释一下完整的数据库结构吗?除了authorid,还有哪些列?

在这种情况下,
m.authorid=im.authorid和m.authorid='”(int)$theID.“
更适合使用。这将连接两个表。是的,这可能会更好。问题是我不知道发生了什么,为什么需要两张桌子;-)需要主题启动程序提供更多详细信息。注意:当有一个
intval()
时,为什么
(int)
?一定错过了intval;-)伟大的谢谢,第一个select语句成功了。它显示一个图像,但当用户“authorid”在“markers”和“imagemarkers”中出现14次时,它显示“$img0”,因此应该显示“$img5”,因此我的IF语句一定有问题……您的查询尚未优化。既然初始问题已经解决,最好在这里创建一个新问题。您必须问自己(并告诉我们)为什么使用2个表而不仅仅是1个表,以及它们之间的关系/结构是什么。
$theID = intval($_GET['id']); // to avoid SQL-injections
$result3 = mysql_query("SELECT * FROM `markers` m, `imagemarkers` im WHERE m.authorid = $theID AND im.authorid = $theID");
// m and im are just table-aliases for brevity, you can use whatever names you like
if ($row[0] > 1) { ...