search
HomeBackend DevelopmentPHP Tutorialthinkphp插入数据库操作错误

thinkphp MySQL 数据库

thinkphp从excel读取数据插入mysql,生成sql语句正确,单独能执行,用
$model->execute($sql),执行时,凡是字符串都只插入了首字符。生成sql语句是这样 insert into table values('','ABCDEF'),没有列举完,只是举例,单独执行插入成功,在thinkphp里面执行,字符串ABCDEF只插入了一个A。在tp里面打印出最后执行的sql语句也是对的

回复讨论(解决方案)

直接执行也是同样的结果吗?   我本来还以为会是数据库字段类型和长度限制了   要是直接执行也这样  那得追踪到TP的最终mysql_query的地方检察mysql_query之前的$sql

直接执行也是同样的结果吗?   我本来还以为会是数据库字段类型和长度限制了   要是直接执行也这样  那得追踪到TP的最终mysql_query的地方检察mysql_query之前的$sql



能说详细一些吗?谢谢你


直接执行也是同样的结果吗?   我本来还以为会是数据库字段类型和长度限制了   要是直接执行也这样  那得追踪到TP的最终mysql_query的地方检察mysql_query之前的$sql
能说详细一些吗?谢谢你

其实是你这边应该描述的详细一点

没有无解的"灵异问题" 一定是哪里有问题

依我浅见,或者是TP对SQL语句的处理出了问题,或者是mysql的数据结构/类型有问题. 没能想出其他可能

所以先试试手动写SQL语句直接在mysql执行, 看看结果,你现在说插入成功,插入后的结果呢?也是想要的吗?

如果手动插入结果正确 那就在TP框架内顺藤摸瓜找到TP插入mysql的语句 将此时的SQL语句打印出来 看跟你手写的SQL语句是否一致.

然后才好进一步看....


当然,可能有大神遇到过这种问题能一下指出原因 不过在此之前可以先试试这样找找原因

其实是你这边应该描述的详细一点

$res = $order_insert->execute($sql);
echo $order_insert->getLastSql();
在执行execute的时候,$sql是正常的,复制出来单独执行,结果也是想要的。 当执行完,用$order_insert->getLastSql();获取最后一条执行的sql语句,sql语句也是正常的,对这个sql字符串直接复制到execute执行也是能正常插入数据,就是当做参数执行时,出现上面说的情况

你怎么不在当做参数执行时,也看一下 $order_insert->getLastSql() 呢 其实是你这边应该描述的详细一点

$res = $order_insert->execute($sql);
echo $order_insert->getLastSql();
在执行execute的时候,$sql是正常的,复制出来单独执行,结果也是想要的。 当执行完,用$order_insert->getLastSql();获取最后一条执行的sql语句,sql语句也是正常的,对这个sql字符串直接复制到execute执行也是能正常插入数据,就是当做参数执行时,出现上面说的情况


你怎么不在当做参数执行时,也看一下 $order_insert->getLastSql() 呢

看了的,就是把组装出来的sql语句当做参数给execute,然后执行后,再获取最后一条执行的sql语句,打印出来是完整的sql语句  insert into table value('','ABCDEF');这样的语句,但是数据库的表里面对应ABCDEF这个字符只插入了一个字符A

这个会不会是数据库的字段问题啊,直接把sql在mysql执行还会这样?

这个会不会是数据库的字段问题啊,直接把sql在mysql执行还会这样?

不是字段问题哈,直接执行插入是正常的!

已经找到问题,是读取excel的时候,要加一句setOutputEncoding

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools