Jump to content

php/coding newb seeks help to adjust a small script


EarlGrey

Recommended Posts

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);
?>

 

Link to comment
Share on other sites

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);
?>
Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.