PHP Basics: GET vs POST, Cookies and MySQLi Explained

A php id is a unique identifier passed through a URL or form in PHP, typically accessed via $_GET['id'] or $_POST['id'] to retrieve a specific database record. GET appends the php id to the URL for safe, bookmarkable reads; POST sends it in the request body for secure writes. MySQLi then fetches the matching row from the database.

Understanding how PHP handles data transfer between pages, stores user state with cookies, and queries databases through MySQLi is the foundation every backend developer needs. Get these right and the rest of PHP clicks into place. According to the 2023 Stack Overflow Developer Survey, PHP is used by roughly 21.4% of professional developers globally, making it one of the most widely deployed server-side languages in shared hosting environments. A 2023 JetBrains Developer Ecosystem Survey found that over 75% of PHP developers use MySQLi or PDO as their primary database extension, confirming that the old mysql_* functions are effectively obsolete in production. And according to W3Techs web technology surveys, PHP powers approximately 77% of all websites with a known server-side language, underscoring why mastering the php id pattern, GET, POST, cookies, and MySQLi matters for any backend developer.

  • GET appends data to the URL; POST sends it in the request body. Use GET for safe, idempotent reads and POST for writes or sensitive data.
  • Cookies in PHP are set with setcookie() before any HTML output, or the header is already sent and the cookie fails silently.
  • strtotime() converts a human-readable date string into a Unix timestamp, making date arithmetic genuinely simple.
  • MySQLi replaces the deprecated mysql_fetch_array() function. Use mysqli_fetch_assoc() or the object-oriented equivalent instead.
  • Common PHP errors like “a php error was encountered” almost always trace back to a missing semicolon, a wrong variable scope, or a failed database connection.

GET vs POST in PHP: What Actually Differs

The difference between GET and POST in PHP is not just academic. It determines security, caching behaviour, and whether your form data survives a browser refresh. According to the W3C HTTP/1.1 specification, GET requests are idempotent, meaning the same request can be repeated without side effects, while POST requests are not.

When you visit something like localhost index php?id=5 in your local development environment, PHP reads that php id value through $_GET['id']. The value is visible in the URL, bookmarkable, and cached by browsers. That is fine for a product listing page. It is a disaster for a login form.

When to Use GET for a PHP ID

Use $_GET when the data is non-sensitive and you want the URL to be shareable. Search filters, pagination, and record lookups by php id are classic GET use cases. A URL like /products/?category=phones&page=2 is perfectly appropriate. Passing a php id such as /product/?id=42 lets users bookmark a specific product page and lets search engines index it.

GET parameters are also easy to test. You can manipulate them directly in the browser address bar, which speeds up debugging on localhost htdocs index php setups like XAMPP or WAMP, both widely used by Indian college students learning PHP locally. Indian IT training institutes in cities like Pune, Hyderabad, and Bengaluru commonly teach GET-based php id retrieval as the first database exercise.

When to Use POST

POST keeps data out of the URL, which matters for passwords, payment details, and any form submission that changes server state. PHP reads POST data through $_POST['fieldname']. The data is sent in the HTTP request body, so it does not appear in server logs or browser history by default.

One practical note: if you are setting up localhost wordpress wp login php on a local server, WordPress uses POST for its login form precisely because exposing credentials in a URL is a critical vulnerability. The same logic applies to any form you build yourself. When a php id is used to update or delete a record, always use POST or a PUT/DELETE method, never GET.

Feature GET POST
Data location URL query string Request body
Max data size ~2,048 characters (browser-dependent) Limited by post_max_size in php.ini (default 8MB)
Caching Can be cached Not cached
Bookmarkable Yes No
Security Low (visible in URL) Higher (not in URL)
Idempotent Yes No
Use case Search, filters, php id reads Login, forms, file uploads, php id writes

According to the official PHP documentation, POST data is available via the $_POST superglobal only when the form’s enctype is correctly set for file uploads (multipart/form-data). Miss that and your file upload silently fails.

Cookies, strtotime and array_merge: PHP Utility Functions You Will Use Every Day

Once you have sorted out how data moves between pages using a php id or form fields, you need to manage state and manipulate data. Cookies, strtotime(), and array_merge() cover a huge chunk of that work.

