简体   繁体   English

根据MYSQL数据类型检查PHP变量类型

[英]Check PHP variable type against a MYSQL data type

Just wondering, if is there any class, function or ideia on how to validate a specific value/variable against a mysql data type. 只是想知道,是否有关于如何根据mysql数据类型验证特定值/变量的类,函数或想法。

We've got in PHP the is_int() is_string() is_float() etc etc... But we do not have them all. 我们已经在PHP中使用了is_int(),is_string(),is_float()等...但是我们并没有全部。 Or do we? 还是我们? Any Cheat sheet? 有备忘单吗? Any thoughts? 有什么想法吗?

EDIT: The point basically is: 编辑:重点基本上是:

  • Go trought a array of values (comming from a CSV for instance). Go遍历了一个值数组(例如,来自CSV)。
  • I know what table, and have all the column information (data type s well) (with adodb). 我知道什么表,并具有所有列信息(数据类型很好)(使用adodb)。
  • Just check if each value fits in a specific column... 只需检查每个值是否适合特定列...

If the data is coming from a CSV file, you have to remember that all the values are going to be strings (even numeric strings still have a string type). 如果数据来自CSV文件,则必须记住所有值都将是字符串(即使数字字符串仍具有字符串类型)。

So you can't use is_int() / is_float() /etc., because that only tells you about the type or the variable. 因此,您不能使用is_int() / is_float() / is_int() ,因为这只会告诉您类型或变量。 You could use is_numeric() to check the value, but this will allow for things like exponential notation like "+0123.45e6". 您可以使用is_numeric()来检查该值,但这将允许诸如“ + 0123.45e6”之类的指数表示法。 Sometimes ctype_digit() can be useful for testing integers for this reason, since it will only allow the numbers 0-9 to be present in a string for it to return true. 出于这个原因,有时ctype_digit()对于测试整数很有用,因为它只允许字符串中存在数字0-9才能返回true。

Regular expressions can also be used to identify pattern-based data types, but you do have to watch for performance overhead when dealing with large data sets. 正则表达式也可以用于识别基于模式的数据类型,但是在处理大型数据集时,您必须注意性能开销。 It's almost always advisable from a performance perspective to use the preg_ family of functions instead of the ereg functions. 从性能的角度来看,几乎总是建议使用preg_系列函数而不是ereg函数。

If you're validating things like ENUM or SET types, you'll probably need to make an array containing legal values (or extract these with a query) and then check the value against them with in_array() . 如果要验证ENUM或SET类型之类的内容,则可能需要创建一个包含合法值的数组(或使用查询提取这些值),然后使用in_array()它们与该值进行对照。

For CHAR/VARCHAR fields, you could parse the column definition and then check whether the length of the value falls within the constraints. 对于CHAR / VARCHAR字段,您可以解析列定义,然后检查值的长度是否在约束范围内。

If the NULL type is allowed on any of your columns, you'd also need to check against this (and probably map empty values or the string "NULL" to an actual NULL value). 如果您的任何列上都允许使用NULL类型,则还需要对此进行检查(可能将空值或字符串“ NULL”映射到实际的NULL值)。

If you're looking to actually escape these values properly, look into using prepared statements and the PDO (PHP Data Objects) extension. 如果您希望实际上正确地转义这些值,请考虑使用准备好的语句和PDO(PHP数据对象)扩展。 This allows MySQL to properly escape the data based on type. 这允许MySQL根据类型正确地转义数据。 (You can also use prepared statements with MySQLi.) (您也可以在MySQLi中使用准备好的语句。)

If you're looking for specific data types and how to identify them, then you might want to edit your question to facilitate more complete answers. 如果您正在寻找特定的数据类型以及如何识别它们,那么您可能需要编辑问题以提供更完整的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM