Page 1 of 3
Reading from a database
Posted: Sun Jul 26, 2009 10:12 am
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

Re: Reading from a database
Posted: Sun Jul 26, 2009 10:16 am
by jackpf
This might be useful.
Remember, google is your friend

Re: Reading from a database
Posted: Sun Jul 26, 2009 11:02 am
by Adam_C
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

Re: Reading from a database
Posted: Sun Jul 26, 2009 11:40 am
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);
}
}
Re: Reading from a database
Posted: Sun Jul 26, 2009 12:01 pm
by Adam_C
thank you so much

i didnt know it was a link lol

thanks again .
Re: Reading from a database
Posted: Sun Jul 26, 2009 12:10 pm
by jackpf
Hahaha.
Hahaha.
xD
Re: Reading from a database
Posted: Sun Jul 26, 2009 12:17 pm
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
Re: Reading from a database
Posted: Sun Jul 26, 2009 12:22 pm
by jackpf
Try putting "or die(mysql_error());" after the queries and connections.
Re: Reading from a database
Posted: Sun Jul 26, 2009 12:34 pm
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
Re: Reading from a database
Posted: Sun Jul 26, 2009 12:36 pm
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());
Re: Reading from a database
Posted: Sun Jul 26, 2009 12:48 pm
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
Re: Reading from a database
Posted: Sun Jul 26, 2009 1:00 pm
by jackpf
What's your code now?
Also, could you run var_dump() on the query before the line with the errors?

Re: Reading from a database
Posted: Sun Jul 26, 2009 1:06 pm
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

Re: Reading from a database
Posted: Sun Jul 26, 2009 1:10 pm
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].
Re: Reading from a database
Posted: Sun Jul 26, 2009 1:28 pm
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

)
http://www.selfamusingstories.elementfx.com/index.php
there are just 5 'number ones'
?
