面向对象查询功能

发布于 2024-10-04 00:27:30 字数 1398 浏览 6 评论 0原文

如果这听起来像是一个非常愚蠢的问题,我很抱歉,但我刚刚开始掌握面向对象编程的概念,而且我对 php 的单例模式有点困惑。

如何合并查询并将作为变量保存的行的结果保存到单例模式中?

例如,下面的代码将如何合并?

$query = "SELECT * FROM members WHERE email=   '".mysql_real_escape_string($_SESSION['email1'])."'";
$result   = mysql_query($query) or die(mysql_error());
$number   = mysql_num_rows($result);
$i   = 0;

while ($i < $number) 
{

    $first_name  = mysql_result($result,$i,"first_name"); 
    $last_name  = mysql_result($result,$i,"last_name");
    $state   = mysql_result($result,$i,"home_state");
    $id_district  = mysql_result($result,$i,"district");
    $political_views = mysql_result($result,$i,"political_views");
    $first_issue  = mysql_result($result,$i,"first_issue");
    $second_issue  = mysql_result($result,$i,"second_issue");
    $third_issue  = mysql_result($result,$i,"third_issue");
    $email   = mysql_result($result,$i,"email");
    $iStand   = mysql_result($result,$i,"iStand");
    $photo   = mysql_result($result,$i,"photo");
    $code   = mysql_result($result,$i,"code");
    $changed   = mysql_result($result,$i,"changed");
    $mem_ID          = mysql_result($result,$i,"member_ID");
    $pass   = mysql_result($result,$i,"password");
    $privacy   = mysql_result($result,$i,"privacy");
    $since   = mysql_result($result,$i,"invite_date");
    $nombre   = mysql_result($result,$i,"peer_name");

    $i++;
  }

I'm sorry if this may sound like a really dumb question but I'm just beginning the grasp the concept of object oriented programming and I am kind of confused about the singleton pattern for php.

How would I incorporate a query along with saving the results from the rows fetched saved as variables into the singleton pattern?

For example, how would the code below get incorporated?

$query = "SELECT * FROM members WHERE email=   '".mysql_real_escape_string($_SESSION['email1'])."'";
$result   = mysql_query($query) or die(mysql_error());
$number   = mysql_num_rows($result);
$i   = 0;

while ($i < $number) 
{

    $first_name  = mysql_result($result,$i,"first_name"); 
    $last_name  = mysql_result($result,$i,"last_name");
    $state   = mysql_result($result,$i,"home_state");
    $id_district  = mysql_result($result,$i,"district");
    $political_views = mysql_result($result,$i,"political_views");
    $first_issue  = mysql_result($result,$i,"first_issue");
    $second_issue  = mysql_result($result,$i,"second_issue");
    $third_issue  = mysql_result($result,$i,"third_issue");
    $email   = mysql_result($result,$i,"email");
    $iStand   = mysql_result($result,$i,"iStand");
    $photo   = mysql_result($result,$i,"photo");
    $code   = mysql_result($result,$i,"code");
    $changed   = mysql_result($result,$i,"changed");
    $mem_ID          = mysql_result($result,$i,"member_ID");
    $pass   = mysql_result($result,$i,"password");
    $privacy   = mysql_result($result,$i,"privacy");
    $since   = mysql_result($result,$i,"invite_date");
    $nombre   = mysql_result($result,$i,"peer_name");

    $i++;
  }

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

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

发布评论

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

评论(1

穿越时光隧道 2024-10-11 00:27:30

单例模式有一个特定的目的:

单例模式是一种用于实现单例数学概念的设计模式,通过限制实例化一个类到一个对象。当只需要一个对象来协调整个系统的操作时,这非常有用

这不适合您上面介绍的功能。

例如,在创建系统范围的设置类时,将使用单例模式。 Singleton 防止自身被实例化多次(或根本),而是提供一个位置来获取和设置设置信息。

A Singleton pattern has a specific purpose:

the singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system

This isn't appropriate for the functionality that you've presented above.

A Singleton pattern would be used, for example, when creating a system-wide settings class. The Singleton prevents itself from being instantiated more than once (or, at all) and instead provides a single place to get and set setting information.

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