How to Define and Set Cookies in PHP

A cookie in PHP is a small piece of data stored in the user’s browser. You create one with setcookie(name, value, expiry, path). The expiry is a Unix timestamp, which is exactly where strtotime() comes in. A typical line looks like this: setcookie("user", "Priya", time() + (86400 * 30), "/");. That sets a cookie named “user” that expires in 30 days.

The critical rule: setcookie() must be called before any HTML output, including whitespace or a blank line before the opening PHP tag. If you get a “headers already sent” error, that is why. This catches almost every beginner working through localhost wordpress wp admin setup config php for the first time.

To read a cookie, use $_COOKIE['user']. To delete it, set the expiry to a time in the past: setcookie("user", "", time() - 3600, "/");. The value does not matter once the expiry is past.

What strtotime Actually Does

strtotime() parses almost any English-language date string and returns a Unix timestamp (seconds since January 1, 1970). It handles strings like "next Monday", "2025-12-31", or "+2 weeks". This makes it indispensable for calculating deadlines, scheduling, and session expiry.

A real example: echo date("d-m-Y", strtotime("+30 days")); prints the date 30 days from now in a readable Indian date format (DD-MM-YYYY). Combine it with setcookie() and you have session management without a framework.

array_merge in PHP

array_merge() combines two or more arrays into one. If arrays have the same string keys, the later value overwrites the earlier one. For numeric keys, values are re-indexed. It is commonly used to merge default settings with user-supplied options, a pattern you will see in every PHP framework.

One gotcha: if you pass non-array arguments, PHP throws a warning. Always validate inputs before merging, especially when data comes from $_GET or $_POST, including a php id value. According to PHP.net, array_merge() is distinct from the + operator, which preserves the first array’s keys rather than re-indexing.

print vs print_r in PHP

print outputs a single string and returns 1, so you can use it in expressions. print_r() outputs a human-readable representation of any variable, including arrays and objects. For debugging a nested array, print gives you “Array”. print_r() gives you the full structure.

Use print_r($variable, true) when you want to capture the output as a string instead of echoing it directly. For serious debugging, pair it with <pre> tags so the output is readable in the browser. This is one of those small habits that saves hours during development.

MySQLi: Fetching Results by PHP ID and Avoiding the mysql_fetch_array Trap

The old mysql_* extension, including mysql_fetch_array(), was removed in PHP 7.0. If you are still using it, your code will fail on any modern server. MySQLi (MySQL Improved) is the replacement, and it supports both procedural and object-oriented styles. Fetching a record by php id is one of the most common MySQLi operations you will write.

A standard MySQLi query flow looks like this. First, connect: $conn = new mysqli("localhost", "root", "", "mydb");. Then query using the php id: $result = $conn->query("SELECT * FROM users WHERE id = 1");. Then fetch: $row = $result->fetch_assoc();. That fetch_assoc() call returns one row as an associative array. Loop through multiple rows with while ($row = $result->fetch_assoc()).

Using mysqli_num_rows and mysqli_error

$result->num_rows tells you how many rows the query returned. Check it before you try to fetch data, or you will get warnings on empty result sets. If a query fails, $conn->error gives you the MySQL error string, which is far more useful than a blank page. When fetching by php id, always use prepared statements to prevent SQL injection: $stmt = $conn->prepare("SELECT * FROM users WHERE id = ?");.

Knowing how to handle mysqli_num_rows and mysqli_error properly separates functional code from fragile code. Indian developers working at companies like TCS, Infosys, and Wipro routinely maintain PHP-MySQL applications where correct MySQLi usage is a baseline expectation.

Common PHP Errors and What Triggers Them

The “a php error was encountered” message in CodeIgniter or similar frameworks is a generic wrapper. The real error sits in your PHP error log or appears when you enable display_errors = On in php.ini during development. Turn it off in production, always.

Other frequent errors include “Undefined index” (accessing a $_GET or $_POST key that does not exist, including a missing php id parameter), “Headers already sent” (outputting content before setcookie() or a redirect), and “Call to undefined function” (usually a missing extension or a typo). Each one has a clear fix once you know what it means.

