Hi there what i'm trying to do is:

return a car manufacturer from a MySQL database and have the text returned a hyperlink to the car manufacturer website.

The below just returns the manufacturer without a hyperlink:

$row

Would i make a row in the database called 'carmakeurl', store the URL in that, then somehow echo it out.

$row


????


Thanks

Dani AI

Generated

Short practical summary and why some replies stalled

Storing the manufacturer website in its own column and outputting an HTML anchor for the car name is the right idea (as and suggested). The reason you saw a blank page is usually that the query/ fetch step was missing or PHP errors were suppressed — pointed that out. For safety and forward-compatibility, avoid the old ext/mysql functions: use PDO or MySQLi with prepared statements, validate the URL before printing it, and always HTML-escape the visible text to prevent XSS. (php.net)

A concise, safer example using PDO (replace credentials and set $id appropriately):

<?php
$pdo = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8mb4', 'dbuser', 'dbpass', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$stmt = $pdo->prepare('SELECT carname, carwebsiteurl FROM cartable WHERE id = :id');
$stmt->execute([':id' => $id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);

if ($row) {
    $url = $row['carwebsiteurl'];
    $url = filter_var($url, FILTER_VALIDATE_URL) ? $url : '#';
    echo sprintf(
        '<a href="%s" rel="noopener noreferrer" target="_blank">%s</a>',
        htmlspecialchars($url, ENT_QUOTES, 'UTF-8'),
        htmlspecialchars($row['carname'], ENT_QUOTES, 'UTF-8')
    );
}
?>

Validate URLs and debug tips

Use PHP's FILTER_VALIDATE_URL to check the stored link, but also verify the scheme is http or https (filter_var can accept odd schemes in edge cases). If you get a blank page turn on error reporting and inspect the fetched row with var_dump() to confirm the query returned data. Prefer prepared statements to avoid SQL injection and always escape any user/DB text before embedding in HTML. (php.net)

Recommended Answers

All 13 Replies

If you are trying to retrieve 1 row of a mysql table via link then it is probably best to just have a unique number/hash column and link to the php page with that row id in it. Then when retrieving the row, you can just use SELECT * FROM `table` WHERE `id`='".mysql_real_escape_string($value)."' As for how to get the unique hash, just hash the date and time the the field was entered. Alternatively you could just make the id field the manufacturer field.

this is wrong syntax

$row['<a href=&websiteurl">$title</a>']

it could be like

<a href='&websiteurl'>$row[title]</a>

if you are fetching data from db and using it as a link

<a href='&websiteurl'>$row[title]</a>

i think this is what i'm looking for, but what if i want to retrieve the websiteurl from a row in the table as well. would it be like..

<a href='$row[websiteurl]l'>$row[title]</a>

?????

Thanks

Thanks BzzBee that works, but what if i want to retrieve the url from a row as well. rather than entering the URL in php.

<a href='$row[linkurlrow]'>$row[title]</a>

??

Thanks

Thanks BzzBee that works, but what if i want to retrieve the url from a row as well. rather than entering the URL in php.

<a href='$row[linkurlrow]'>$row[title]</a>

??

Thanks

thats going to work, as long as you update the table to include a column of urls, called linkrulrow

OMG!! this is driving me mad!!!!!!!!!!

$sql = 'SELECT * FROM cartable';
$finalurl = <a href='$row[carwebsiteurl]'>$row[carname]</a>;
echo $finalurl;

I have 1 table with 2 rows in my SQL database. The rows are named:

carwebsiteurl - this contains a URL which is http://www.ford.co.uk

&

carname - this contains the text ford.


It still shows errors!!!!

Hi,

You are missing double quotes

$sql = 'SELECT * FROM cartable';
$finalurl = <a href='$row["carwebsiteurl"]'>$row[carname]</a>;
echo $finalurl;

Hi,

You are missing double quotes

$sql = 'SELECT * FROM cartable';
$finalurl = <a href='$row["carwebsiteurl"]'>$row[carname]</a>;
echo $finalurl;

You are also missing the quotes around the html code. Try the following:

$sql = 'SELECT * FROM cartable';
$finalurl = '<a href=\''.$row['carwebsiteurl'].'\'>'.$row['carname'].'</a>';
echo $finalurl;
$sql = 'SELECT * FROM cartable';$finalurl = '<a href=\''.$row['carwebsiteurl'].'\'>'.$row['carname'].'</a>';echo $finalurl;

Now it just shows a blank page, no errors though!!!!

$sql = 'SELECT * FROM cartable';$finalurl = '<a href=\''.$row.'\'>'.$row.'</a>';echo $finalurl;


Now it just shows a blank page, no errors though!!!!

Just to check, you have actually executed the query, right? And fetched the result with mysql_fetch_assoc?

ou will need to paste your code so that we can check what is the actual problem

Just to check, you have actually executed the query, right? And fetched the result with mysql_fetch_assoc?

What exactly do you mean by that? Sorry i'm a complete newbie to this stuff.

i thought you just entered the:

Select * from bla bla bla bit

then it was selected???

Thanks

First you have to connect to your mysql_database with your username and password, and select the database you want to use.

Then you create your query (the select statement) and send that query to the server. Then you get your result back, and you can fetch the values.

// Connect to the database
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

// Select the database you want to use on the server
if (!mysql_select_db('databasename', $link)) {
   die('Could not select database: ' . mysql_error());
}

// Create the query-string. Nothing is selected yet
$query = "SELECT * FROM cartable";
// Send the query-string to actually perform the query
$result = mysql_query($query, $link);
if (!$result) {
    // This happens if there was something wrong with the query
    die('Invalid query: ' . mysql_error());
}

// Iterate over each row the select returned.
// mysql_fetch_assoc returns an associative array
// http://no2.php.net/manual/en/function.mysql-fetch-assoc.php
while ($row = mysql_fetch_assoc($result)) {
    // Now you can print the hyperlink
    print("<a href=\''".$row['carwebsiteurl']."\">".$row['carname']."</a>");
}

// Close the link when you're done
// This is also done automatically when the script ends. Do not open and close database connections for every query you do. You open one global connection to the database, and let all the scripts you execute use that one.
mysql_close($link);

See http://no2.php.net/manual/en/ref.mysql.php for more information about the different mysql_* functions.

Hope this helps :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.