Jump to content

What wrong with this code?


Flowdy

Recommended Posts

If you are using cPanel, when you log in do you see various icons (see attached pic), then when you click on MySQL Database you will see the connection string at the top (under MySQL Account Maintenance)

like this

Connection Strings
Perl	$dbh = DBI->connect("DBI:mysql:ad_joke:localhost","ad_joker","<PASSWORD HERE>");
PHP	$dbh=mysql_connect ("localhost", "ad_joker", "<PASSWORD HERE>") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("ad_joke");

Note ; password is not shown there , if you forgot the password you can delete current user and create a new one.

 

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

Now your connection must be like this, please keep the password and try

<?php 
$dbhost = "localhost";
$dbuser = "poison_frgn1";
$dbpass = " "; // keep your password here 
$dbname = "poison_frgn1";

$link = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$link) 
{
    die('Could not connect: ' . mysql_error());
}

$selectdb = mysql_select_db($dbname) or die("Could not connect to database.");
?>

Link to comment
Share on other sites

The posion_frgn1 isnt the database i made for this its the poison_game one is the first attached image...

so i put in the poison_game instead, this being the new code

 

<?php 
$dbhost = "localhost";
$dbuser = "poison_mygame";
$dbpass = ""; // keep your password here 
$dbname = "poison_mygame";

$link = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$link) 
{
    die('Could not connect: ' . mysql_error());
}

$selectdb = mysql_select_db($dbname) or die("Could not connect to database.");
?>

// Check to see if user logged in

session_start();

if((!isset($_SESSION['id'])) || (!isset($_SESSION['username'])) ||
(!isset($_SESSION['password'])))
{
unset($_SESSION['username']);
unset($_SESSION['password']);
unset($_SESSION['id']);

$loggedin = 0;
}
else
{
$loggedin =1;
}

?>

 

I took my password out but now i get this message  ???

 

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'poison_mygame'@'localhost' (using password: YES) in /home/poison/public_html/testphp/connect.php on line 7

Could not connect: Access denied for user 'poison_mygame'@'localhost' (using password: YES)

Link to comment
Share on other sites

n~ link=topic=169493.msg748629#msg748629 date=1196244152]

Now your connection must be like this, please keep the password and try

<?php 
$dbhost = "localhost";
$dbuser = "poison_frgn1";
$dbpass = " "; // keep your password here 
$dbname = "poison_frgn1";

$link = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$link) 
{
    die('Could not connect: ' . mysql_error());
}

$selectdb = mysql_select_db($dbname) or die("Could not connect to database.");
?>

 

Why do you need $link?

 

Not just:

<?php 
mysql_connect($dbhost, $dbuser, $dbpass) or die('Could not connect: ' . mysql_error());
?>

To me it looks tidier, but I'm no professional; just opinion.

 

Also, just to eliminate any type of spelling error (even though there is none), have

<?php
$dbhost = "127.0.0.1";
?>

 

in the MySQL databases via Control Panel there is nothing under the database i made for this code   just has the title poison_mygame

 

That could explain why you can't connect to the database - it doesn't exist. ;)

 

 

The posion_frgn1 isnt the database i made for this its the poison_game one is the first attached image...

so i put in the poison_game instead, this being the new code

 

 

<?php 
$dbhost = "localhost";
$dbuser = "poison_mygame";
$dbpass = ""; // keep your password here 
$dbname = "poison_mygame";

$link = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$link) 
{
    die('Could not connect: ' . mysql_error());
}

$selectdb = mysql_select_db($dbname) or die("Could not connect to database.");
?>

// Check to see if user logged in

session_start();

if((!isset($_SESSION['id'])) || (!isset($_SESSION['username'])) ||
(!isset($_SESSION['password'])))
{
unset($_SESSION['username']);
unset($_SESSION['password']);
unset($_SESSION['id']);

$loggedin = 0;
}
else
{
$loggedin =1;
}

?>

I took my password out but now i get this message 

 

 

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'poison_mygame'@'localhost' (using password: YES) in /home/poison/public_html/testphp/connect.php on line 7

Could not connect: Access denied for user 'poison_mygame'@'localhost' (using password: YES)

 

