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 9215715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:15:54+00:00 2026-06-18T02:15:54+00:00

I have database tables in MySQL that have a primary keys defined as follows:

  • 0

I have database tables in MySQL that have a primary keys defined as follows:

`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY (`id`)

I have already created models for the tables. I’m using Zend Framework v1, without an ORM, but this question should apply for all model implementations.

Now I want to create a function that allows me to grab by id:

public static function getById ($id) {

    $id = intval($id);

    if ($id < 1) {
        throw new Exception('bad id');
    }

    // business logic and stuff

    return $stuff;
}

The reason for this approach was twofold. Firstly, form input, including numbers, is often read from the view as a string. Secondly, invalid strings (such as 'asdf') would become zero after casting, thereby causing the exception to fire. However, there are many invalid strings that may still bypass the check:

echo intval('1asdf');

Output:

1

Other approaches I have tried include:

if (intval($id) === 0)

if ($id == intval($id))

What is the best way to validate the id in the model? I want it to accept integers greater than 0, and strings that are valid positive integers.

I would prefer something with a very low time cost. This is the reason I am not using regex to validate. These calls will be used everywhere in the application, and I cannot afford the overhead.

  • 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-18T02:15:56+00:00Added an answer on June 18, 2026 at 2:15 am

    I cannot understand why you are going through so much trouble just to validate your input. Simply use the php functions is_numeric() and htmlentities() to check your input before passing it to your SQL logic. Moreover an Invalid Numeric ID (which isnt present in your database) will return null output when SQL against it is executed and that should’nt be an issue.

    References:

    is_numeric()

    htmlentites()

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

Sidebar

Related Questions

Consider a database(MSSQL 2005) that consists of 100+ tables which have primary keys defined
I have a MySQL database with 60 tables. Most of the tables have primary
I have a MySQL database table called Participant that looks something like this: (idParticipant)
I have a MySQL database, and a particular table in that database will need
I'm trying to normalize a mysql database.... I currently have a table that contains
Suppose I am building an app using current mysql database tables. I have this
I have a MySQL database with 3 tables: articles, tags, and articles_tags The schema
I have a MySQL database with 3 tables: **articles:** id - articletitle - articleorganization
I have a MySQL database with multiple tables, each of which have the same
Should a database table that contains two columns that are foreign keys have a

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.