Page 1 of 2

MySQL Error: No Database Selected

Posted: Tue Jul 12, 2005 11:59 am
by thesimon
I am new to php, but have been getting the hang of it. That is until today, when it left me heartbroken.

I am building a photogallery to add to a site i have built. The cms component where users upload images seems to work fine.

However, the public page is supposed to work like such,

-- Page check to see if the category variable is set in the query string
-- If yes
-----Select all photo information from the photos table, where its foreign key category matches the variable value in the query string.
-----Show all photos, it sends the file path to phpThumb.php for thumbnailing.

(it has not been set up properly for this yes function yet)

-- If no
-----Select all categories (or events) and show a single thumbnailed photo from each, which is also a link to "zoom" into that category. ie Link back to itself with the category variable set.

(this should be working)

However, when i go to the page its like flipping a coin, heads it will show me the gallery and tails it will read as such:

MySQL Error: No Database Selected

There is a lot of lines of code, and different files so its very hard to know what to display here.

The page in question is http://www.keenan.net.au/hmm.php

Will you get a page, or an error? I have no idea, but if you press f5 a couple of times you will get both instances.

Thanks heaps in advance

Posted: Tue Jul 12, 2005 12:22 pm
by Burrito
would help to see some code.

do you have mysql_select_db() on your page in question?

Posted: Tue Jul 12, 2005 12:35 pm
by thesimon
Yes before each SQL query.

The page has a header.php, footer.php, itself,, and it uses the phptumb files

Posted: Tue Jul 12, 2005 12:35 pm
by Burrito
thesimon wrote:Yes before each SQL query.
you only need it once...

Posted: Tue Jul 12, 2005 12:43 pm
by thesimon
yeah i wil go through and change that, but thats not the issue, i have searched this error on google and got very little results :S

Posted: Tue Jul 12, 2005 12:46 pm
by Burrito
^^ would help to see some code ^^

Posted: Tue Jul 12, 2005 12:59 pm
by thesimon
It's messy, sorry.

Code: Select all

<?php require_once('Connections/keenandb.php'); ?>

<?php include('header.php') ?>

<?php $catp = $_GET['cat']; ?>

<?php if(isset($catp)) { ?>

<?php
mysql_select_db($database_keenandb, $keenandb);
$query_photocat = "SELECT * FROM photocat WHERE pcatid = $catp ";
$photocat = mysql_query($query_photocat, $keenandb) or die(mysql_error());
$row_photocat = mysql_fetch_assoc($photocat);
$totalRows_photocat = mysql_num_rows($photocat);
?>
<?php echo $row_photocat['title']; ?>


      <table width="100%"  border="0" cellspacing="0" cellpadding="2">
        <tr>
          <?php $counter = 0; do { ?>
          <td><table width="200"  border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td><img src="phpThumb.php?src=Files/Photo/<?php echo $row_photos['path'] ?>&w=200"></td>
              </tr>
            </table></td>
          <?php $counter = $counter + 1 ; if( $counter == $totalrows_photos) echo "</tr><tr>"; elseif($counter % 3 == 0) echo "</tr><tr>" ; ?>
          <?php } while ($row_photocat = mysql_fetch_assoc($photos)); ?>
      </table>




<?php } else { ?>

<?php

mysql_select_db($database_keenandb, $keenandb);
$query_photocat = "SELECT * FROM photocat ORDER BY title ASC";
$photocat = mysql_query($query_photocat, $keenandb) or die(mysql_error());
$row_photocat = mysql_fetch_assoc($photocat);
$totalRows_photocat = mysql_num_rows($photocat);
?>
      <p class="inthenewssummaries">copy</p>
      <table width="100%"  border="0" cellspacing="0" cellpadding="2">
        <tr>
          <?php $counter = 0; do { ?>
          <?php
			$pcatid = $row_photocat['pcatid'];
			mysql_select_db($database_keenandb, $keenandb);
			$query_photos = "SELECT * FROM photos WHERE pcatid = $pcatid ORDER BY pid ASC";
			$photos = mysql_query($query_photos, $keenandb) or die(mysql_error());
			$row_photos = mysql_fetch_assoc($photos);
			$totalRows_photos = mysql_num_rows($photos);
		?>
          <td><table width="200"  border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td><a href="hmm.php?cat=<?php echo $row_photocat['pcatid'] ?>"><img src="phpThumb.php?src=Files/Photo/<?php echo $row_photos['path'] ?>&w=200" border="0"></a></td>
              </tr>
              <tr>
                <td height="30" bgcolor="#000066"><div align="center" style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; font-weight: bold"><span style="color: #FFFFFF"><span style="color: #FFFFFF"><?php echo $row_photocat['title'] ?></span></span></div></td>
              </tr>
            </table></td>
          <?php $counter = $counter + 1 ; if( $counter == $totalrows_photocat) echo "</tr><tr>"; elseif($counter % 3 == 0) echo "</tr><tr>" ; ?>
          <?php } while ($row_photocat = mysql_fetch_assoc($photocat)); ?>
      </table>


<?php } ?>



<?php include('footer.php') ?>

Posted: Tue Jul 12, 2005 1:02 pm
by Burrito
definitely remove the mulitple instances of it.

you also might try having it throw an error if it fails:

Code: Select all

mysql_select_db($database_keenandb, $keenandb)
  or die(mysql_error());

Posted: Tue Jul 12, 2005 8:30 pm
by thesimon
Its working on my local server now, but not on the main server :(

It you have anymore ideas, please help me :?

Posted: Tue Jul 12, 2005 8:36 pm
by Burrito
was it working locally before? Are the databases named / setup exactly the same? Are you sure the user has rights to the db on the server?

I would assume the answer to all of those is yes as it is working sporadically before.

I'm fresh out of ideas, you might try calling your provider and letting them know the scoop, it could be something on their end...

Posted: Tue Jul 12, 2005 9:02 pm
by thesimon
yeah it was happening on my local server before.

Could it have something to do with the amount of time it takes phpthumb to generate the thumbs, or a cache problem

Posted: Tue Jul 12, 2005 9:27 pm
by Burrito
I've never heard of an error about something else caused by a timeout issue.

if the error says "no database selected" it's gotta be related to that.

Posted: Tue Jul 12, 2005 9:34 pm
by thesimon
Hmm, but its the same connection file i use for all pages, and they did have similar recordsets

could the problem be my if statement?

Posted: Tue Jul 12, 2005 9:41 pm
by Burrito
try putting your mysql_select_db right at the top of your page, (outside the if conditions), it's connecting to the same db so there's not sense in making it conditional.

still doesn't make sense that it works on your local machine and not the server (the if scenario).

Posted: Tue Jul 12, 2005 9:52 pm
by thesimon
wow thanks seems ok for me on the remote server, with about 15 refreshes, is it ok for you?