1

I am working on a commenting system. I need to be able to post (Insert into database) and the post automatically has to be visible on the site as well.

I have two PHP files, one which is the main page (index.php) and one to connect to the database and fetch (connect_to_database.php).

From connect_to_database.php I connect to the database, and try to fetch row. Then Ajax script is supposed to print into a div the information.

I am able to post, and I can check that the information is added to the database, but I do not see any of the database rows printed.

Could the problem be with the Ajax script?

Thanks for your help,

connect_to_database.php

...
$connect=mysqli_connect($mysql_host, $mysql_user, $mysql_password, $mysql_database);
//$connect=mysqli_connect("localhost", "commenter","phyBq2JT", "udb_commenter" );

// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$db_select = mysqli_select_db($connect,'udb_commenter');
$sql="SELECT * FROM $TABLE_NAME";
$result=mysqli_query( $connect, $sql);


$array = mysqli_fetch_row($result);
echo json_encode($array);
?>

index.php Here is only the part of the Ajax code. Above it, I have a working connection to the database.

<div class="output-post" id="output">
                        <script language="JavaScript" id="print" type="text/javascript" src="jquery.js">
                            $(function ()
                            {
                                $.ajax({

                                    url: 'connect_to_database.php',
                                    data: "",
                                    dataType: 'json',
                                    success: function(data)
                                    {
                                        var id = data[0];
                                        var name = data[1];
                                        var email = data[2];
                                        var type = data[3];
                                        var message = data[4];
                                        var date = data[5];

                                        $('.output-post').html("<b>id: </b>"+id+"<b> name: </b>"+name+email+type+message+date);
                                    }

                                });
                            });

                        </script>
</div>

The sql table:

CREATE TABLE IF NOT EXISTS `posts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(40) NOT NULL,
  `email` varchar(60) NOT NULL,
  `type` varchar(40) NOT NULL,
  `message` text NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
);
7
  • What does console.log(data) says? Commented Mar 30, 2014 at 14:09
  • Where can I find console.lo(data)? Commented Mar 30, 2014 at 14:17
  • console.log() will write into the javascript console of your browser. you can access this console. i don't know which browser you use but you can try to press F12 to open it up. Commented Mar 30, 2014 at 14:50
  • After the changes made below: SyntaxError: Using //@ to indicate sourceMappingURL pragmas is deprecated. Use //# instead jquery.min.js:1 Error: ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js is being assigned a //# sourceMappingURL, but already has one SyntaxError: missing } after function body example.de:34 Commented Mar 30, 2014 at 14:55
  • move your javascript code in an own <script>-block instead of writing it into jquery-script-tag Commented Mar 30, 2014 at 15:00

1 Answer 1

1

i think "data" in your success-method is not one row this are rows. so you have to access one row at once or use a "foreach".

<div class="output-post" id="output">
                        <script language="JavaScript" id="print" type="text/javascript" src="jquery.js">
                            $(function ()
                            {
                                $.ajax({

                                    url: 'connect_to_database.php',
                                    data: "",
                                    dataType: 'json',
                                    success: function(data)
                                    {
                                        jQuery.each(data.rows, function() {
                                           var id = this[0];
                                           var name = this[1];
                                           var email = this[2];
                                           var type = this[3];
                                           var message = this[4];
                                           var date = this[5];

                                           $('.output-post').append("<b>id: </b>"+id+"<b> name: </b>"+name+email+type+message+date);
                                        });
                                    }

                                });
                            });

                        </script>
</div>

also change your php-code into the following:

$array = mysqli_fetch_row($result);
echo "{'rows':" . json_encode($array) . "}";
?>
Sign up to request clarification or add additional context in comments.

7 Comments

Still nothing is printed. I was using the code for row printing, to see if anything could be printed at all.
can you just call the connect_to_database.php directly in another browser-window and send us the json you'll get. you can remove the data from this json, we just need the structure of it.
What I get is : ["27","name","[email protected]","new_post","Hello, this is me. I am the creator of Commenter. \r\nPlease feel free to post or comment.","2014-03-30 15:12:34"]
I was just changing and making sure I did everything as you suggested. The json output is still the same, only the first row and I still do not see anything printed.
ok can you replace "$('.output-post').append(" with "alert(" ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.