PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
I am going through the Create Your Own Database Driven Website book by Sitepoint and have a few questions you may be able to help me with.
For any that have the book I am working from page 77 at the moment:
The problem I am having is when I upload the code to my webspace, the database table I am trying to access is on my computers mysql (localhost i believe?) it isn't connecting to the mysql database, and I believe it is because I need the ip of my computer - where the database is stored - so that it can be accessed via a browser.
I need to find out what I need to enter here - $dbcnx = @mysql_connect ('[b]111.111.1.1[/b]', 'root', '(password for mysql'); - is it the ip address of my mac? And for the password, if I use the password for my mysql databse, will this be viewable by users who 'view source' on my web page?
I have gone to [b]system preferences/sharing/personal: web sharing[/b], as I was told this is the ip for my computer, and have these addresses:
View this computer’s website at http://111.111.1.1/ or your personal website at http://111.111.1.1/~SiBailey/
I have substituted the actual numbers for 1's as I am unsure whether I need to keep these numbers secure, but could you tell me if these are the characters I need to substitute 'localhost' for to access the mysql database on my computer, and if so how i would enter them, with the http:// or just the numbers.
The code I have so far is....
<?php
// Connect to the databse server
$dbcnx = @mysql_connect('111.111.1.1', 'root', '(password for mysql');
if (!$dbcnx) {
exit('<p>Unable to connect to the ' .
'database server at this time.</p>');
}
// Select the jokes database
if (!@mysql_select_db('ijdb')) {
exit('<p>Unable to locate the joke ' .
'database at this time.</p>');
}
?>
<p>Here are all the jokes in out database:</p>
<blockquote></blockquote>
<?php
// Request the text of all the jokes
$result = @mysql_query('SELECT joketext FROM joke');
if (!@result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
// Display the text of each joke in a paragraph
while ($row = mysql_fetch_array($result)) {
echo '<p>' . $row['joketext'] . '</p>';
}
?>
Sorry if any of this is unclear, I hope you can help,
try changing the ip to just "localhost". also i would highly recommend NOT putting the @ infront of all of your mysql functions. what the @ does is suppress errors, but if there ARE errors there then you might want to know about them since you are having problems am i not right?
$dbcnx = mysql_connect('111.111.1.1', 'root', '(password for mysql');
if ($dbcnx == false) {
exit('<p>Unable to connect to the ' .
'database server at this time.</p>');
} else {
exit('connected');
}
Now put this in a php file and run it. Get your username and password right and your host. Go there wth your browser and try to get 'connected' to appear on your screen. Try using "localhost" instead of the IP address.
You should never, ever, ever connect to your databases with the root user. It's a bit of a pain, but if anyone ever intercepted your connection, or read your code, your databases would be opened like a can of sardines.
That said, if you connect with another user, you should check the access privileges of that other user. There needs to be a line in the mysql->users table that says that user can connect from anywhere (%).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /mounted-storage/home3/sub006/sc10962/www/php/joketest2.php on line 14
I tried with my IP address, root, mysql password, and got this message:
Warning: mysql_connect(): Lost connection to MySQL server during query in /mounted-storage/home3/sub006/sc10962/www/php/joketest.php on line 14
Unable to connect to the database server at this time.
I've had that error before - looks like a socket error. I'd look for the mysql.sock file and rename it to mysql.sock.bak. Then, restart the MySQL server and it MAY be fixed. If that doesn't work, just do a Google for it.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.