Page 1 of 1
Unable to connect on database
Posted: Sat Aug 02, 2008 7:56 pm
by GreenCell
So I'm new to SQL and databases in general. For fun, I just scripted a Tetris game in a 3D software, and wanted to setup an online scoreboard. I've been following this tutorial:
http://www.clickteam.com/eng/resources/ ... oards.html, and hit a wall when trying to get the php portion to work. Whenever I try to connect, it just displays, 'Unable to select database'. I'm pretty sure my info is right, but I still don't know where else in the code I could have messed up. Here it is:
Code: Select all
<html>
<head>
<title>ScoreBoard</title>
</head>
<body>
<?
$service = h41mysql63.secureserver.net;
$username = "labbejason";
$password = "pass";
$database = "labbejason";
mysql_connect($service, $username, $password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT * FROM tetrisScores";
$result = mysql_query($query);
$num = mysql_numrows($result);
?>
<table>
<tr class="header"><td>Pos</td><td>Name</td><td>Score</td><td>Date Submitted</td></tr>
<?
$loopindex = 0;
while ($loopindex < $num) {
$thisname = mysql_result($result, $loopindex, "name");
$thisscore = mysql_result($result, $loopindex, "score");
$thisdate = mysql_result($result, $loopindex, "dateuploaded");
$thispos = $loopindex+1;
echo ("<tr><td align=left><p>$thispos</p></td>
<td align=center><p>$thisname</p></td>
<td align=center><p>$thisscore</p></td>
<td align=center><p>$thisdate</p></td></tr>\n");
$loopindex++;
}
?>
</table>
</body>
</html>
Can anyone point me in the right direction with this?
Re: Unable to connect on database
Posted: Sat Aug 02, 2008 8:11 pm
by EverLearning
Check if you're connecting to the database at all
Code: Select all
mysql_connect($service, $username, $password) or die(mysql_error());
Also $service value should be in quotes.
Code: Select all
$service = "h41mysql63.secureserver.net";
Re: Unable to connect on database
Posted: Sat Aug 02, 2008 8:20 pm
by GreenCell
Thanks for the quick reply. I made a new php file with the following:
Code: Select all
<html>
<?
mysql_connect("h41mysql63.secureserver.net", "labbejason", "pass") or die(mysql_error());
?>
</html>
When trying to connect on it, via Firefox (is this how I should test it?), it displayed the message:
Client does not support authentication protocol requested by server; consider upgrading MySQL client
I'm not sure if I wrote it properly, so does that just mean that it didn't connect or that the script itself failed?
Re: Unable to connect on database
Posted: Sat Aug 02, 2008 8:27 pm
by EverLearning
Whats your PHP version, and whats your MySql version? The error message means that the PHP mysql client is to old for the mysql server.
Re: Unable to connect on database
Posted: Sat Aug 02, 2008 10:29 pm
by Chalks
GreenCell wrote:Code: Select all
<html>
<?
mysql_connect("h41mysql63.secureserver.net", "labbejason", "pass") or die(mysql_error());
?>
</html>
When trying to connect on it, via Firefox (is this how I should test it?), it displayed the message:
Client does not support authentication protocol requested by server; consider upgrading MySQL client
This looks _very_ similar to an issue I had on a godaddy.com account... do you have one?
To fix this, I switched the server from windows to linux, and used .htaccess to turn on php5:
Code: Select all
AddHandler x-httpd-php5 .php
AddHandler x-httpd-php .php4
takes .htaccess files ~1 hour to update on their servers.
For
some reason I couldn't access mysql databases on the windows server. It was a _pain_ to figure out. >:(
Re: Unable to connect on database
Posted: Sun Aug 03, 2008 2:15 am
by GreenCell
Actually, I am trying this on a GoDaddy account. It's pretty late right now, so I'll give you the info tomorrow, EverLearning. Thanks guys.
Re: Unable to connect on database
Posted: Sun Aug 03, 2008 4:03 pm
by GreenCell
MySQL version is 4.1. Not too sure where to find the PHP version.
Re: Unable to connect on database
Posted: Sun Aug 03, 2008 6:31 pm
by GreenCell
I redid everything with byethost and got it to work perfectly! Right now I'm able to submit scores from 3D studio Max. I just have one final question. Would it be very difficult to include a sorting function inside the php code, so all the scores get sorted from lowest to highest? I would assume it's just gathering the scores, do a loop with if statements to get it in the right place, then just using that array to display the scores.
Re: Unable to connect on database
Posted: Sun Aug 03, 2008 7:06 pm
by Bill H
The database will sort the scores for you. Just use an ORDER BY in your SQL statement when you read from the database.
Re: Unable to connect on database
Posted: Mon Aug 04, 2008 7:44 pm
by GreenCell
This is done through the database itself, and not through my php code, right? From mySQL I see 'Alter table order by', and I can sort it from there, but afterward when you keep adding onto it, it just appends at the end of the list instead of still sorting. Is there a way from there where it can permanently sort?
Re: Unable to connect on database
Posted: Mon Aug 04, 2008 8:31 pm
by Bill H
Check MySQL instructions for SELECT ORDER BY
Re: Unable to connect on database
Posted: Tue Aug 05, 2008 1:19 am
by GreenCell
Ok, got it. Had to use implement it in the php code which displays the information, not from mySQL.
Re: Unable to connect on database
Posted: Tue Aug 05, 2008 5:31 am
by Stryks
Everyone else said it, so I figure I might as well too.
There is no need to manually sort with PHP. mySQL will do it for you if you SELECT the data using an ORDER BY clause. As you have noted, altering the order of the items in the table itself is pretty pointless ( i.e. 'Alter table order by'). It becomes unordered when you add new items. But using the ORDER BY in a SELECT query will order items when you pull the list. i.e - things will always be ordered (although they aren't in the database if you go looking) and you save yourself the hassle and processing time of getting PHP to do something that is actually the job of the database.
Seriously ... you came here for advice. Take it.
