hashkash Posted December 20, 2006 Share Posted December 20, 2006 Hello!Im new to php.My problem is this.I have some user info stored in the db.Now i would like to display only the names from the userinfo table.(this i can do).I would like the clients to be able click on the names displayed so that they can view that users information.Pls help me!!Thanks!Kashyap Link to comment https://forums.phpfreaks.com/topic/31331-creating-a-link-when-i-extract-from-the-database/ Share on other sites More sharing options...
vbnullchar Posted December 20, 2006 Share Posted December 20, 2006 [code]$query="select id,name from users";$result=mysql_query($query);while($row=mysql_fetch_array($result, MYSQL_ASSOC)) {echo "<a href='showinfo.php?id=$row[id]'>".$row['name']."</a> \n";}[/code] Link to comment https://forums.phpfreaks.com/topic/31331-creating-a-link-when-i-extract-from-the-database/#findComment-144996 Share on other sites More sharing options...
hashkash Posted December 20, 2006 Author Share Posted December 20, 2006 hey thanks alot!!Could you elaborate on the showinfo.php please Link to comment https://forums.phpfreaks.com/topic/31331-creating-a-link-when-i-extract-from-the-database/#findComment-144998 Share on other sites More sharing options...
chronister Posted December 20, 2006 Share Posted December 20, 2006 The showinfo.php that vbnullchar posted is just an example. The actual page is going to be the page that you want the info to show on.replace that with the page name you want the data to show onThe ?id=$row[id] part is how you pass the record id through the url you retrieve that on your showinfo page by using the get superglobale.g. $user_id=$_GET['id'];you then have a variable called $user_id with which to add into your mysql query for that users idhope this helps Link to comment https://forums.phpfreaks.com/topic/31331-creating-a-link-when-i-extract-from-the-database/#findComment-145007 Share on other sites More sharing options...
hashkash Posted December 20, 2006 Author Share Posted December 20, 2006 thanks for the help,will try it out and let u know Link to comment https://forums.phpfreaks.com/topic/31331-creating-a-link-when-i-extract-from-the-database/#findComment-145011 Share on other sites More sharing options...
hashkash Posted December 20, 2006 Author Share Posted December 20, 2006 Im getting a warningWarning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\showinfo.php on line 7.here is the showinfo.php code[code]<?php $con=mysql_connect("localhost","root","") or die("Couldnt Connect :" . mysql_error()); mysql_select_db("joomla"); $uname=$_GET['name']; $query="select * from j_userdetails where name= '$uname' "; while ($line = mysql_fetch_array($query, MYSQL_ASSOC)) { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n"; } echo "</table>";?>[/code] Link to comment https://forums.phpfreaks.com/topic/31331-creating-a-link-when-i-extract-from-the-database/#findComment-145013 Share on other sites More sharing options...
chronister Posted December 20, 2006 Share Posted December 20, 2006 You need to change the select statement to get the record that matches the id passed in the URLinstead of [code]<?php $con=mysql_connect("localhost","root","") or die("Couldnt Connect :" . mysql_error()); mysql_select_db("joomla"); $uname=$row[name]; // this is not valid, because you have no query above it to deliver this data into this variable $query="select * from j_userdetails where name='$uname'"; while ($line = mysql_fetch_array($query, MYSQL_ASSOC)) { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n"; } echo "</table>";?>[/code]try [code]<?php $con=mysql_connect("localhost","root","") or die("Couldnt Connect :" . mysql_error()); mysql_select_db("joomla"); $user_id=$_GET['id']; //added this line to get the var passed through the url $query="select * from j_userdetails where uid='$user_id'"; // changed here to retrieve user id while ($line = mysql_fetch_assoc($query)) //shortened this line to be a little cleaner { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n"; } echo "</table>";?>[/code]you may need to change uid to whatever your actual field name for user id is. Link to comment https://forums.phpfreaks.com/topic/31331-creating-a-link-when-i-extract-from-the-database/#findComment-145014 Share on other sites More sharing options...
hashkash Posted December 20, 2006 Author Share Posted December 20, 2006 I did try it out but im still getting the warning i mentioned earlier.What could be the problem? Link to comment https://forums.phpfreaks.com/topic/31331-creating-a-link-when-i-extract-from-the-database/#findComment-145019 Share on other sites More sharing options...
chronister Posted December 20, 2006 Share Posted December 20, 2006 AHHHH I cannot believe I missed this..[code]$query="select * from j_userdetails where uid='$user_id'"; // changed here to retrieve user id$result=mysql_query($query); // your not actually running the query until you add this while ($line = mysql_fetch_assoc($result)) //shortened this line to be a little cleaner [/code]once you set up your query variable, you have to actually run that query using the mysql_query($query) method. then you have to reference the $result variable in your mysql_fetch_assoc() method like above. Link to comment https://forums.phpfreaks.com/topic/31331-creating-a-link-when-i-extract-from-the-database/#findComment-145022 Share on other sites More sharing options...
hashkash Posted December 20, 2006 Author Share Posted December 20, 2006 Thanks alot!!!haha, it was just staring right at us! Link to comment https://forums.phpfreaks.com/topic/31331-creating-a-link-when-i-extract-from-the-database/#findComment-145029 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.