在 Doctrine 中编写以下 mySQL 请求
首先我要说的是,我对教义一无所知,但在我的新职位上,我们到处都在使用它(不知道为什么......)。不管怎样,这是我试图将其转换为 Doctrine 语句的 php 和 mySQL 语句:
$find_vac = mysql_query("SELECT Vacancies FROM States WHERE Abbreviation = '".$state."'");
我认为让我困惑的部分是缩写是一个变量。任何帮助将不胜感激!
更新:
$res = Doctrine_Query::create()
->select('Vacancies')
->from('States')
->where('Abbreviation = ?', $state)
->execute();
$vacancies = $res[0]->getVacancies();
上面返回一个错误。
echo $res['Vacancies']."<br />";
无论选择哪个州,这都会返回数字 4(即使如此,所有州的空缺数量范围都在 0-3 之间)。
Let me preface by saying I know nothing about doctrine, but at my new position we use it all over the place (not sure why...). Either way, here's the php and mySQL statement I'm trying to turn into a Doctrine statement:
$find_vac = mysql_query("SELECT Vacancies FROM States WHERE Abbreviation = '".$state."'");
I think the part that's tripping me up is where the Abbreviation is a variable. Any help would be greatly appreciated!!!
UPDATE:
$res = Doctrine_Query::create()
->select('Vacancies')
->from('States')
->where('Abbreviation = ?', $state)
->execute();
$vacancies = $res[0]->getVacancies();
The above returns an error.
echo $res['Vacancies']."<br />";
This returns the number 4 no matter which state is selected (and even then all states range from 0-3 for the number of vacancies).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的事情应该可以做到。可以按照与准备语句相同的方式将变量插入到查询中。
编辑:这将为您提供与搜索条件匹配的数组形式的
States
数组。如果你只想获取第一个空缺的值,你可以这样获取:或者当然,你还需要检查
$res[0]
是否存在并且本身是一个数组以防使用虚假或不存在的$state
。Something like this should do it. The variables can be inserted into the query in the same way as prepared statements.
EDIT: This will give you an array of
States
in array form that match the search criteria. If you just want to get the value of the first one's vacancies, you can get it like this:Or course, you'll also want to check that
$res[0]
exists and is itself an array in case a bogus or nonexistent$state
is used.