Forum Moderators: coopster

Message Too Old, No Replies

help connecting to MySQL using PHP

         

bodycount

3:50 pm on Oct 14, 2005 (gmt 0)

10+ Year Member



I am a complete newbie at PHP and mySQL all I want to do is connect to my database and display it in a web page, but i can not for the life of me get this to work i know there is somthing missing under the line "$sql = 'SELECT * FROM `Addresses and Occupations` LIMIT 0, 30 ';
"

to display the contents of the database every thing i have tried does not work.

1. what i am doing wrong
2. how do you know that you have connected database (database is on a web server not on my PC).

<html>
<body>
<?php

$db = mysql_connect("localhost", "username", "PASSWORD");
mysql_select_db ("body1975_famiytree");

$sql = 'SELECT * FROM `Addresses and Occupations` LIMIT 0, 30 ';

?>
</body>
</html>

HELP PLEASE I AM GOING NUTS!

jatar_k

4:32 pm on Oct 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld bodycount,

this thread might help
Basics of extracting data from MySQL using PHP [webmasterworld.com]

I do have a question as well

$sql = 'SELECT * FROM `Addresses and Occupations` LIMIT 0, 30 ';

is Addresses and Occupations the exact tablename in your database?

ergophobe

4:35 pm on Oct 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Also this might help

$db = mysql_connect("localhost", "username", "PASSWORD") or die("Could not connect");

mysql_select_db ("body1975_famiytree") or die("Could not select DB");

Then at least you'll know you're connecting.

bodycount

10:27 pm on Oct 14, 2005 (gmt 0)

10+ Year Member



yep this is the acctual name "Addresses and Occupations" is this not advised?

jatar_k

11:25 pm on Oct 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



spaces in table names can cause problems, I didn't even think it would let you do it

ergophobe

12:39 am on Oct 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Allowed characters:


Table 64 Any character that is allowed in a filename, except ‘/’, ‘\’, or ‘.’

In addition to the restrictions noted in the table, no identifier can contain ASCII 0 or a byte with a value of 255. Database, table, and column names should not end with space characters.

[dev.mysql.com...]

But definitely to be avoided in mySQL. It works okay with spaces in Access, but it just makes everything more complicated - all table names must be quoted (Access) or backticked (MySQL), which is a hassle IMO.

bodycount

11:20 am on Oct 16, 2005 (gmt 0)

10+ Year Member



Thanks Guys

I changed the name of the table to ADDRESSES I dont know if this was part of the problem but hey I got somthing to display on my web page, and the link to that other post help alot aswell, I think i will be back with a few more questions.

Here is my NEWBIE starting script below.

<?php

$db = mysql_connect("localhost", "USERNAME", "PASSWORD") or die("Could not connect");

mysql_select_db ("YOURDATABASE_HERE") or die("Could not select DB");

$sql = 'SELECT * FROM `ADDRESSES` WHERE 1 LIMIT 0, 30 ';

$query = mysql_query($sql);

while ($row = mysql_fetch_array($query)) {
echo "<p>",$row['FULLNAME'],": ",$row['YOB']; }

?>