links in mysql data

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

Post Reply
df75douglas
Forum Newbie
Posts: 6
Joined: Thu Nov 10, 2011 1:30 am

links in mysql data

Post by df75douglas »

I am trying to get the names in this table to link to another php script that will display another mysql table.

how can I turn my cells into a href ?

here is my code , any help would be appreciated.

Code: Select all

<html><head><title></title></head><body>
<?php
$db_host = 'localhost';
$db_user = 'dfuller3';
$db_pwd = 'dfuller37318';

$database = 'dfuller3';
$table = 'address_book';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

// sending query
$result = mysql_query("SELECT first_name, last_name FROM {$table} ORDER BY first_name ASC");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td><a href = "user_info.php">$cell</a></td>";

    echo "</tr>\n";
}
mysql_free_result($result);
?>

Scottayy| Please use the PHP Code syntax button when posting code in the forums.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: links in mysql data

Post by s.dot »

Code: Select all

echo "<td><a href = "user_info.php">$cell</a></td>";
Escape your double quotes and get rid of the spaces.

Code: Select all

echo "<td><a href=\"user_info.php\">$cell</a></td>";
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
df75douglas
Forum Newbie
Posts: 6
Joined: Thu Nov 10, 2011 1:30 am

Re: links in mysql data

Post by df75douglas »

thank you for your help

also I will use the php code syntax button in the future.
Post Reply