5 Replies - 460 Views - Last Post: 14 July 2011 - 03:43 PM

#1 Felix_Violo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-September 10

Cannot Connect To Database

Posted 13 July 2011 - 09:40 PM

Hi all,

I´m new to PHP and MySQL. I am trying to simply connect to my MySQL database I set up on hostgator. When I navigate to the .php file on the server I get this: Parse error: syntax error, unexpected T_VARIABLE on line 9

Note, I simply set up the database and assigned a user to it, no tables were added yet.

<?php

$dbhost = “localhost”;
$dbuser = “********”;
$dbpass = “********”;

$db = “********”;

mysql_connect (“$dbhost”,“$dbuser”,“$dbpass”) 

echo “successful connection”;

?>



Thank you for any input... if the answer is obvious, please suggest hints to me, rather than give me the answer, I´m trying to get a good grasp on this stuff.

Is This A Good Question/Topic? 0
  • +

Replies To: Cannot Connect To Database

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Cannot Connect To Database

Posted 13 July 2011 - 09:50 PM

Try the mysql_connect function without the inverted commas, and remove the space bteween mysql_connect and the opening bracket...

mysql_connect($dbhost, $dbuser, $dbpass)


Was This Post Helpful? 1
  • +
  • -

#3 Felix_Violo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-September 10

Re: Cannot Connect To Database

Posted 14 July 2011 - 05:40 AM

Okay, thanks for the tip... it makes logical sense to do so.

I am now getting this error Parse error: syntax error, unexpected T_ECHO on line 11

When I remove the echo statement, a blank screen display, so that should mean I connected to mysql.

I also just tried to change the code to this, no luck:

<?php

$dbhost = "localhost";
$dbuser = "********";
$dbpass = "********";

$db = "********";

mysql_connect($dbhost, $dbuser, $dbpass) 

if($dbhost, $dbuser, $dbpass = true)
echo "connection successful!";
if else
die

?>

This post has been edited by Felix_Violo: 14 July 2011 - 05:50 AM

Was This Post Helpful? 0
  • +
  • -

#4 Felix_Violo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-September 10

Re: Cannot Connect To Database

Posted 14 July 2011 - 08:56 AM

<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("database") or die(mysql_error());
echo "Connected to Database";
?>


^^This worked great! It connected and output "Connected to MySQL" and "Connected to Database". I also changed the database username to test to see if it wasn´t simply echoing out that data... I found that this is a fully working script.

With hostgator, you have to add a database user with their mysql tool, not phpmyadmin. That database user does not show up in phpmyadmin, I was told by hostgator it is the owner of the database. - How do I add site members to the database´s table so they can log in?

Should each user get two rows... both varchar(65)?

Also, after I get this to work, I´m going to try and add input forms and a submit form so the user could type in their username/password and login. I may get confused with that and hop over to the PHP forum... I will put that thread link on here when I do that.

Thanks!
Was This Post Helpful? 0
  • +
  • -

#5 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Cannot Connect To Database

Posted 14 July 2011 - 03:11 PM

I use HostGator too, so I'm familiar with what you're talking about.

Don't confuse the database login credentials with user login credentials. Most sites use the superadmin credentials to get into the database, and these credentials are hidden in the code, so the users can't see them.

If users need their own login and password, you're going to have to create database tables to hold that information. The user doesn't login to the database with their credentials, they login to the site with their credentials.

It's like the difference between giving someone the keys to their apartment, and giving them the keys to every apartment in the block.

I would strongly advise that you Google around a bit before you start to build this. W3Schools is a good place to start learning, or you could even install phpBB to circumvent the problem, as that has credentials, persistent sessions, and Captcha included.

Logins and passwords is a pretty difficult place to start with. I avoided it when I was first coding, as I could see it has many pitfalls.
Was This Post Helpful? 0
  • +
  • -

#6 Felix_Violo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-September 10

Re: Cannot Connect To Database

Posted 14 July 2011 - 03:43 PM

View Poste_i_pi, on 14 July 2011 - 03:11 PM, said:

I use HostGator too, so I'm familiar with what you're talking about.

Don't confuse the database login credentials with user login credentials. Most sites use the superadmin credentials to get into the database, and these credentials are hidden in the code, so the users can't see them.

If users need their own login and password, you're going to have to create database tables to hold that information. The user doesn't login to the database with their credentials, they login to the site with their credentials.

It's like the difference between giving someone the keys to their apartment, and giving them the keys to every apartment in the block.

I would strongly advise that you Google around a bit before you start to build this. W3Schools is a good place to start learning, or you could even install phpBB to circumvent the problem, as that has credentials, persistent sessions, and Captcha included.

Logins and passwords is a pretty difficult place to start with. I avoided it when I was first coding, as I could see it has many pitfalls.


Right, I understand what you are saying. I did this to input the users into the table and it worked great:
INSERT INTO database
VALUES (all of the rows I assigned... username, password, id...)

It´s tempting, but I´m going to hold off on PHPBB. I´d like to do this all by hand to learn the ins and outs of the process :)

Thank you for your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1