-
PHP - SQL problem
I have been working on a website recently that needs to contain databases with things like emails (used as user name), passwords, and other information on the user. The problem I have been having is accessing the database using PHP. When I view this file in my browser, it only shows "connecting..." and doesn't continue with anything. I called the tech support, and that is the information they said to use, but its not working. I figured even if the information wasn't correct, it should go to the IF statement that displays an error, but that doesn't happen. Here is code that I have attempted to use to connect to the database. The domain is registered with Register.com if that helps. As you can tell, I am very new to PHP and SQL, so any help would be greatly appreciated.
Code:
<?php
/* Program: mysql_up.php
* Desc: Connects to MySQL Server and
* outputs settings.
*/
echo "<html>
<head><title>Test MySQL</title></head>
<body>";
$host="sql5c6a.megasqlservers.com";
$user="dbm.restoretraditionalamerica.com";
$password="*";
echo "connecting...";
$cxn = mysqli_connect($host,$user,$password);
echo "connected";
$sql="SHOW STATUS";
$result = mysqli_query($cxn,$sql);
if($result == false)
{
echo "<h4>Error: ".mysqli_error($cxn)."</h4>";
}
else
{
/* Table that displays the results */
echo "<table border='1'>
<tr><th>Variable_name</th>
<th>Value</th></tr>";
for($i = 0; $i < mysqli_num_rows($result); $i++)
{
echo "<tr>";
$row_array = mysqli_fetch_row($result);
for($j = 0;$j < mysqli_num_fields($result);$j++)
{
echo "<td>".$row_array[$j]."</td>\n";
}
}
echo "</table>";
}
?>
</body></html>
-
try with a simple script like this to see if it connects ok ->
Code:
// Connect to database.
@mysql_connect("localhost", "loginname", "password") or die('<font face="helvetica">At this time we are unable to connect to the Database.<br><br>The most likely cause is a problem with the server hosting the database.<br><br>Please try again later as these problems tend to be transient.<br><br>We apologise for the inconvenience.</font>');
@mysql_select_db("databasename") or die("Unable to select db");
If this works with your info, maybe there's a typo somewhere in the other script?
In this script too, "localhost" may need to be changed to the address of your ISP's server.
Cheers,
Niggles
-
I attempted to use localhost and the host i previously posted, both of which returned the answer: At this time we are unable to connect to the Database....
-
Have you verified username, host information, password etc etc?
your PHP connect to the MySQL database isn't working.
Also you have an error in your connection string.
$cxn = mysqli_connect
should be
$cxn = mysql_connect
-
That mysqli statement is correct if the MySQL is version 4.1 or above.
http://nl.php.net/manual/en/ref.mysqli.php
There is a slight flaw in the code though. The code doesn't check if the connection actually succeeded.
Add this and it may give you a clue why it's failing:
Code:
if (!$cxn) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
-
Ahh...shows you how adapt I am at programming...only do it when I have to..lol
-
Doesn't matter, I had to look up mysqli_ myself, I'm also used to seeing mysql_ :D
This shows you how long it's been since I played with the stuff ;)
It seems you can use mysql_ but the mysqli_ lets you make use of the features found in 4.1.0 and higher.
-
Don't suppress your error messages in your test scripts. That's kind of counter-productive, no?
My thoughts.
-
You have to isolate your problem so id comment out a bunch of code and see if it gets that far
-
The problem is it gets to line
echo "connecting...";and is fine, but it seems to stop on the next line.
I don't really know what you'd want me to comment out, because I know where its hanging, I just don't know what to do about it.