Reading from a database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Reading from a database

Post by jackpf »

jackpf wrote:You need to put "or die(mysql_error())" after all queries and connections...which you have not done...
You're still not doing that...
Adam_C
Forum Newbie
Posts: 22
Joined: Wed Jul 22, 2009 1:45 pm

Re: Reading from a database

Post by Adam_C »

i dont understand where Exactly in the code to put it?
Adam_C
Forum Newbie
Posts: 22
Joined: Wed Jul 22, 2009 1:45 pm

Re: Reading from a database

Post by Adam_C »

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);
?>
i think this is it ?

it seems to be working !!!

thank you thank you thank you !!!! :)

:) :)
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Reading from a database

Post by jackpf »

Oh yeah, I see it working now.

Btw, you don't want to use the "or die..." with mysql_fetch_array() since it will return false when the result set is empty, triggering an error that doesn't exist.

Also, I was just wondering, why do you need to do this? Don't you have all your news stories in one table?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Reading from a database

Post by Eran »

you don't want to use the "or die..."
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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Reading from a database

Post by jackpf »

No, you should use trigger_error() and error handling, but I was trying to help him resolve his errors....
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Reading from a database

Post by Eran »

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
Adam_C
Forum Newbie
Posts: 22
Joined: Wed Jul 22, 2009 1:45 pm

Re: Reading from a database

Post by Adam_C »

thank you very much both of you !! :)

but i think there one more problem to look at.

Basically, I have done the code:

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);
?>
the code is shown here: http://www.selfamusingstories.elementfx.com/index.php

it does work, but for some reason only for the table 'family_stories' rather than for these too :
friends_stories
home_stories
strangers_stories
transport
work_stories

i think it is because its the first table in my list but i am not sure.

It does select all rows in the table 'family_stories' but not all of the Tables And Rows ?

I'm sorry to keep hastling you but i really have no idea what to do ?

please help :) :)
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Reading from a database

Post by jackpf »

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
Well, with trigger_error() and set_error_handler() you can do just that. Just out of interest, how would you do it?

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?
Adam_C
Forum Newbie
Posts: 22
Joined: Wed Jul 22, 2009 1:45 pm

Re: Reading from a database

Post by Adam_C »

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?
i just removed or die() from mysql_fetch_array() but i get this error 'Unknown column 'dateadded' in 'order clause'' (without outer ' marks)

and its fine it selects 5 stories from every table, i can modify later :)

thanks again but what do i do ? :)
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Reading from a database

Post by jackpf »

Does dateadded exist in every table?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Reading from a database

Post by Eran »

Well, with trigger_error() and set_error_handler() you can do just that. Just out of interest, how would you do it?
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.
Adam_C
Forum Newbie
Posts: 22
Joined: Wed Jul 22, 2009 1:45 pm

Re: Reading from a database

Post by Adam_C »

well i feel like a di*k :P in family_stories its 'dateadded' and the rest 'date added' :O

IT WORKED !!!!!!!!!!!! :):):):):):):):):):):):):):):):):)

thanks you so much guys ive been working on this for days !!! :) :) :)
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Reading from a database

Post by jackpf »

No problem.

I think you need to learn to debug your code a bit better though.... :)
Adam_C
Forum Newbie
Posts: 22
Joined: Wed Jul 22, 2009 1:45 pm

Re: Reading from a database

Post by Adam_C »

jackpf wrote:No problem.

I think you need to learn to debug your code a bit better though.... :)
:)

how do you mean ? :)
Post Reply