在 Doctrine 中编写以下 mySQL 请求

发布于 2024-11-26 14:21:21 字数 603 浏览 1 评论 0原文

首先我要说的是,我对教义一无所知,但在我的新职位上,我们到处都在使用它(不知道为什么......)。不管怎样,这是我试图将其转换为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖哒 2024-12-03 14:21:21

像这样的事情应该可以做到。可以按照与准备语句相同的方式将变量插入到查询中。

$res = Doctrine_Query::create()
    ->select('Vacancies')
    ->from('States')
    ->where('Abbreviation = ?', $state)
    ->execute();

编辑:这将为您提供与搜索条件匹配的数组形式的States数组。如果你只想获取第一个空缺的值,你可以这样获取:

$vacancies = $res[0]['Vacancies'];

或者当然,你还需要检查 $res[0] 是否存在并且本身是一个数组以防使用虚假或不存在的 $state

Something like this should do it. The variables can be inserted into the query in the same way as prepared statements.

$res = Doctrine_Query::create()
    ->select('Vacancies')
    ->from('States')
    ->where('Abbreviation = ?', $state)
    ->execute();

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:

$vacancies = $res[0]['Vacancies'];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文