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

Adam_C
Forum Newbie
Posts: 22
Joined: Wed Jul 22, 2009 1:45 pm

Reading from a database

Post by Adam_C »

I have the following code:

Code: Select all

<?php
$con = mysql_connect("localhost","user","passw");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("dbname", $con);
 
$result = mysql_query("SELECT * FROM Tab ORDER BY dateadded DESC LIMIT 5");
 
while($row = mysql_fetch_array($result))
  {
  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);
?>
What i want to do then is select all of the tables in the database and then select everything that is in all of those tables.

This works if i only specify one table rather than trying to specify them all.

Any ideas?

thanks :)
Last edited by Weirdan on Sun Jul 26, 2009 10:17 am, edited 1 time in total.
Reason: corrected code tags (changed to php)
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 »

This might be useful.

Remember, google is your friend :P
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:This might be useful.

Remember, google is your friend :P

I have searched but i haven't long started learning and am not really sure what is right?

could you please help me with my current code?

thanks :)
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 »

Well, that link gives you the exact code.

Here's my interpretation:

Code: Select all

$sql = mysql_query("SHOW TABLES;");
 
while($fetch = mysql_fetch_array($sql))
{
    $sql2 = mysql_query("SELECT * FROM `".$fetch[0]."`;");
    
    while($fetch2 = mysql_fetch_array($sql2))
    {
        print_r($fetch2);
    }
}
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 so much :) i didnt know it was a link lol :P thanks again .
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 »

Hahaha.



Hahaha.


xD
Adam_C
Forum Newbie
Posts: 22
Joined: Wed Jul 22, 2009 1:45 pm

Re: Reading from a database

Post by Adam_C »

sorry to annoy you but it isn't working when i try putting the div 'type thing' into the code ?

am i overlooking something - by the way the site i am wokring on is here: http://www.selfamusingstories.elementfx.com/

could you please look at my code so far and find out what is wrong with it as i have no idea :S

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;");
 
while($fetch = mysql_fetch_array($sql))
{
    $sql2 = mysql_query("SELECT * FROM ORDER BY dateadded DESC LIMIT 5");
   
    while($row = mysql_fetch_array($sql2))
{
  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);
?>
lol, thanks again :)

Adam
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 »

Try putting "or die(mysql_error());" after the queries and connections.
Adam_C
Forum Newbie
Posts: 22
Joined: Wed Jul 22, 2009 1:45 pm

Re: Reading from a database

Post by Adam_C »

can you say in between what lines? :) as i say im very new to this and this is the first php site :)

thanks
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 »

After mysql_query() and all mysql functions. If they fail, they return false, and mysql_error() will display the last error. Using "or" will allow you to display the error if the query fails.

Like this:

Code: Select all

mysql_query("query...") or die(mysql_error());
Adam_C
Forum Newbie
Posts: 22
Joined: Wed Jul 22, 2009 1:45 pm

Re: Reading from a database

Post by Adam_C »

i have tried doing what you said and put it on line 35, this error came up as before: 'Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/adamc/public_html/index.php on line 41' (without quotes) :S
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 »

What's your code now?

Also, could you run var_dump() on the query before the line with the errors? :)
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

<?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))
{
    $sql2 = mysql_query("SELECT * FROM ORDER BY dateadded DESC LIMIT 5");
   
    while($row = mysql_fetch_array($sql2))
{
  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);
?>
also could you state where i should put the fore mentioned code :)
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 »

You need to put "or die(mysql_error())" after all queries and connections...which you have not done...

In $sql2, you're not selecting from any table. I assume you want to select from $fetch[0].
Adam_C
Forum Newbie
Posts: 22
Joined: Wed Jul 22, 2009 1:45 pm

Re: Reading from a database

Post by Adam_C »

Here is my new 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;");
 
while($fetch = mysql_fetch_array($sql))
{
    $sql2 = mysql_query("SELECT * FROM $fetch[0] ORDER BY dateadded DESC LIMIT 5");
 
    while($row = mysql_fetch_array($sql2))
{
  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>" or die(mysql_error());
  }
     }
     
mysql_close($con);
?>
Is this right?

and here is what it comes out like (which is wrong :P) http://www.selfamusingstories.elementfx.com/index.php

there are just 5 'number ones'

? :)
Post Reply