Jump to content

Hyperlinks and MySQLi


MNSarahG

Recommended Posts

Hi,

 

I'm using PHP5 with MySQLi, which has proven to be kind of a challenge for my first PHP project.  I'm having a hard time finding good resources of example code and stuff.  So if anybody knows of any good, beginner-friendly information out there, please share!

 

That said, I'm having trouble with a seemingly simple issue and I can't really research my way out of it.

 

I've outputted a list of names from my DB, and I want each name to be a hyperlink that carries an employee ID # over to another page.  My script looks like this:

 

                 
  <?php

$link = mysqli_connect(
            'localhost',  
            'user',      
            'pw',   
            'db');     

if (!$link) {
   printf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error());
   exit;
}

if ($result = mysqli_query($link, 'SELECT employeeID, firstName, lastName FROM employees ORDER BY lastName')) {

?>
<a href=/profile.php?employeeID=$employeeID>

<?php 
   
/* Fetch the results of the query */
    while( $row = mysqli_fetch_assoc($result) ){
        printf("%s, %s</a><br><br>\n", $row['lastName'], $row['firstName']);
    }?>

<?php	
    mysqli_free_result($result);
}

mysqli_close($link);
?> 			

 

This outputs the list of names just fine, but only the first one is a link, and the employee ID variable doesn't make it into the link (just $employeeID).  I think this is some sort of issue with my syntax.  Thanks in advance for any help - I'm really liking PHP and am excited to be able to actually make things that work on my own!

 

- Sarah -

 

Link to comment
Share on other sites

I fixed part of it!!  Everybody was probably really concerned.  But I'm still struggling with getting the employee ID for each record to show up in the hyperlink.  Again, help would be awesome.  Here's what I've got for my query/output:

 

 
if ($result = mysqli_query($link, 'SELECT employeeID, firstName, lastName FROM employees ORDER BY lastName')) {

while( $row = mysqli_fetch_assoc($result) ){
        printf("<a href=/profile.php?employeeID=$employeeID>
%s, %s</a><br><br>\n", $row['lastName'], $row['firstName']);
    }

 

Right now, each name links to "/profile.php?employeeID=" which is close...

 

- Sarah -

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.