PDA

View Full Version : Reading from a Database


Adam_C
07-26-2009, 03:10 PM
I have the following code:

<?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 :)

Corrosive
07-26-2009, 04:04 PM
I have the following code:

<?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 :)


You could create a VIEW of all of the tables in your database and then query that.

Adam_C
07-26-2009, 04:06 PM
thanks for your reply, but i forgot to mention i am pretty new to this, could you guide me with your answer please? :)

thanks again

Corrosive
07-26-2009, 04:22 PM
Ah, sorry

A VIEW basically joins up your tables in the database using an index item such as 'customerid'. You can create a view in PHP my admin if that is what you are using.

Once created you can query it like it is a big table and so achieving what you want to do. You would query a view in exactly the same way as you have been querying tables. I.e.

"SELECT * FROM view ORDER BY dateadded DESC LIMIT 5"

I hope this makes sense. I haven't really got time to go through it step-by-step but the principal is what you need. You may have to do some 'wider reading' as they call it in school!

edbr
07-27-2009, 01:33 AM
why do you want to select all tables btw 8. just asking as it seems it might be superfluous maybe in you 'woder learning look at joins. that might answer your needs

Corrosive
07-27-2009, 05:58 AM
why do you want to select all tables btw 8. just asking as it seems it might be superfluous maybe in you 'woder learning look at joins. that might answer your needs

I think we are talking about the same thing Ed. You use a join (left, right etc) to create a view that you can query like a table. I used it when I had a six page questionnaire. I wanted all six pages to feed into different tables so I could use the data differently but I wanted to print back to the user what they had input. I queried the view into an array and spat the component parts back out when required.

edbr
07-27-2009, 06:19 AM
ok right but it would be simpler if it only needs 1 join, thats why i questioned what he needs it for

Corrosive
07-27-2009, 06:22 AM
That's a fair shout. :)