Re: Reading from a database
Posted: Sun Jul 26, 2009 2:14 pm
You're still not doing that...jackpf wrote:You need to put "or die(mysql_error())" after all queries and connections...which you have not done...
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
You're still not doing that...jackpf wrote:You need to put "or die(mysql_error())" after all queries and connections...which you have not done...
Code: Select all
$sql = mysql_query("SHOW TABLES;")or die(mysql_error());
while($fetch = mysql_fetch_array($sql)or die(mysql_error()))
{
$sql2 = mysql_query("SELECT * FROM $fetch[0] ORDER BY dateadded DESC LIMIT 5")or die(mysql_error());
while($row = mysql_fetch_array($sql2)or die(mysql_error()))
{
echo "<div id= \"latest10\">". "<li>". "<h3 class=\"h3cap\">". "<span>". $row['first']. " ". $row['last']. "</span>". "<span style=\"float:right;\">". " [". $row['id']. "]". "</span>". "</h3>". "<p class=\"paragraph\">". $row['story']. "</p>". "</br>". "<p style=\"text-align:right;font-size:11px;\">". "Added: ". $row['dateadded']. "</p>". "</li>". "</div>";
}
}
mysql_close($con);
?>Actually, you don't want to use die() as a error handler in a real application, ever. Unless this is some exercise script, I would strongly advise against it.you don't want to use the "or die..."
Code: Select all
<?php
$con = mysql_connect("localhost","user","pwd");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname", $con);
$sql = mysql_query("SHOW TABLES;")or die(mysql_error());
while($fetch = mysql_fetch_array($sql)or die(mysql_error()))
{
$sql2 = mysql_query("select * from " . $fetch[0] . " ORDER BY dateadded DESC LIMIT 5")or die(mysql_error());
while($row = mysql_fetch_array($sql2)or die(mysql_error()))
{
echo "<div id= \"latest10\">". "<li>". "<h3 class=\"h3cap\">". "<span>". $row['first']. " ". $row['last']. "</span>". "<span style=\"float:right;font-size:14px;\">". " [". $row['id']. "]". "</span>". "</h3>". "<p class=\"paragraph\">". $row['story']. "</p>". "</br>". "<p style=\"text-align:right;font-size:10px;\">". "Added: ". $row['dateadded']. "</p>". "</li>". "</div>";
}
}
mysql_close($con);
?>Well, with trigger_error() and set_error_handler() you can do just that. Just out of interest, how would you do it?pytrin wrote:Yeah, I know - I'm making sure he doesn't make this common practice. you probably don't want to trigger an error either, better to log silently and show an error page instead. Better than a blank screen or an internal error message
i just removed or die() from mysql_fetch_array() but i get this error 'Unknown column 'dateadded' in 'order clause'' (without outer ' marks)And did you remove the or die() from mysql_fetch_array()? Because looking at your source code and the way it ends abruptly, to me it suggests that you haven't.
Also, you do realise that it's not going to select 5 stories, rather 5 stories from every table right?
Those functions (mysql_connect,mysql_query) don't actually throw an error. You can simply test to see if they return false and act appropriately.Well, with trigger_error() and set_error_handler() you can do just that. Just out of interest, how would you do it?
jackpf wrote:No problem.
I think you need to learn to debug your code a bit better though....