如何在php中的查询中转义保留的mysql单词? [英] How to escape reserved mysql words in query in php?

查看:78
本文介绍了如何在php中的查询中转义保留的mysql单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想我要将唱片目录从专辑"更改为发行",因为无论如何这对我来说更有意义... 大错!查询突然停止工作了……研究了许多小时后,我意识到释放"是mySQL中的保留字,只有在使用正确的转义符时才能使用.在这种情况下,转义是符号`,就像这样

So i figure i was going to change my discography table name from 'album' to 'release', coz it made more sense to my anyways... Big mistake! the queries stopped working suddenly... After researching for many hours, i realize that 'release' is a reserve word in mySQL and can be use only if the correct escaping is use. In this case the escaping is the symbol `, like this

<?php
$sql = 'SELECT release.release_id, release.title, release.subtitle
        FROM `release`
        WHERE release.released_id = 567';
?>

我的问题是,这是否是在PHP中转义保留的mysql单词的正确方法?有标准化的方法吗? 谢谢

My question is if this is the correct way of escaping a reserved mysql word in PHP? Is there a normalize way of going about this? Thanks

推荐答案

要将它们转义,只需将它们放在两个反引号之间即可.像这样

to escape them just enclose them between two backticks. like this

$sql = 'SELECT `release`.release_id, `release`.title, `release`.subtitle
        FROM `release`
        WHERE `release`.released_id = 567';

为避免到处都出现反引号,只需使用这样的别名即可:

to avoid make backticks everywhere just use an alias like that:

$sql = 'SELECT r.release_id, r.title, r.subtitle
        FROM `release` as r
        WHERE r.released_id = 567';

这篇关于如何在php中的查询中转义保留的mysql单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