MySQL connect question, using 000webhost.com.

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
Zerk
Posts: 58
Joined: Wed Jan 20, 2010 2:59 am

MySQL connect question, using 000webhost.com.

Post by Zerk »

EDIT: Nevermind, I got it right. (code is updated) lol

When I try to connect to the DataBase, I get this message:

Code: Select all

Warning: mysql_connect() [function.mysql-connect]: Host '66.197.250.117' is not allowed to connect to this MySQL server in /home/a8504274/public_html/connect.php on line 5

Could not connect to data base.
my code is:

Code: Select all

<?php

$db = mysql_connect("mysql14.000webhost.com",
"a8504274_zerk",
"password") or die("Could not connect to db");

if(!$db)
 die("no db");

if(!mysql_select_db("a8504274_void",$db))
 die("No Database Selected");

 ?>
What am I supposed to put for the 3rd field of $db? I've tried my data base password and hosting pass word, but neither make the error go away. Do I have the first and second field right for $db? lol.....As you can see, I'm completely confused on how to connect to the data base. :oops:

EDIT: Nevermind, I got it right. (code is updated) lol
Coding - Unity3d JS, PHP, and small amount of C#.
Art stuff - GIMP, Blender
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: MySQL connect question, using 000webhost.com.

Post by Jackolantern »

The basic prototype of the connection is this:

Code: Select all

$dbResource = mysql_connect(host, username, password, database);
Host is the machine where the db resides. Your webhost will give that to you.

Username is the username to sign into your db with. Depending on your host, they will either give this to you, or you will have limited ability to create your own username.

Password is the password for the database. You set this up through your control panel.

Database is the database you want to connect to.

Alternatively, as you did in your code, you can leave off the last parameter (database) and select it later with mysql_select_db(database), but I think it is easier just to select it with the initial statement.

It looks like your issue is the password. You should have some kind of control panel on your webhost account. If you are having problems finding it or setting it up, you may need to contact your webhost. However, 000webhost is pretty common, so maybe someone else around here uses them and can give you more info.

Ohhh, I also just noticed that you have die("string") parameters in IF statements. Remove those. die() should only be used in actual database connection statements. Change your code to:

Code: Select all

<?php

@ $db = mysqli_connect("yourHost", "yourUserName", "yourPassWord", "database") or die("Could not connect to db");

if (!$db) {
echo 'No database resource';
exit;
}

if (mysqli_connect_errno()) {
echo 'Error in connecting to database.';
exit;
}
Add the "@" at the beginning of the database connection. This suppresses errors, so that you can give better error messages. It is also a security issue to allow users to see your database errors (it can give them the database name). The second part, mysqli_connect_errno() is to find out if any other errors occurred while connecting to the db. Also, the use of "mysqli_..." instead of "myql_" is to use the more modern MySQL PHP functions.
The indelible lord of tl;dr
User avatar
Zerk
Posts: 58
Joined: Wed Jan 20, 2010 2:59 am

Re: MySQL connect question, using 000webhost.com.

Post by Zerk »

Jackolantern wrote:The basic prototype of the connection is this:

Code: Select all

$dbResource = mysql_connect(host, username, password, database);
Host is the machine where the db resides. Your webhost will give that to you.

Username is the username to sign into your db with. Depending on your host, they will either give this to you, or you will have limited ability to create your own username.

Password is the password for the database. You set this up through your control panel.

Database is the database you want to connect to.

Alternatively, as you did in your code, you can leave off the last parameter (database) and select it later with mysql_select_db(database), but I think it is easier just to select it with the initial statement.

It looks like your issue is the password. You should have some kind of control panel on your webhost account. If you are having problems finding it or setting it up, you may need to contact your webhost. However, 000webhost is pretty common, so maybe someone else around here uses them and can give you more info.

Ohhh, I also just noticed that you have die("string") parameters in IF statements. Remove those. die() should only be used in actual database connection statements. Change your code to:

Code: Select all

<?php

@ $db = mysqli_connect("yourHost", "yourUserName", "yourPassWord", "database") or die("Could not connect to db");

if (!$db) {
echo 'No database resource';
exit;
}

if (mysqli_connect_errno()) {
echo 'Error in connecting to database.';
exit;
}
Add the "@" at the beginning of the database connection. This suppresses errors, so that you can give better error messages. It is also a security issue to allow users to see your database errors (it can give them the database name). The second part, mysqli_connect_errno() is to find out if any other errors occurred while connecting to the db. Also, the use of "mysqli_..." instead of "myql_" is to use the more modern MySQL PHP functions.

o.O

You know a lot more about this than I do. Lol, I'll try to use this when I get a chance^^
Coding - Unity3d JS, PHP, and small amount of C#.
Art stuff - GIMP, Blender
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: MySQL connect question, using 000webhost.com.

Post by Jackolantern »

Just for the record, you can use die("text here") the way you were. However, normal coding standards usually dictate to use die() with database connections/querying/etc., and exit("text here") for other uses. This ensures the maximum portability between different server configurations.

And I appreciate the compliment :) However, I am still learning, too. I have just been reading "PHP and MySQL Web Development, 4th Edition". I really can't praise this book enough, and recommend it for anyone learning PHP and MySQL for any purpose. When I finish it (I am about 4/5 of the way through it), I am going to write up a proper review for the review section, but I already know I will be giving it at least a 9 out of 10. I highly suggest reading it if you can. That way you will already have a firm grip on PHP and MySQL, and will only be having to focus on the techniques in Halls' tutorials. The 5th part of the book goes over a ton of common projects from the ground up, such as an ecommerce site (with a shopping cart), complex and secure user authentication (aka logging in), creating a forum system, and others. It is really amazing.
The indelible lord of tl;dr
User avatar
Zerk
Posts: 58
Joined: Wed Jan 20, 2010 2:59 am

Re: MySQL connect question, using 000webhost.com.

Post by Zerk »

Jackolantern wrote:Just for the record, you can use die("text here") the way you were. However, normal coding standards usually dictate to use die() with database connections/querying/etc., and exit("text here") for other uses. This ensures the maximum portability between different server configurations.

And I appreciate the compliment :) However, I am still learning, too. I have just been reading "PHP and MySQL Web Development, 4th Edition". I really can't praise this book enough, and recommend it for anyone learning PHP and MySQL for any purpose. When I finish it (I am about 4/5 of the way through it), I am going to write up a proper review for the review section, but I already know I will be giving it at least a 9 out of 10. I highly suggest reading it if you can. That way you will already have a firm grip on PHP and MySQL, and will only be having to focus on the techniques in Halls' tutorials. The 5th part of the book goes over a ton of common projects from the ground up, such as an ecommerce site (with a shopping cart), complex and secure user authentication (aka logging in), creating a forum system, and others. It is really amazing.
I'm pretty sure I saw that at the local library last time I was there haha. I'll go have a look. Thanks a lot^^

EDIT: and that connect code is much cleaner and works. :)

EDIT2: nevermind, doesn't have that book. Lol. I will have to find a PHP book of some kind though.
Coding - Unity3d JS, PHP, and small amount of C#.
Art stuff - GIMP, Blender
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: MySQL connect question, using 000webhost.com.

Post by Jackolantern »

Zerk wrote:I'm pretty sure I saw that at the local library last time I was there haha. I'll go have a look. Thanks a lot^^

EDIT: and that connect code is much cleaner and works. :)

EDIT2: nevermind, doesn't have that book. Lol. I will have to find a PHP book of some kind though.
Glad to hear it worked!

Too bad about the library not having that book. I would still try to buy it, borrow it, get a subscription with Safari Books Online, etc. That book easily replaced about 4 books I was planning to read, as it covers the needs that could only be filled with a plethora of other books.
The indelible lord of tl;dr
User avatar
Zerk
Posts: 58
Joined: Wed Jan 20, 2010 2:59 am

Re: MySQL connect question, using 000webhost.com.

Post by Zerk »

Jackolantern wrote:
Zerk wrote:I'm pretty sure I saw that at the local library last time I was there haha. I'll go have a look. Thanks a lot^^

EDIT: and that connect code is much cleaner and works. :)

EDIT2: nevermind, doesn't have that book. Lol. I will have to find a PHP book of some kind though.
Glad to hear it worked!

