I'm trying to grab a value from a mySQL database and put that value on the end of a url path

e.g. http://url.com/$valuefromdb

Is there any tutorials to how to do this or can anybody recommend how to tackle this?

Kind Regards


JayJ

Dani AI

Generated

The root problems here are: the query result was never checked before calling fetch, multiple separate SELECTs were used when one will do, and output was not escaped or URL-encoded. saw the fetch warning because the query returned false; was on the right track about fetching and looping, and attempted a fix but ran three separate queries and printed unquoted HTML — all fragile patterns. The long-term fix is to use a single safe query, check for errors, URL-encode the filename for a path segment, and HTML-escape the link text.

Example using PDO (safer than the old mysql extension):

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

$stmt = $pdo->query('SELECT log_filename, log_issue, log_month FROM uploads_log');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  $path = rawurlencode($row['log_filename']);
  $label = htmlspecialchars('Issue ' . $row['log_issue'] . ', ' . $row['log_month'], ENT_QUOTES, 'UTF-8');
  echo '<a href="https://example.com/uploads/' . $path . '">' . $label . '</a>';
}

Troubleshooting tips: if you see "supplied argument is not a valid MySQL result resource", the SQL failed — check the exact error (use exceptions or mysqlerror during debugging) and verify the connection and table/column names. Prefer prepared statements when inserting dynamic input into queries. Use rawurlencode for path segments (not urlencode), and always wrap attribute values in quotes and escape output with htmlspecialchars to avoid XSS. The old `mysql*` functions are deprecated/removed; migrate to PDO or mysqli (PDO docs, mysqli docs). For encoding and escaping see rawurlencode and htmlspecialchars.

Recommended Answers

All 4 Replies

I've tried various things now to attempt to get this to work - all to no avail.

My current is

// Define variables
$filename = mysql_query("SELECT log_filename FROM uploads_log");
$issueno = mysql_query("SELECT log_issue FROM uploads_log");
$issuemonth = mysql_query("SELECT log_month FROM uploads_log");

print "<a href=www.url/uploads/$filename><?php echo Issue $issueno.", ".$issuemonth; ?></a>";
<?php

$xconn =mysql_pconnect("localhost","username","userpassword") or die("unable to connect to dbengine".mysql_error());// assuming a connection to mysql db
$qry = "select field from table" ; //your sql statement


$exec_qry = mysql_query($qry) or die ("query failed".mysql_query());

$pulling =  mysql_fetch_array( $exec_qry,MYSQL_ASSOC);

foreach($pulling as $valuefromdb);
printf('%s','<a href="targetlink.ext". "?parameter= ". $valuefromdb . " '  ) ;

mysql_close($xconn);

?>

I hope this helps, Goodluck.

Thanks.

I've tried that and got the following errors:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in table2.php on line 7

Warning: Invalid argument supplied for foreach() in table2.php on line 9

Hi...
Try this....

<?php

// Define variables
$result1 = mysql_query("SELECT log_filename FROM uploads_log");
$filename = mysql_fetch_array($result1);
$result2 = mysql_query("SELECT log_issue FROM uploads_log");
$issueno = mysql_fetch_array($result2);
$result3 = mysql_query("SELECT log_month FROM uploads_log");
$issuemonth = mysql_fetch_array($result3);

print "<a href='www.url/uploads/".$filename."'>Issue ".$issueno.", ".$issuemonth."</a>"; 

?>
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.