If you only get that error, it's now because you didn't use a password. Try the same code (with the changes I suggested above) but actually include your db password. Then in theory it should work.

 

Sam

Link to comment
Share on other sites

I did use my password last time i just took it out when posting the code on here....I changed things and now i get this

 

the code

<?php 
$dbhost = "localhost";
$dbuser = "poison_mygame";
$dbpass = ""; // keep your password here 
$dbname = "poison_mygame";

<?php 
mysql_connect($dbhost, $dbuser, $dbpass) or die('Could not connect: ' . mysql_error());
?>

// Check to see if user logged in

session_start();

if((!isset($_SESSION['id'])) || (!isset($_SESSION['username'])) ||
(!isset($_SESSION['password'])))
{
unset($_SESSION['username']);
unset($_SESSION['password']);
unset($_SESSION['id']);

$loggedin = 0;
}
else
{
$loggedin =1;
}

?>

 

The error

Parse error: syntax error, unexpected '<' in /home/poison/public_html/testphp/connect.php on line 7
Link to comment
Share on other sites

I did use my password last time i just took it out when posting the code on here....I changed things and now i get this

 

the code

<?php 
$dbhost = "localhost";
$dbuser = "poison_mygame";
$dbpass = ""; // keep your password here 
$dbname = "poison_mygame";

<?php 
mysql_connect($dbhost, $dbuser, $dbpass) or die('Could not connect: ' . mysql_error());
?>

// Check to see if user logged in

session_start();

if((!isset($_SESSION['id'])) || (!isset($_SESSION['username'])) ||
(!isset($_SESSION['password'])))
{
unset($_SESSION['username']);
unset($_SESSION['password']);
unset($_SESSION['id']);

$loggedin = 0;
}
else
{
$loggedin =1;
}

?>

 

The error

Parse error: syntax error, unexpected '<' in /home/poison/public_html/testphp/connect.php on line 7

 

You already have the <?php tag open at the top of the page.. you can't open it twice. Remove that tag and it should work.

 

Sam

 

N.B. I only put the <php tags on every piece of code I post here just so the php highlighting works.

Link to comment
Share on other sites

Nope didnt work..

 

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'poison_mygame'@'localhost' (using password: YES) in /home/poison/public_html/testphp/connect.php on line 8
Could not connect: Access denied for user 'poison_mygame'@'localhost' (using password: YES)

 

Am starting to think i added a username and password to the database the wrong way

Link to comment
Share on other sites

Nope didnt work..

 

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'poison_mygame'@'localhost' (using password: YES) in /home/poison/public_html/testphp/connect.php on line 8
Could not connect: Access denied for user 'poison_mygame'@'localhost' (using password: YES)

 

Am starting to think i added a username and password to the database the wrong way

 

Try omitting the variables and just type the values in as themselves (not sure why it'd work but it might).

 

For example:

 

<?php 
mysql_connect("127.0.0.1", "poison_mygame", "[your-password]"); // 127.0.0.1 goes to the IP of the host (where most likely your db is stored).
mysql_select_db("poison_mygame");
?>

 

Sam

Link to comment
Share on other sites

Sorry for the double post...incase you cant see the page as a friend told me she couldn't i get these errors

 

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'poison_mygame'@'localhost' (using password: YES) in /home/poison/public_html/testphp/connect.php on line 8

 

Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home/poison/public_html/testphp/connect.php on line 9

 

Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in /home/poison/public_html/testphp/connect.php on line 9

 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/poison/public_html/testphp/connect.php:8) in /home/poison/public_html/testphp/connect.php on line 14

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/poison/public_html/testphp/connect.php:8) in /home/poison/public_html/testphp/connect.php on line 14

 

But i can see text boxes for

username

password

and submit button

Link to comment
Share on other sites

Sure here is the code

 

<?php 
$dbhost = "localhost";
$dbuser = "poison_mygame";
$dbpass = "england"; // keep your password here 
$dbname = "poison_mygame";


mysql_connect("127.0.0.1", "poison_mygame", "[england]"); // 127.0.0.1 goes to the IP of the host (where most likely your db is stored).
mysql_select_db("poison_mygame");


// Check to see if user logged in

session_start();

if((!isset($_SESSION['id'])) || (!isset($_SESSION['username'])) ||
(!isset($_SESSION['password'])))
{
unset($_SESSION['username']);
unset($_SESSION['password']);
unset($_SESSION['id']);

$loggedin = 0;
}
else
{
$loggedin =1;
}

?>

Link to comment
Share on other sites

Sure here is the code

 

<?php 
$dbhost = "localhost";
$dbuser = "poison_mygame";
$dbpass = "england"; // keep your password here 
$dbname = "poison_mygame";


mysql_connect("127.0.0.1", "poison_mygame", "[england]"); // 127.0.0.1 goes to the IP of the host (where most likely your db is stored).
mysql_select_db("poison_mygame");

// Check to see if user logged in

session_start();

if((!isset($_SESSION['id'])) || (!isset($_SESSION['username'])) ||
(!isset($_SESSION['password'])))
{
unset($_SESSION['username']);
unset($_SESSION['password']);
unset($_SESSION['id']);

$loggedin = 0;
}
else
{
$loggedin =1;
}

?>

 

Ok, I think that's my fault, take the square brackets out from your password in the mysql_connect() see if that works. I only put them there to say that's where to type your password, my bad.

 

So if you have it as

 

mysql_connect("127.0.0.1", "poison_mygame", "england");

 

That should work now.

 

Sam

Link to comment
Share on other sites

I took the brackets out and still not working...i dont quite understand what you mean janim

 

Errors

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'poison_mygame'@'localhost' (using password: YES) in /home/poison/public_html/testphp/connect.php on line 8

 

Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home/poison/public_html/testphp/connect.php on line 9

 

Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in /home/poison/public_html/testphp/connect.php on line 9

 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/poison/public_html/testphp/connect.php:8) in /home/poison/public_html/testphp/connect.php on line 14

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/poison/public_html/testphp/connect.php:8) in /home/poison/public_html/testphp/connect.php on line 14

 

Code

<?php 
$dbhost = "localhost";
$dbuser = "poison_mygame";
$dbpass = "england"; // keep your password here 
$dbname = "poison_mygame";


mysql_connect("127.0.0.1", "poison_mygame", "england"); // 127.0.0.1 goes to the IP of the host (where most likely your db is stored).
mysql_select_db("poison_mygame");


// Check to see if user logged in

session_start();

if((!isset($_SESSION['id'])) || (!isset($_SESSION['username'])) ||
(!isset($_SESSION['password'])))
{
unset($_SESSION['username']);
unset($_SESSION['password']);
unset($_SESSION['id']);

$loggedin = 0;
}
else
{
$loggedin =1;
}

?>

Link to comment
Share on other sites

I took the brackets out and still not working...i dont quite understand what you mean janim

 

Errors

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'poison_mygame'@'localhost' (using password: YES) in /home/poison/public_html/testphp/connect.php on line 8

 

Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home/poison/public_html/testphp/connect.php on line 9

 

Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in /home/poison/public_html/testphp/connect.php on line 9

 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/poison/public_html/testphp/connect.php:8) in /home/poison/public_html/testphp/connect.php on line 14

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/poison/public_html/testphp/connect.php:8) in /home/poison/public_html/testphp/connect.php on line 14

 

Code

<?php 
$dbhost = "localhost";
$dbuser = "poison_mygame";
$dbpass = "england"; // keep your password here 
$dbname = "poison_mygame";


mysql_connect("127.0.0.1", "poison_mygame", "england"); // 127.0.0.1 goes to the IP of the host (where most likely your db is stored).
mysql_select_db("poison_mygame");


// Check to see if user logged in

session_start();

if((!isset($_SESSION['id'])) || (!isset($_SESSION['username'])) ||
(!isset($_SESSION['password'])))
{
unset($_SESSION['username']);
unset($_SESSION['password']);
unset($_SESSION['id']);

$loggedin = 0;
}
else
{
$loggedin =1;
}

?>

 

use

 {
$loggedin == 0;
}
else {
$loggedin == 1;
}

 

Is what I think he meant.

 

session_start(); must be at the top of all your code.

 

It would seem that your database isn't on the same server as your actual localhost. Unless.. are you using apache or a web server?

 

Sam

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.