News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Hmm, query is not good

Started by Metho, August 15, 2003, 11:57:04 PM

Previous topic - Next topic

Metho

Alright, probably something stupid on my part, but can anyone tell me why this doesn't work?
<?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>


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
Joshua "Methonis" Frazer
Support Specialist
The Simple Machines Team

[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
Joshua "Methonis" Frazer
Support Specialist
The Simple Machines Team

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

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>

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
Joshua "Methonis" Frazer
Support Specialist
The Simple Machines Team

Metho

Nope. Gotta be a query problem....because resourceid#3 doesn't exist anywhere in that table. I've even tried just doing something like SELECT pid FROM yabbse_gallery_pictures and trying to echo that. I just get those resource id3's repeated for as many entries as I have in that table....argh...

Methonis
Joshua "Methonis" Frazer
Support Specialist
The Simple Machines Team

Spaceman-Spiff

i think when using double quotes, you dont need to put single quotes for arrays
$row['pid'] => $row[pid]

[Unknown]

If you echo $result you will get "resource#3" because $result is just a result resource.

-[Unknown]

Metho

AHH! First, thanks [Unknown], I understand about the echo'ing $result now and how I should be echo'ing $row[title]!! Dumb of me. And then thanks Spaceman for pointing out on taking those single quotes out of the array value, I think that's what was causing it to not display anything! It's working great now. :D

Thanks everyone for your patience and help! :D

Methonis
Joshua "Methonis" Frazer
Support Specialist
The Simple Machines Team

Advertisement: