General Community > Scripting Help

Hmm, query is not good

(1/2) > >>

Metho:
Alright, probably something stupid on my part, but can anyone tell me why this doesn't work?
--- Code: ---<?PHP
function lastuploads($length)
{
$connection = mysql_connect("localhost", "myname", "mypass") or
die("Invalid
server or user");


mysql_select_db("methonis_yabbse",$connection) or die("Invalid
database");
$query = ("SELECT pid, title FROM yabbse_gallery_pictures ORDER BY pid DESC LIMIT $length");
$result = mysql_db_query($query, $connection) or
die("Error in query");
   if (mysql_num_rows($result) <= 0) {
      echo "No uploads...";
      return false;
   }
   $i = 1;
   echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"1\">\n";
   while ($row = mysql_fetch_array($result)) {
      echo "<tr><td style=\"white-space: nowrap;\"><a href=\"displayimage.php?album=lastup&pid={$row['pid']}\">" . truncate_string($row['title']) . "</a></td></tr>\n";
      $i++;
   }
   echo "</table>";
}

/**************************************************************************
 * Main code
 **************************************************************************/

?>
   <tr>
      <td valign="top" class="tableh1_compact" style="white-space: nowrap;"><div class="nav">
         <a href="thumbnails.php?album=lastup">
         <b>Last uploads</b></a><br></div>
      </td>
      <td valign="top" class="tableh1_compact" style="white-space: nowrap;"><div class="nav">
         <a href="thumbnails.php?album=topn">
         <b>Top pictures</b></a> /
         <a href="thumbnails.php?album=lasthits">
         <b>Last hits</b></a><br></div>
      </td>
   </tr>
   <tr>
      <td valign="top" class="tableb_compact">
         <?php lastuploads(5); ?>
      </td>
   </tr>
--- End code ---

The db connection seems fine, it's dying at the query...I'm tired, but that query looks correct to me...Anyone see it?

Thanks,

Methonis

[Unknown]:
Please try replacing this:
$result = mysql_db_query($query, $connection) or
die("Error in query");

With this:
$result = mysql_db_query($query, $connection) or
die(mysql_error());

-[Unknown]

Metho:
Incorrect database name 'SELECT pid, title FROM yabbse_gallery_pictures ORDER BY pid DESC LIMIT 5' is the error...but the database is called methonis_yabbse...and the table is called yabbse_gallery_pictures...Something wrong with the syntax at all? Or are other problems is may report as incorrect database?

Methonis

Shoeb Omar:
try using mysql_query instead.  With mysql_db_query you have to specify the database name:

resource mysql_db_query ( string database, string query [, resource link_identifier ] )

Metho:

--- Code: ---function lastuploads($length)
{
mysql_connect("localhost", "myuser", "mypass") or DIE("Unable to
connect to database");
mysql_select_db("methonis_yabbse") or die("Invalid
database");
$result = mysql_query("SELECT pid, title FROM yabbse_gallery_pictures ORDER BY pid DESC LIMIT $length") or
die(mysql_error());
   if (mysql_num_rows($result) <= 0) {
      echo "No uploads...";
      return false;
   }
   $i = 1;
   echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"1\">\n";
   while ($row = mysql_fetch_array($result)) {
      echo "<tr><td style=\"white-space: nowrap;\"><a href=\"displayimage.php?album=lastup&pid={$row['pid']}\">" . truncate_string($row['title']) . "</a></td></tr>\n";
      $i++;
   }
   echo "</table>";
}

/**************************************************************************
 * Main code
 **************************************************************************/

?>
   <tr>
      <td valign="top" class="tableh1_compact" style="white-space: nowrap;"><div class="nav">
         <a href="thumbnails.php?album=lastup">
         <b>Last uploads</b></a><br></div>
      </td>
      <td valign="top" class="tableh1_compact" style="white-space: nowrap;"><div class="nav">
         <a href="thumbnails.php?album=topn">
         <b>Top pictures</b></a> /
         <a href="thumbnails.php?album=lasthits">
         <b>Last hits</b></a><br></div>
      </td>
   </tr>
   <tr>
      <td valign="top" class="tableb_compact">
         <?php lastuploads(5); ?>
      </td>
   </tr>

--- End code ---
Alright, no error now...but no data either. It just prints the stuff that's html and no data printing from the query...Also, I took all the html and link printing out and had it echo $result. It echoed this: Resource id #3Resource id #3Resource id #3Resource id #3Resource id #3. So it's getting "Resource id #3" from the query....I have no idea what that's about, as the pid's would never be the same (autoincrementing) and the titles aren't the same as well....Also, I tried removing the $length variable and just limiting the query by 5. Still doesn't show results with the html and linking, still returns 5 Resource id #3 if I simply echo them...anyone have any more help for the clueless? :(

Methonis

Navigation

[0] Message Index

[#] Next page

Go to full version