Comment Box Script

dbqclassof2012

New Member
Messages
15
Reaction score
0
Points
0
Hello I am currently working on making a comment box for my website and have had a lot of progress but this end part i have not been able to get. I have the form and everything done it submits it to the database fine but then bringing the comments back to the page it always comes up with this error
Code:
Warning:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/dbqclass/public_html/getcomments.php on line 22
I have not been able to fix this and if anyone could help me it would be greatly appreciated!!!!!Heres the code i am working with
PHP:
<?php
// Open Connection To mySQL.
// Replace 'username' and 'password' with appropiate login values for your server.

$mysqlLink = mysql_connect( 'localhost' , 'username' , 'pass' );

// Select mySQL Database.

mysql_select_db( 'comments' , $mysqlLink );

// Create Query To Create Table.

$sql = 'SELECT `name`, `comment_date`, `comment` FROM `comments`';

// Issue Query.

$result = mysql_query( $sql );

// Loop through results and print them.

while( $row = mysql_fetch_array( $result ) ) {
    echo $row[ 'name' ] . $row[ 'comment_date' ] . $row[ 'comment' ];
}

// Close mySQL Connection.

mysql_close( $mysqlLink );

?>


IF YOU PROVIED ME WITH MY ANSWER I WILL REWARD YOU WITH OVER 200 CREDITS!
 
Last edited:

freecrm

New Member
Messages
629
Reaction score
0
Points
0
This might be a quick answer.

I'm not sure you have to put ' in the $sql

i.e.

$sql = 'SELECT name, comment_date, comment FROM comments';
 

dbqclassof2012

New Member
Messages
15
Reaction score
0
Points
0
Ok thanks for the answer but it didnt work so if you have another answer it would be greatly appreciated
 
Last edited:

scopey

New Member
Messages
62
Reaction score
0
Points
0
Edit to the following and tell us what you get:

PHP:
<?php
// Open Connection To mySQL.
// Replace 'username' and 'password' with appropiate login values for your server.

$mysqlLink = mysql_connect( 'localhost' , 'username' , 'pass' );

// Select mySQL Database.

mysql_select_db( 'comments' , $mysqlLink );

// Create Query To Create Table.

$sql = 'SELECT `name`, `comment_date`, `comment` FROM `comments`';

// Issue Query.

$result = mysql_query( $sql ) or die(mysql_error());

// Loop through results and print them.

while( $row = mysql_fetch_array( $result ) ) {
    echo $row[ 'name' ] . $row[ 'comment_date' ] . $row[ 'comment' ];
}

// Close mySQL Connection.

mysql_close( $mysqlLink );

?>

Also, are you sure that both the table and the database have the name of 'comments'.

Usually the database name has a name like 'dbqclassof2012_comments' or something. Check phpMyAdmin for the proper database name. It will show the prefix (ie the 'dbqclassof2012_' bit) above all the database names.
 

dbqclassof2012

New Member
Messages
15
Reaction score
0
Points
0
I have just tried that but t does not seem to work the error it keeps saying :
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/dbqclass/public_html/getcomments.php on line 21

so i think it is with the coding but i dont know how to fix it because im not the best coder
 

scopey

New Member
Messages
62
Reaction score
0
Points
0
So you tried adding in the 'or die(mysql_error());' bit? The error you are getting is usually returned when you are selecting an invalid database or have an error in your syntax. Try adding the 'or die(mysql_error());' code to the end of all your mysql functions.
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
I agree with scopey here - all my database names are preceeded by my username.

i.e. me_databasename.

The name "comments" sounds too much like a table.

The line we are refering to is mysql_select_db( 'comments' , $mysqlLink );

If you are doing multiple pages, it may be worth setting up a connection file rather than putting the connection script into each page. This keeps secure info seperate and easier to manage.

This is done by setting all the variables in one connection.php file, usually put in a seperate folder in the root and would look something like this

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_connect= "localhost";
$database_connect= "databasename";
$username_connect= "username";
$password_connect= "password";
$connect = mysql_pconnect($hostname_connect, $username_connect, $password_connect) or die(mysql_error());
?>


then each file would just have one line at the top to reference this file

<?php require_once('Connections/exampleconnection.php'); ?>

And the select db would be

mysql_select_db($databasename, $connect);
 
Last edited:

dbqclassof2012

New Member
Messages
15
Reaction score
0
Points
0
I'm sorry but i have not been able to get this to work i will give you the whole code everything but the password and could you please change it to what it needs to be because fro some reason i cannot get.

Code:
<?php
// Open Connection To mySQL.
// Replace 'username' and 'password' with appropiate login values for your server.

$mysqlLink = mysql_connect( 'localhost' , 'dbqclass' , 'Password' );

// Select mySQL Database.

mysql_select_db( 'dbqclass_comments' , $mysqlLink );

// Create Query To Create Table.

$sql = 'SELECT `name`, `comment_date`, `comment` FROM `dbqclass_comments`';

// Issue Query.

$result = mysql_query( $sql );

// Loop through results and print them.

while( $row = mysql_fetch_array( $result ) ) {
    echo $row[ 'name' ] . $row[ 'comment_date' ] . $row[ 'comment' ];
}

// Close mySQL Connection.

mysql_close( $mysqlLink );

?>

So the username is dbqclass if you didn't notice above

Thanks
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
for your username, and database name, the prefix, like stated before is:

yourcpanelname_name

so, your cPanel name corresponds to the location of your site, like, my website's root would be,

/home/leafy/public_html/


since you are on a shared server, it must be preceeded with the name, so that there isn't two people with the database "forums" and writing information over each other.

have you tried that?



EDIT: also, try this script in any file, or by itself, like, connectiontest.php

PHP:
<?php
# Purpose: Establish a connection to the database
#
$dbname = 'cpanelname_dbname';
$dbhost = 'localhost';
$dbusername = 'cpanelname_dbuname';
$dbpassword = 'dbpass';
#
$dbconnection = @mysql_connect($dbhost, $dbusername, $dbpassword);
 
if ($dbname != '' && !@mysql_select_db($dbname)) 
 
{
 
echo 'I could NOT connect to the database! Check the username, password, etc.';
}
 
 else 
{
echo 'I connected. The connection is: ' . $dbconnection;
}
?>
 
Last edited:

dbqclassof2012

New Member
Messages
15
Reaction score
0
Points
0
I was just experimenting and i have figured out the answer i changed a dbqclass_comments to comments and now it works

Thank You All For Your Replys

I am going to give 200 credits to freecrm because he helped me the most!

*Now Sending*

THANKS AGAIN
Edit:
I have figured it out now so i DO NOT need anymore help
Thanks for looking

Could a mod please close this thread
 
Last edited:
Top