So, I've been having a bit of trouble getting MySQL to work.
My Apache server is up and running, PHP is properly installed, and my MySQL server status is running.
When I call phpinfo()
it tells me where the mysql.sock is located, both the folder and file exist.
I'm running OSX 10.8.4
I'm following this tutorial to try to get a grasp of how chats would work: http://www.ibm.com/developerworks/library/x-ioschat/
So when I run my php script, the page loads with errors. I'm new to PHP, and am having a difficult time debugging, installation was particularly troublesome.
So if I keep content-type: text/xml uncommented I get this error:
error on line 2 at column 1: Document is empty
It also tells me that the page is rendered up to the first error, so it makes sense that the page is blank when I load it.
When it is commented, the errors begin to make a bit more sense; however, given my absolute newbish nature to PHP I'm not really sure how to navigate them.
Here are the errors:
Notice: Undefined index: past in /messages.php on line 6
-- This makes sense, I think. I haven't built the client side yet, so there should be no past variable provided.
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /messages.php on line 16
--So, my background is client side, so what this tells me is that my $result variable has a boolean stored in it, instead of whatever is supposed to be in it for the mysql_fetch_assoc()
function call. Since the else statement should trigger, that means that either mysql_query()
is not working how properly, or my parameter for it is incorrect. I don't know which (if either), nor do I know the solution to either.
Warning: mysql_free_result() expects parameter 1 to be resource, boolean given in /messages.php on line 24
--Again, same as above; semi-makes sense, very unsure how to go about fixing it.
So, after the errors, nothing is displayed below. Which makes sense because the conditions are written under the assumption they'll have a resource in it, not a boolean (I think??)
In my php file, if you compare it with the tutorial you'll see I took out the htmlentities()
calls because I read on StackOverflow that they are not needed, and they didn't change the state of the errors I was getting either way.
Anyway, thanks so much for any advice/help given! Here is my code so far:
chat.sql:
DROP TABLE IF EXISTS chatitems;
CREATE TABLE chatitems (
id BIGINT NOT NULL PRIMARY KEY auto_increment,
added TIMESTAMP NOT NULL,
user VARCHAR(64) NOT NULL,
message VARCHAR(255) NOT NULL
);
messages.php:
<?php
ini_set('display_errors','1');
//header( 'Content-type: text/xml' );
mysql_connect( 'localhost:/private/var/mysql/mysql.sock', 'root', '' );
mysql_select_db( 'http://localhost/Documents/JoistChat/chat.sql' );
if ( $_REQUEST['past'] ) {
$result = mysql_query('SELECT * FROM chatitems WHERE id > '.
mysql_real_escape_string( $_REQUEST['past'] ).
' ORDER BY added LIMIT 50');
} else {
$result = mysql_query('SELECT * FROM chatitems ORDER BY added LIMIT 50' );
}
?>
<chat>
<?php
while ($row = mysql_fetch_assoc($result)) {
?>
<message added="<?php echo( $row['added'] ) ?>" id="<?php echo( $row['id'] ) ?>">
<user><?php echo( $row['user'] ) ?></user>
<text><?php echo( $row['message'])?></text>
</message>
<?php
}
mysql_free_result($result);
?>
</chat>
test.html:
<html>
<head>
<title>Chat Message Test Form</title>
</head>
<body>
<form action="http://localhost/JoistChat/messages.php"
method="POST">
User: <input name="user" /><br />
Message: <input name="message" /><br />
<input type="submit" />
</form>
</body>
</html>