我已经开始学习如何使用OOP并创建了一个用户授权类来检查用户是否存在等.目前我使用全局变量连接到数据库,$dbh这是一个PDO连接.我听说以这种方式使用全局变量并不是一种好的做法,但我不确定如何改进它,我只是将$dbh变量传递给连接到数据库时需要它的方法,为什么不考虑这个好的做法?
这是我正在使用的一些代码:
调用程序中包含的数据库PDO连接:
//test the connection
try{
//connect to the database
$dbh = new PDO("mysql:host=localhost;dbname=oopforum","root", "usbw");
//if there is an error catch it here
} catch( PDOException $e ) {
//display the error
echo $e->getMessage();
}
Run Code Online (Sandbox Code Playgroud)
需要数据库连接的类:
class Auth{
private $dbh;
function __construct(){
global $dbh;
$this->dbh = $dbh;
}
function validateLogin($username, $password){
// create query (placing inside if statement for error handling)
if($stmt = $this->dbh->prepare("SELECT * FROM oopforumusers WHERE username = ? AND password = ?")){ …Run Code Online (Sandbox Code Playgroud) 我是jQuery的新手,我正在尝试编写一个简单的脚本,当单击"显示更多"链接时将显示更多文本.
目前我能够更改"显示更多"和"显示更少"之间的链接文本,但我仍然无法切换两个css类.第一个类隐藏选择的内容,第二个类应该显示它.
这是迄今为止的代码:
CSS
.hideThis{
display:none;
}
.showThis{
display:inline;
}
Run Code Online (Sandbox Code Playgroud)
HTML
<div class="review">
<p >Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus pellentesque
sagittis velit, varius commodo odio accumsan ut. Etiam vestibulum commodo viverra.
Integer condimentum quis ligula eget interdum. Vivamus semper posuere sapien ut tristique
<span class="hideThis">Etiam vestibulum commodo viverra.
Integer condimentum quis ligula eget interdum. Vivamus semper posuere sapien</span>
<a href="#" class="showMoreText">... show more</a>
</p>
</div>
Run Code Online (Sandbox Code Playgroud)
jQuery的
$('.showMoreText').click(function(){
var text = $(this).html();
if(text == '... show more'){ …Run Code Online (Sandbox Code Playgroud) 我试图读取.csv文件,进行一些格式化,将每一行拆分为其列数据,并将新的分离列数据数组添加到数组列表中.然后我想以不同的方式订购列表.目前只是按用户名按字母顺序升序.
这是我到目前为止所尝试的:
// create list for storing arrays
List<string[]> users;
string[] lineData;
string line;
// read in stremreader
System.IO.StreamReader file = new System.IO.StreamReader("dcpmc_whitelist.csv");
// loop through each line and remove any speech marks
while((line = file.ReadLine()) != null)
{
// remove speech marks from each line
line = line.Replace("\"", "");
// split line into each column
lineData = line.Split(';');
// add each element of split array to the list of arrays
users.Add(lineData);
}
IOrderedEnumerable<String[]> usersByUsername = users.OrderBy(user => user[1]);
Console.WriteLine(usersByUsername); …Run Code Online (Sandbox Code Playgroud) c# sorting console-application visual-studio-2010 unassigned-variable
目前,该方法使用for()循环和foreach()循环来迭代列表中的每个数组元素,以比较数组元素内的值.如果符合条件,我需要for循环来记录列表的当前索引值,以便我知道之后重建列表的位置.
// update high score list
public void updateScores()
{
// if the list of scores isnt empty compare score
if (list.Count() != 0)
{
// loop through each array in the list
for (int i = 0; i <= list.Count(); i++)
{
// look into each linedata array in the list
foreach (string[] ld in list)
{
// only look at scores that are the same difficulty
if (lineData[1] == difficulty)
{
// if score …Run Code Online (Sandbox Code Playgroud) 我有一个string[]包含数组的列表.
数组包含两个元素,[0] =得分,[1] =难度(1或2).
我使用以下LINQ语句按难度为2的降序按分数重新排序列表.
scoresDesHard = list.OrderByDescending(ld => lineData[0]).Where(ld => lineData[1] == "2");
Run Code Online (Sandbox Code Playgroud)
我目前正在将新排序的列表绘制到屏幕中,XNA如下所示:
// draw highscores to the screen
public void Draw(SpriteBatch spriteBatch)
{
string mainString = "";
// build the hard list string
foreach (var li in scoresDesHard)
{
mainString += li[0] + " " + li[1] + "\r\n";
}
spriteBatch.DrawString(scoreFont, ""+ mainString, hardScoresPos, Color.White);
}
Run Code Online (Sandbox Code Playgroud)
它根本没有订购列表,并显示两个困难的分数:
000001 1
000001 2
122122 1
125555 1
22 1
23131 2
Run Code Online (Sandbox Code Playgroud)
它应该输出:
23131 …Run Code Online (Sandbox Code Playgroud) 我几天前部署了我的第一个Java Web应用程序,并意识到发生了一件奇怪的事情.一段时间后,所有依赖于我的数据库连接的动态内容和功能(推荐提交,管理员登录)都停止了工作.似乎这种情况每24小时左右就会发生一次.每天早上我都意识到它不再起作用了.
我通过访问Tomcat Web应用程序管理器并单击相关Web应用程序上的"重新加载"来解决此问题.网站的动态功能立即再次运作.
我的服务器正在运行Tomcat7,MySQL并且Web应用程序使用JDBC驱动程序建立与数据库的连接.我没有对Apache或Tomcat设置进行任何更改.
我有其他用PHP编写的Web应用程序,它们可以持久地工作而不会出现故障,这似乎是这个Java Web应用程序存在这个问题.
导致这种情况发生的原因是什么?如何在不再需要重新加载Web应用程序之前再次建立数据库连接?
编辑:附加一些数据库连接的代码
数据库连接
public class DBConnection {
private static Connection conn;
private static final Configuration conf = new Configuration();
private static final String dbDriver = conf.getDbDriver();
private static final String dbHostName = conf.getDbHostname();
private static final String dbDatabaseName = conf.getDbDatabaseName();
private static final String dbUsername = conf.getDbUsername();
private static final String dbPassword = conf.getDbPassword();
public Connection getConnection(){
try{
Class.forName(dbDriver);
Connection conn = (Connection) DriverManager.getConnection(dbHostName + dbDatabaseName, dbUsername, …Run Code Online (Sandbox Code Playgroud)