Results 1 to 5 of 5

Thread: PHP & MySQL Problem

  1. #1
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP & MySQL Problem

    Hey DD,

    I have a problem inserting information from a post into my MySQL database. I don't really know what the problem is or how to fix it, so i'm hoping one of you guys can help me. Here's the code:

    Code:
    function showMessages() {
    	if (!empty($_REQUEST['message']) and !$_REQUEST['message']=="") {
    		$chatText = $_REQUEST['message'];
    		$chatName = $_REQUEST['username'];
    		mysql_query("insert into Chat VALUES (0, '$chatText', '$chatName')",$con) or die('Problem sending message: ' . mysql_error());
    		echo $_REQUEST['message'];
    	}
    }
    I've located the problem somewhere in that function, but I don't know what the problem is. The error message i recieve when this function is called is this:

    Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in c:\web\htdocs\chatroom.php on line 98
    Problem sending message:

    Anyway, I really need help on this one, so if anyone can help, I would greatly appreciate it.

    Thanks.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in c:\web\htdocs\chatroom.php on line 98
    $con is never defined. If it was defined outside the function, you need to explicitly
    Code:
    global $con;
    before using it. A better idea would be to pass it as an argument. I personally would write that function like so:
    Code:
    function showMessages($con = false) {
    	if($con === false) global $con;
    	if (empty($_REQUEST['message'])) return;
    
    	$chatText = mysql_real_escape_string($_REQUEST['message']);
    	$chatName = mysql_real_escape_string($_REQUEST['username']);
    
    	mysql_query("insert into Chat VALUES (0, '$chatText', '$chatName');") or die('Problem sending message: ' . mysql_error());
    	echo $_REQUEST['message'];
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Great. Thanks Twey.

  4. #4
    Join Date
    Aug 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanx for the posting of the problem and for answering it, I had almost the same unluck, so used the info

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Readers of this thread should note that in he code I posted $con was entirely redundant anyway, since it was never passed to either of the mysql_* functions so they did the default of simply using the last connection opened.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •