...

View Full Version : display mysql table



mike_m
06-11-2006, 01:33 AM
Hello. I have a MySQL table that has three columns per row ID, title, and body. How do I display it like this:



row 1:
<div>
<div>title</div>
<div>body</div>
</div>

row 2:
<div>
<div>title</div>
<div>body</div>
</div>

and so on.

lavinpj1
06-11-2006, 01:56 AM
<?php
mysql_connect('localhost', 'username', 'password');
mysql_select_db('database_name');
$result = mysql_query("SELECT `title`, `body` FROM `table_name`");
while ($row = mysql_fetch_array($result)) {
echo '
<div>
<div>' . $row['title'] . '</div>
<div>' . $row['body'] . '</div>
</div>
';
}
?>


~Phil~

mike_m
06-11-2006, 02:19 AM
thanks allot. one more question, how can I have it so when a link by the the row is clicked the row is deleted from the table?

lavinpj1
06-11-2006, 02:38 AM
<?php
if (!isset($_GET['did'])) {
mysql_connect('localhost', 'username', 'password');
mysql_select_db('database_name');
$result = mysql_query("SELECT `ID`, `title`, `body` FROM `table_name`");
while ($row = mysql_fetch_array($result)) {
echo '
<div>
<div>' . $row['title'] . '</div>
<div>' . $row['body'] . '</div>
<div><a href="' . $_SERVER['PHP_SELF'] . '?did=' . $row['ID'] . '">Delete</a></div>
</div>
';
}
}
else {
$id = mysql_real_escape_string($_GET['id']);
mysql_query("DELETE FROM `table_name` WHERE `ID`='$id'");
header("Location: " . $_SERVER['PHP_SELF']);
}
?>


~Phil~

mike_m
06-11-2006, 02:43 AM
thank you very very much.

lavinpj1
06-11-2006, 03:09 AM
welcome :)

mike_m
06-11-2006, 05:15 AM
im little confused shouldin't the delete code be like this:



if (!isset($_GET['did']))
{
$id = mysql_real_escape_string($_GET['did']);
mysql_query("DELETE FROM `test_table` WHERE `id`='$id'");
header("Location:". $_SERVER['PHP_SELF']);
}
else
{
$result = mysql_query("SELECT `id`, `title`, `body` FROM `test_table`");
while ($row = mysql_fetch_array($result))
{
echo '
<div>
<div>'.$row['title'].'</div>
<div>'.$row['body'].'</div>
<div><a href="'.$_SERVER['PHP_SELF'].'?did='.$row['id'].'">Delete</a></div>
</div>
';
}
}

lavinpj1
06-11-2006, 07:45 PM
Simple answer, no. Mine was the correct way around. Why would you do this...


if (!isset($_GET['did']))
{
$id = mysql_real_escape_string($_GET['did']);

??? You are basically saying if $_GET['did'] isn't set, set $id = to it. Therefore, $id is always = to NULL.

~Phil~

lavinpj1
06-11-2006, 07:48 PM
Although, I did make a mistake there...



<?php
mysql_connect('localhost', 'username', 'password');
mysql_select_db('database_name');

if (!isset($_GET['did'])) {
$result = mysql_query("SELECT `ID`, `title`, `body` FROM `table_name`");
while ($row = mysql_fetch_array($result)) {
echo '
<div>
<div>' . $row['title'] . '</div>
<div>' . $row['body'] . '</div>
<div><a href="' . $_SERVER['PHP_SELF'] . '?did=' . $row['ID'] . '">Delete</a></div>
</div>
';
}
}
else {
$id = mysql_real_escape_string($_GET['id']);
mysql_query("DELETE FROM `table_name` WHERE `ID`='$id'");
header("Location: " . $_SERVER['PHP_SELF']);
}
?>


You were only connecting to the database server when $_GET['did'] wasn't set.

mike_m
06-11-2006, 09:02 PM
well im doing somthing else with it. with a url like ths:

mysite.com/admin.php?id=01&action=remove
the code above was just some late night confusion:confused:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum