Zend Framework中自动引用MySQL查询疑惑

发布于 2024-08-11 07:32:04 字数 1285 浏览 2 评论 0原文

我对在 Zend 框架中引用 mysql 查询没有什么疑问。虽然这个问题对我有一些帮助,但有些事情仍然令人困惑:

1)$表是 Zend_Db_Table。尝试从表中获取一行。

$where[] = $db->quoteInto('id = ?', $id);
$where[] = $db->quoteInto('user_id = ?', $user_id);
$row = $table->fetchRow($where);

我需要在这里使用 quoteInto 还是会在 fetchRow 中自动引用它?对于这种查询,更好的方法是什么?如果需要的话,可以将 2 个 quoteInto 合并为一个吗?

2) $table 是 Zend_Db_Table。尝试在表中保存一行。

$tablerow = $table->createRow();
$rowdata = array('id' => $id, 'user_id' => $user_id);
$tablerow->setFromArray($rowdata);
$ret = $tablerow->save();

我需要在这里使用某种报价功能吗?

3) $table 是 Zend_Db_Table。尝试更新表中的一行。

$row = $table->fetchRow($where);
$row->name = $name; 
$row->save();

我需要在第二步中引用 $name 吗?

4) 一般查询

A)

$sql = "SELECT * FROM users where id=? and name=?";
$results = $db->fetchAll($sql, array($id, $name));

我需要在这里报价吗?
B)

$sql =  "SELECT * FROM users where id=? and name=?";
$stmt = $db->query($sql, array($id, $name)); 
$result = $stmt->fetchAll();

我需要在这里引用吗?

C) 对于一般查询,A 和 B 哪个更好?

I've got few doubts regarding quoting mysql queries in Zend framework. Though this question has helped me a bit but few things are still confusing:

1) $table is Zend_Db_Table. Trying to fetch a row from the table.

$where[] = $db->quoteInto('id = ?', $id);
$where[] = $db->quoteInto('user_id = ?', $user_id);
$row = $table->fetchRow($where);

Do I need to use quoteInto here or would it be automatically quoted in fetchRow? What'd be the better way for this kind of query? Could 2 of the quoteInto be merged into one, if required at all?

2) $table is Zend_Db_Table. Trying to save a row in the table.

$tablerow = $table->createRow();
$rowdata = array('id' => $id, 'user_id' => $user_id);
$tablerow->setFromArray($rowdata);
$ret = $tablerow->save();

Do I need to use some sort of quote function here?

3) $table is Zend_Db_Table. Trying to update a row in the table.

$row = $table->fetchRow($where);
$row->name = $name; 
$row->save();

Do I need to quote $name in second step?

4) General query

A)

$sql = "SELECT * FROM users where id=? and name=?";
$results = $db->fetchAll($sql, array($id, $name));

Do I need to quote here?
B)

$sql =  "SELECT * FROM users where id=? and name=?";
$stmt = $db->query($sql, array($id, $name)); 
$result = $stmt->fetchAll();

Do I need to quote here?

C) Which of the A or B is better for a general query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

甜扑 2024-08-18 07:32:04

不管答案如何,我认为编写一些测试可能会很有用,这样您就可以确定它正在做您想要它做的事情。设置测试数据库并使用 PHPUnit 或您喜欢的任何测试框架创建一些单元测试。

Regardless of the answer, I think it might be useful to write some tests so you can be certain it's doing what you want it to do. Set up a test database and create some unit tests with PHPUnit or whatever test framework you like.

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