EarlGrey Posted April 14, 2015 Share Posted April 14, 2015 I know very little about PHP or coding but have enjoyed learning a little here and there. I am working to connect a database table with PHP to create html pages. I am hoping to use a url that offers something like ?id=235 on the end to call the right table row and then be able to print particular cells from that table row. ie The writer's name is <?php $name ?> or whatever format that might need. So far this little script functions to pull the two rows of the table and print them. I would like to adjust the query and following code to only call the one cell of the queried table. I am hoping this is a simple question, gulp. It is far from simple to me. Can anyone offer a way to do this? Thanks very much for any help! Tom <?php $con=mysqli_connect("localhost","username","password","database"); // check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $result = mysqli_query($con,"SELECT * FROM honor"); while($row = mysqli_fetch_array($result)) { echo $row['name'] . " " . $row['title']; echo "<br>"; } mysqli_close($con); ?> Quote Link to comment Share on other sites More sharing options...
joel24 Posted April 14, 2015 Share Posted April 14, 2015 If you use the following as index.php, it will list the users from the database and link to the second file (user.php) which will show the details of the user. <?php $con=mysqli_connect("localhost","username","password","database"); // check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $result = mysqli_query($con,"SELECT * FROM honor"); while($row = mysqli_fetch_array($result)) { echo "<a href='/user.php?id={$row['id']}'>" . $row['name'] . " " . $row['title'] . "</a>"; echo "<br>"; } mysqli_close($con); ?> and then the script to show the user (i've linked it as /user.php) <?php $con=mysqli_connect("localhost","username","password","database"); // check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } if (!isset($_GET['id']) || !is_numeric($_GET['id'])) exit("You need a numeric user ID to view this page"); $id = $_GET['id']; $result = mysqli_query($con,"SELECT * FROM honor WHERE id='$id'"); $row = mysqli_fetch_assoc($result); //$row will contain all the fields for that user's row in the database echo "Name: " . $row['name'] . " " . $row['title']; echo "<br>"; mysqli_close($con); ?> Quote Link to comment Share on other sites More sharing options...
EarlGrey Posted April 14, 2015 Author Share Posted April 14, 2015 Wow! I cannot thank you enough! I loaded up the user.php file and ran it and it was able to pull the data from the DB on its own without the index. Should I also use the index file? My newb self is very happy with the user.php file that works like a charm. Thank you very much for your help with this. Tom Quote Link to comment Share on other sites More sharing options...
joel24 Posted April 15, 2015 Share Posted April 15, 2015 you can use user.php without index.php if you wish... I just put in index.php so you could create a list of the users in your database and link to that user in user.php - up to you how you use it Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.