Too bad about the library not having that book. I would still try to buy it, borrow it, get a subscription with Safari Books Online, etc. That book easily replaced about 4 books I was planning to read, as it covers the needs that could only be filled with a plethora of other books.
Okay, I guess I'll try to keep looking.

EDIT: You can read it online here !!!
Coding - Unity3d JS, PHP, and small amount of C#.
Art stuff - GIMP, Blender
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: MySQL connect question, using 000webhost.com.

Post by Jackolantern »

Zerk wrote:
Jackolantern wrote:
Zerk wrote:I'm pretty sure I saw that at the local library last time I was there haha. I'll go have a look. Thanks a lot^^

EDIT: and that connect code is much cleaner and works. :)

EDIT2: nevermind, doesn't have that book. Lol. I will have to find a PHP book of some kind though.
Glad to hear it worked!

Too bad about the library not having that book. I would still try to buy it, borrow it, get a subscription with Safari Books Online, etc. That book easily replaced about 4 books I was planning to read, as it covers the needs that could only be filled with a plethora of other books.
Okay, I guess I'll try to keep looking.

EDIT: You can read it online here !!!
You mean you can purchase it to read online? I don't see any links beyond the "Look inside"-type of system like Amazon has where it lets you skim a couple of pages from the book.

You can also read it online with a subscription to Safari Books Online. I personally think a subscription with them is worth every penny. I have the unlimited plan where I can read all of their books (and they have almost every technical book that your average Barnes and Nobles would carry). However, I started with the basic plan where they give you a bookshelf that you have a limited amount of "check-outs" per month (which is around $24 a month), but then they sent me an email offering me the unlimited plan for a year for only $5 more (the unlimited plan is normally around $44, I think). So maybe if you signed up with the basic plan they would do the same for you, too. Or maybe just ask them for the deal and say your friend has it :P With how expensive technical books have gotten today, if you buy at least one book a month, you are much better off getting Safari.
The indelible lord of tl;dr
User avatar
Zerk
Posts: 58
Joined: Wed Jan 20, 2010 2:59 am

Re: MySQL connect question, using 000webhost.com.

Post by Zerk »

Jackolantern wrote:
Zerk wrote:
Jackolantern wrote: Glad to hear it worked!

Too bad about the library not having that book. I would still try to buy it, borrow it, get a subscription with Safari Books Online, etc. That book easily replaced about 4 books I was planning to read, as it covers the needs that could only be filled with a plethora of other books.
Okay, I guess I'll try to keep looking.

EDIT: You can read it online here !!!
You mean you can purchase it to read online? I don't see any links beyond the "Look inside"-type of system like Amazon has where it lets you skim a couple of pages from the book.

You can also read it online with a subscription to Safari Books Online. I personally think a subscription with them is worth every penny. I have the unlimited plan where I can read all of their books (and they have almost every technical book that your average Barnes and Nobles would carry). However, I started with the basic plan where they give you a bookshelf that you have a limited amount of "check-outs" per month (which is around $24 a month), but then they sent me an email offering me the unlimited plan for a year for only $5 more (the unlimited plan is normally around $44, I think). So maybe if you signed up with the basic plan they would do the same for you, too. Or maybe just ask them for the deal and say your friend has it :P With how expensive technical books have gotten today, if you buy at least one book a month, you are much better off getting Safari.
No, I mean you can read it online for free. Almost the whole thing :lol: If you have a barns and noble account (free) you can click the "See Inside" icon on the book picture and it lets you read the whole thing :P

Thanks for the Safari info, I'll check it out when I get a little more advanced in my "coding life"^^
Coding - Unity3d JS, PHP, and small amount of C#.
Art stuff - GIMP, Blender
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: MySQL connect question, using 000webhost.com.

Post by Jackolantern »

Wow, you're right. I signed up for a Barnes and Noble online account, and it does let you read about half of the book for free. The first half of this book is still about 500 pages, so that is more than enough to get most people started. That gives you all of the normal chapters, and about the only thing of significant value that you are missing in this version from what I can see are the full-size projects in Part 5. Those are an important part of the book, but not a bad deal for free at all ;)
The indelible lord of tl;dr
Post Reply

Return to “Advanced Help and Support”