Search code examples
phpmysqlselectrating

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


I am trying to create a user rating system based on the content stored in 2 MYSQL tables.

One table is called "imagemarkers", and the other "markers".

I want to check the row "authorid", which is the same in both tables, which stores a number based on the 'authorid' of the user.

I want to show a particular image based on the number of times that specific user id number is found in "authorid" from both tables "markers" and "imagemarkers".

this is declared at the top of the page:

  $theID = $_GET['id'];

The code i have so far is:

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

 <? } ?>

My mysql table row "authorid" is stored as INT, 11, and allow null ticked.

The error im getting is:

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

Please help me solve this. Thank you very much for your time.

Update, This is the new code i have, and it is still displaying '$img0', when it should be showing '$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";
  }
  ?>

I am counting where the specific user number shows and matches 'theID' in the 'authorid' row, from both tables 'imagemarkers' and 'markers'. The user is storing other data in the database, and by storing this fills the 'authorid' row in both 'markers' and 'imagemarkers' based on which form they fill in to store the info.

I hope this helps you understand what i am trying to do..


Solution

  • add single quotes around $theID, also make sure $theID is checked as an integer or place (int) in front of it:

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

    if the error still occurs replace with this, and see what error you get:

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

    Because then $result3 is no valid result resource for mysql_num_rows