If you are working with localhost wordpress wp admin setup config php and hitting database connection errors, the most common cause is a wrong database name or password in wp-config.php, not a PHP bug. Check the credentials first.

Learning these fundamentals is exactly the kind of practical skill that employers in India’s IT sector look for. If you are a student or graduate building your technical foundation, check out the top skills every college student must learn before graduation and see where PHP sits alongside other in-demand technologies.

Working professionals exploring backend development as a career pivot should also read about the best career options in 2026 for students and professionals, which covers how PHP and programming skills translate into real job opportunities. For salary benchmarks tied to backend development, the top online courses for high-salary jobs in 2025 article is worth bookmarking.

If you are still figuring out which direction to take your career, the guide on how to choose the right career path after graduation in 2026 gives a structured framework for making that decision. And if you are just starting out, the list of entry-level jobs for teens and fresh graduates shows how backend PHP skills open doors early in your career.

For free resources to supplement your PHP learning, the roundup of best free online learning platforms for students in 2025 is a solid starting point. And when you are ready to go deeper into cybersecurity, AI, or blockchain, the full 3.0 University course catalogue lists every certification programme available.

PHP fundamentals, specifically understanding the php id pattern, GET vs POST, cookies, and MySQLi, are not just beginner checkboxes. They are the building blocks for every PHP application you will build or maintain. Get them solid this week. Pick one concept from this article, implement it in a small local project on XAMPP, and push yourself to break it intentionally so you understand the errors. That is how real learning sticks.

3.0 University offers practical, industry-aligned certifications in Cybersecurity, Ethical Hacking, AI, Blockchain, and Web3. If you want structured learning with real-world projects and mentorship, explore the courses at 3.0 University and find the programme that fits where you want to go.

Frequently Asked Questions

What is a php id in PHP?

A php id is a unique identifier, usually a numeric value, passed through a URL query string or form field to identify a specific database record. It is accessed in PHP via $_GET['id'] for URL parameters or $_POST['id'] for form submissions. For example, a URL like /product/?id=42 passes a php id of 42 to the server.

How do I get an ID from a URL in PHP?

Use $_GET['id'] to retrieve a php id from a URL query string. Always validate and sanitise the value before using it in a database query. Use intval($_GET['id']) to ensure it is an integer, and use MySQLi prepared statements to prevent SQL injection when querying by php id.

What is the difference between GET and POST in PHP?

GET appends data to the URL as query parameters, making it visible, bookmarkable, and cached by browsers. POST sends data in the HTTP request body, keeping it out of the URL. Use GET for safe reads like fetching a record by php id, and POST for logins, form submissions, and any operation that changes data on the server.

How do you set a cookie in PHP?

Use setcookie("name", "value", expiry_timestamp, "/") before any HTML output. The expiry is typically time() + seconds, and strtotime() helps calculate it. Read the cookie with $_COOKIE['name']. Delete it by setting the expiry to a past timestamp. If headers are already sent, the cookie will not set, so placement matters.

What does strtotime do in PHP?

strtotime() converts a human-readable date string into a Unix timestamp. It accepts strings like "next Friday", "2025-06-01", or "+7 days". The returned integer represents seconds since January 1, 1970. It is commonly used with date() for formatting and with setcookie() for calculating expiry times.

What is the difference between print and print_r in PHP?

print outputs a single string value and returns 1, so it can be used inside expressions. print_r() outputs a structured, human-readable representation of any variable, including arrays and objects, making it ideal for debugging. Pass true as the second argument to print_r() to return the output as a string instead of printing it.

How do you fetch MySQLi results in PHP?

Connect with new mysqli(), run a query with $conn->query(), then fetch rows using $result->fetch_assoc() for associative arrays or $result->fetch_row() for numeric arrays. Use a while loop to iterate multiple rows. Check $result->num_rows before fetching to avoid errors on empty results. The old mysql_fetch_array() is removed in PHP 7 and above.

Last updated: September 2026. Reviewed by the 3University editorial team.

We create, review, and maintain educational content covering Cybersecurity, Ethical Hacking, Artificial Intelligence, Blockchain, Web3, and emerging technologies.

Read More Articles