Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8102293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:12:29+00:00 2026-06-05T23:12:29+00:00

Say I’ve got a mySQL database table with 4 fields: link_id (primary key) page_id

  • 0

Say I’ve got a mySQL database table with 4 fields:

  • link_id (primary key)
  • page_id
  • anchor_text
  • url

My data looks like this:

link_id | page_id | anchor_text | url 
1       | 1       | Link One    | http://www.one.com
2       | 1       | Link Two    | http://www.two.com
3       | 2       | Link Three  | http://www.three.com

How would I best write a function to get the links for a given page and then use that function to display them?

Function:

    function get_page_links($page_id) {

        $db = new mysqli("localhost", "root", "root", "my_db");

        //what's next?

        }

Usage:

$my_links = get_page_links(1);

//do something to parse $my_links

Display:

<a href="http://www.one.com">Link One</a>
<a href="http://www.two.com">Link Two</a>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:12:30+00:00Added an answer on June 5, 2026 at 11:12 pm
    $q = "SELECT *";
    $q.= " FROM yourtablename";
    $q.= " WHERE `page-id` = ".(int)$page_id;
    $q.= " ORDER BY `link-id`";
    

    The backticks might not be necessary, but I’m throwing them in there since you have hyphens in your field names.

    Note that there is very basic validation going on here. By casting $page_id to an int before appending it, you ensure it won’t be some sort of injection attack. This isn’t a great way to do it, but it will work.

    Something like mysqli_real_escape_string() is an alternative that should be considered, especially for more general sanitization.

    Alternatively:

    $q = sprintf("SELECT *
                  FROM yourtablename
                  WHERE `page-id` = %d
                  ORDER BY `link-id", $page_id);
    

    Which I like better.


    Edit re: now what?

    First off, lets not use mysqli, lets use PDO.

    Second, we don’t want to connect to the database on each call of the function, we want to do this once. So move this out of the function.

    // Typically this line is in another file and included once, but for now lets just
    //  get this out of the function
    $db = new PDO('mysql:host=localhost;dbname=my_db', 'root', 'root');
    
    // Your function
    function get_page_links($page_id) {
        // Build query
        $q = sprintf("SELECT *
                      FROM yourtablename
                      WHERE `page-id` = %d
                      ORDER BY `link-id`", $page_id);
        // Run Query
        foreach ($db->query($q) as $a) {
            printf('<a href="%s">%s</a>'."\n", $a['url'], $a['anchor-text']);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a Student table, it's got an int ID. I have a
Say I have a mysql table of names and one column has the following
Say I have a select box eg <div data-bind='visible: someProp'> <select class=selectSubWidgets data-bind='options: subWidgets,optionsText:
Say i have a few fields like the following: abd738927 jaksm234234 hfk342 ndma0834 jon99322
Say I have three tables, a table of users, a table of around 500
say I have input data like so: firstName | lastName | Country Bob |
Say I have a higher kinded type SuperMap[Key[_],Value[_]]`. Suppose now that I had something
Say, the field of Race in my table has values like W,A (white and
Say we have the following simple data-frame of date-value pairs, where some dates are
Say you have an application divided into 3-tiers: GUI, business logic, and data access.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.