|
-
May 22nd, 2006, 03:20 AM
#6
Re: How to have multiple queries in the same page
You can't just echo a query result. The query result returned by mysql_query() is a pointer to the result set. You need to use a function such as mysql_fetch_* to get a single row.
There are two things you should be doing as a matter of habit when writing your code.
- As penagate mentioned, you need to format your code properly. It is good to see that you have started embedding the PHP inside HTML
. But you also need to ensure that you are indenting your code.
This is important because it helps whoever is reading it understand the logic and flow you have employed. When using PHP inside HTML, you can use the alternate (almost VB like) tmeplating syntax with control-flow structures and loops. Again, this helps those who read your code. An example of how you might use this:
PHP Code:
?>
<table>
<tbody>
<?php while(($row = mysql_fetch_assoc($result))): ?>
<tr>
<td align="middle"><? echo $numbers; ?></td>
<td>
<a href="albums.php?albumname=<?php echo(strval( $row['artist'] ));?>">
<font size="2" face="Arial, Helvetica, sans-serif">
<b><?php echo(strval( $row['artist'] ));?></b></font>
</a>
</td>
<td align="middle"><?php echo(strval( $row['count(distinct album)']));?></td>
<td align="middle"><?php echo(strval( $row['count(album)']));?></td>
</tr>
<?php endwhile; ?>
<tr>
<td align="middle"> </td>
<td align="right"><b>Totals Albums</b></td>
<td align="middle"><b><? echo ($result_var) ?></b></td>
<td align="middle"> </td>
</tr>
</tbody>
</table>
Also, notice how all the PHP is enclosed in <?php ?> tags and not <?= ?>. This makes your code more portable as, if in future your host upgrades to a new version of PHP, they could decide to turn off the ability to use short tags.
It is of utmost importance that your code is readable, especially as you are posting it in a forum where you expect others to help you. The more readable your code the better the responses you will get.
- The second thing you need to be doing is error checking your queries. This is especially important while developing. If a query fails, you will not know until you come to use mysql_fetch_* where you will get an error saying that the variable is an invalid result resource. Error checking your queries is simple:
Code:
$query = "select artist,count(distinct album) d_album_count,count(album) album_count from musictest group by artist";
if (! ($result = mysql_query($query))) {
echo(mysql_error());
exit;
}
$query = "select count(distinct album) album_count as albums from musictest";
if (! ($result2 = mysql_query($query))) {
echo(mysql_error());
exit;
}
Now, if you have made a mistake in the query syntax, your script will print out the error and exit. Queries should never fail, so you should always exit.
Notice also how I have used column aliases, highlighted in bold. When you give the column an alias name you cna use it to refer to it in the result returned by mysql_fetch_assoc:
Code:
<td align="middle"><?php echo(strval( $row['count(distinct album)']));?></td>
// Becomes
<td align="middle"><?=strval( $row['d_album_count'] );?></td>
- Last but not least is error reporting; as demonstrated by your error above where you stated there was no output, error_reporting is not set to its maximum level. When developing, you should have error reporting set to its maximum level. This will among other things cause PHP to spit out a notice if you attempt to read a variable which you have not assigned a value to before. This will highlight possible spelling mistakes and save you a lot of time. To set error reporting to its maximum level insert the following line at the top of your script:
PHP Code:
error_reporting(E_ALL);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|