开发者

Help retrieving information from php

开发者 https://www.devze.com 2023-03-25 02:09 出处:网络
I\'m building a website where users can view eachother\'s profile. When a person clicks on another user\'s name they are directed to their pro开发者_运维问答file page. The URL would look as follows:

I'm building a website where users can view eachother's profile. When a person clicks on another user's name they are directed to their pro开发者_运维问答file page. The URL would look as follows:

http://www.mywebsite.com/profile.php?id=21

In ASP.NET this was trivial to do since I could call controls from C# and edit their text after I've retrieved user information from the database, but i can't seem to find a way how to do the same thing using PHP and jquery. This is how I would like the procedure to go:

  • User A clicks name of User B
  • User A is redirected to profile page of User B
  • Server retrieves information about User B and sends it to jquery
  • Page is loaded and the HTML fields are filled with the variable contents which were just sent from the server

I guess what i'm finding most hard is how to pass information from php to jquery within the same page.


In PHP you can mix server side code with HTML. You don't really need to involve jQuery to fill the HTML fields (unless you're using AJAX).

You would execute your mysql query and then use that result set to populate the page like so:

This isn't the best code (from w3Schools) but it illustrates the point:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons");

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

Notice the echo statements. This will output the value from the database into your HTML page.

As I said this is not an example of good code but you can easily change this to add the parameter from the query string (remember to escape the string) to the select statement and output into a profile page.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号