繁体   English   中英

显示基于2个表的MYSQL结果的特定img。 选择,PHP

[英]show specific img based on MYSQL results from 2 tables. SELECT, PHP

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

一个表称为“图像标记”,另一个表称为“标记”。

我想检查“ authorid”行,这在两个表中都相同,该行存储了一个基于用户的“ authorid”的数字。

我想根据在“标记”和“图像标记”两个表的“ 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";
 }
 ?>

 <? } ?>

我的mysql表行“ authorid”存储为INT,11,并允许为空打勾。

我得到的错误是:

   Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource....

请帮我解决这个问题。 非常感谢您的宝贵时间。

更新,这是我拥有的新代码,当它应该显示“ $ img3”时,它仍显示“ $ img0”:

 <? $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";
  }
  ?>

我正在计算“ imagemarkers”和“ markers”两个表中特定用户号在“ authorid”行中显示和匹配“ theID”的位置。 用户正在数据库中存储其他数据,并通过存储该数据来填充“标记”和“图像标记”中的“ authorid”行,这取决于他们填写的用于存储信息的表格。

我希望这可以帮助您了解我正在尝试做的事情。

$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_num_rows的无效结果资源

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

首先,如果两个表中都存在行“ authorid”,则应在查询中指定要引用的行:

$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”循环内的任何地方都用$ row [0]替换$ result3:

if ($row[0] > 1) { ...

最后,逻辑存在问题。

您能否解释完整的数据库结构,除了authorid之外,还有哪些列?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM