面向对象查询功能
如果这听起来像是一个非常愚蠢的问题,我很抱歉,但我刚刚开始掌握面向对象编程的概念,而且我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
单例模式有一个特定的目的:
这不适合您上面介绍的功能。
例如,在创建系统范围的设置类时,将使用单例模式。 Singleton 防止自身被实例化多次(或根本),而是提供一个位置来获取和设置设置信息。
A Singleton pattern has a specific purpose:
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.