PHP HELP - 'cannot connect'

pspost

New Member
Messages
1
Reaction score
0
Points
0
Im vary new to php I'm doing ok on geting my surver up and the code but I cant get the surver to connect to my website!
i dont know what im saposted to do this is what im puting down

the **** is blocked content but you get the idea

<?php
$host=http://chopin.x10hosting.com:****; // Host name
$username="*"; // Mysql username
$password="*"; // Mysql password
$db_name="pspost_database"; // Database name ^thats the database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

I dont know what's the ulr of my data base can some one help me
maby someone that had the problem before?
thank you :dunno::dunno::dunno::dunno:

pleas do not take my code with out asking me thank you
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
PHP:
<?php
$host="localhost"; // Host name
$username="*"; // Mysql username
$password="*"; // Mysql password
$db_name="pspost_database"; // Database name ^thats the database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
 
Last edited:

Alice_Hoyden

New Member
Messages
6
Reaction score
0
Points
0
Add this to the top: error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
Then if the database is on your site as well (which it seems to be), the host name would be 'localhost' and you should use single quotes for the values, not double quotes.

It also might be a good idea to put the connection details and the connection function into a separate php file which will be included into every file that requires a database connection (include 'connectiondetailsfile.php')

here's a connection function:
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
set the values before you call it. $servername='localhost' $dbuser='yoursiteCpanelloginname_databasename' (probably, in any case there will be a username involved)
$dbpassword='password associated with the username for that database'

Hope that helps.
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
Alice, here is an updated version of you function, which doesn't use globals:
PHP:
function connectToDb($servername, $dbname, $dbuser, $dbpassword) {
$link = mysql_connect($servername, $dbuser, $dbpassword);
if(!$link) {
die("Could not connect to MySQL: " . mysql_error());
}
$db = mysql_select_db($dbname, $link);
if(!$db) {
die ("Cannot use " . $dbname . " : " . mysql_error());
}
return $link;
}

I would also recommend using mysqli instead.
 

sapotest

New Member
Messages
1
Reaction score
0
Points
0
My php-mysql script create.php placed under /home/sapo/public_html and given 755 privileges causes a Gateway Timeout when run. I have tested simple non-db php scripts which work just fine.

I have created a database named sapo_testingdb and a db user named sapo_testing which has been added and given full privileges for the database sapo_testingdb. The database is shown correctly in the phpMyAdmin panel as well.

My code for create.php is as follows.

Code:
<?php
echo "Creating database... ";

$username="sapo_testing";
$password="notthis";
$database="sapo_testingdb";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query="CREATE TABLE Tag (id int(6) NOT NULL auto_increment,uid varchar(100),PRIMARY KEY (id),UNIQUE id (id))";
mysql_query($query);
mysql_close();

echo "Database created";
?>

My domain is called sapo and when I create a database with an intended name testingdb then it is changed to sapo_testingdb, the same for the user testing. I believe this is supposed to happen, but I have tried all variations with and without the prefix sapo_ for both the username and database, and all causes a timeout. As I've said, other php scripts without database access run just fine.

Why does this give a Gateway Timeout? Your help is appreciated, since I cannot find a previous post or thread which could help me.
 
Top