0

I am trying to make the results from mysql results clickable links in php. I am new to php and please help.

<?php mysql_connect("localhost", "root", "pass") or die(mysql_error());
mysql_select_db("Branches") or die(mysql_error());
$data = mysql_query("SELECT * FROM items order by ID desc Limit 5") or die(mysql_error());
while ($info = mysql_fetch_array($data)) {
    echo $info['title'];
    echo " <br>";
    echo $info['descr'];
    echo "<br>";
    echo "<br>";
} ?>
2
  • you can do something like echo '<a href="'.$info['descr'].'">bla link</a>'; whats the problem ? Commented Dec 21, 2012 at 5:57
  • Seems like, you wanna show data from database on link click. ?? Commented Dec 21, 2012 at 5:57

3 Answers 3

1
while ($info = mysql_fetch_array($data)) {
    echo '<a href="somefile.php?id='.$info['ID'].'">'.$info['title'].'</a>';
    echo " <br>";
    echo $info['descr'];
    echo "<br>";
    echo "<br>";
}

Then in somefile.php, use $_GET to capture the id and show the results

$id = $_GET['id'];
// pull info from db based on $id
$sql = mysql_query('SELECT * FROM items WHERE ID = "'.$id.'"');
.
.
.
.
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Asprin, The clickable link worked briliantly but I am now strugling with pulling a field 'details' from the items table corresponding with the 'id'. I tried the following code but it not working, where am I going wrong? '<?php $id = $_GET['id']; // pull info from db based on $id $sql = "SELECT * FROM items WHERE ID = '".$id."'"; while ($info = mysql_fetch_array($sql)){ echo $info ['Title']; echo $info ['Details']; " <br>"; }?>'
The message from the browser is: Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\xampp\htdocs\news\article.php on line 7
@Ucz that error - Warning: mysql_fetch_array() expects parameter 1 to be resource ... is caused because you are using $sql which is your sql string without mysql_query(). Change it to - $sql = mysql_query("SELECT * FROM items WHERE ID = '".$id."'");
Yep! @Sean got it spot on. Wonder how I missed that
Thanks guys, you r life servers. Works perfectly.
0

just use anchor tag like this

while ($info = mysql_fetch_array($data)) {
    echo "<a href =\"$info[title]\">".$info['title'];. "</a>";
}

1 Comment

let op replay my comment first ..i am telling same ... question is vague
0

echo '<a href="php page which you want to display on click">$info['descr']</a>'

1 Comment

let op replay my comment first ..i am telling same ... question is vague

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.