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

  • Home
  • SEARCH
  • 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 1051955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:00:58+00:00 2026-05-16T17:00:58+00:00

I am learning PHP and SQL, and I’m trying to figure out how to

  • 0

I am learning PHP and SQL, and I’m trying to figure out how to select a record from a database.
I created a function called selectById()

Right now in the browser displayed is “Error:” but, no specific error was displayed.

 // function selectById --------------------------------------------------------------------
    function selectById($pUInput) {
        $sql = mysql_query("SELECT * FROM tblStudents 
                            WHERE id='$pUInput[0]'");

            if (!mysql_query($sql))
              {
              die('Error: ' . mysql_error());
              }
            echo "Record Selected";

    }

PHP Code:

//Call function mainline

mainline();

// Declare the function mainline
function mainline() {

    $uInput = getUserInput();

    $connectDb = openConnect(); // Open Database Connection
    selectDb($connectDb); // Select Database
    doAction($uInput);
    //display();
    //closeConnect();

}

//Declare function getUserInput --------------------------------------------------------
function getUserInput() {

    echo "In the function getUserInput()" . "<br/>";

    // Variables of User Input
    $idnum = $_POST["idnum"];              // id (NOTE: auto increments in database)
    $fname = $_POST["fname"];             // first name
    $lname = $_POST["lname"];            // last name
    $major = $_POST["major"];           // major
    $year = $_POST["year"];            // year
    $action = $_POST["action"];       // action (select, insert, update, delete)

    $userInput = array($idnum, $fname, $lname, $major, $year, $action);

    return $userInput;
}

function doAction($pUserInput) {
    echo "In function doAction()" . "<br/>";

    if ($pUserInput[5] == "sel") {
        selectById($pUserInput);    

    } elseif ($pUserInput[5] == "ins") {
        insert($pUserInput);    
    }

}

// Create a database connection --------------------------------------------------------
function openConnect() {
    $connection = mysql_connect("localhost", "root_user", "password");
        echo "Opened Connection!" . "<br/>";

    if(!$connection) {
        die("Database connection failed: " . mysql_error());
    }

    return $connection;
}

// Select a database to ---------------------------------------------------------------- 
function selectDb($pConnectDb) {
    $dbSelect = mysql_select_db("School", $pConnectDb);
    if(!$dbSelect) {
        die("Database selection failed: " . mysql_error());
    } else {
    echo "You are in the School database! <br/>";   
    }

}

// function select ---------------------------------------------------------------------
function selectById($pUInput) {
    $sql = mysql_query("SELECT * FROM tblStudents 
                        WHERE id='$pUInput[0]'");

        if (!mysql_query($sql))
          {
          die('Error: ' . mysql_error());
          }
        echo "Record Selected";

}

// function insert -----------------------------------------------------------------------------
function insert($pUInput) {

    $sql="INSERT INTO tblStudents (first_name, last_name, major, year)
          VALUES
         ('$pUInput[1]','$pUInput[2]','$pUInput[3]', '$pUInput[4]')";


        if (!mysql_query($sql))
          {
          die('Error: ' . mysql_error());
          }
        echo "1 record added";
}



?> 

SQL Syntax:

CREATE TABLE `tblStudents` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(30) NOT NULL,
  `last_name` varchar(50) NOT NULL,
  `major` varchar(40) NOT NULL,
  `year` date NOT NULL,
  PRIMARY KEY (`id`)
)
  • 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-05-16T17:00:59+00:00Added an answer on May 16, 2026 at 5:00 pm

    You are running a query on a query result. This will not work. You will need to use something along the lines of

    $sql = mysql_query("SELECT * FROM tblStudents 
                            WHERE id='" . $pUInput[0] . "'");
    
    if (!$row = mysql_fetch_assoc($sql))
    

    Which would assign $row an array value if the query did not fail. You may also want to filter the pUinput as well with mysql_real_escape_string as you do not necessarily know what it contains (or statically cast it to an integer).

    EDIT

    Added a bit extra information.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Learning php! I have a sql database which contains a table called 'images' which
I am learning php, trying to use the fopen() function. The php file I
I am learning php and html. I am trying to make a function run
Ok I am learning php and trying out classes and functions. Currently I have
I'm trying to connect to SQL Server using PHP, I'm learning this language. I
I'm interested in learning more about design practices in PHP for Database Abstraction &
I've been learning some PHP and MySQL from a book that teaches you how
I'm learning how to sanitize my forms and know that using the PHP function
What I'm trying to do is run the same SQL select on many Oracle
I've been learning Php/SQL/JSON for just over 24 hours now and its gone pretty

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.