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
Post
by Adam_C » Sun Jul 26, 2009 10:12 am
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)
Adam_C
Forum Newbie
Posts: 22 Joined: Wed Jul 22, 2009 1:45 pm
Post
by Adam_C » Sun Jul 26, 2009 11:02 am
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
jackpf
DevNet Resident
Posts: 2119 Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK
Post
by jackpf » Sun Jul 26, 2009 11:40 am
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
Post
by Adam_C » Sun Jul 26, 2009 12:01 pm
thank you so much
i didnt know it was a link lol
thanks again .
jackpf
DevNet Resident
Posts: 2119 Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK
Post
by jackpf » Sun Jul 26, 2009 12:10 pm
Hahaha.
Hahaha.
xD
Adam_C
Forum Newbie
Posts: 22 Joined: Wed Jul 22, 2009 1:45 pm
Post
by Adam_C » Sun Jul 26, 2009 12:17 pm
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
jackpf
DevNet Resident
Posts: 2119 Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK
Post
by jackpf » Sun Jul 26, 2009 12:22 pm
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
Post
by Adam_C » Sun Jul 26, 2009 12:34 pm
can you say in between what lines?
as i say im very new to this and this is the first php site
thanks
jackpf
DevNet Resident
Posts: 2119 Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK
Post
by jackpf » Sun Jul 26, 2009 12:36 pm
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
Post
by Adam_C » Sun Jul 26, 2009 12:48 pm
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
jackpf
DevNet Resident
Posts: 2119 Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK
Post
by jackpf » Sun Jul 26, 2009 1:00 pm
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
Post
by Adam_C » Sun Jul 26, 2009 1:06 pm
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
jackpf
DevNet Resident
Posts: 2119 Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK
Post
by jackpf » Sun Jul 26, 2009 1:10 pm
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
Post
by Adam_C » Sun Jul 26, 2009 1:28 pm
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'
?