duanfa2014 2015-09-14 14:33
浏览 46
已采纳

MySQL和AJAX,在一个工作但不在另一个工作[重复]

This question already has an answer here:

I am using AJAX to make a "load call" to a PHP script that contains information to be added to a database, the script is as such:

<?php

$create_tables = "
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` varchar(255) NOT NULL UNIQUE,
`password` varchar(255) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `user_details`
(
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`email_address` varchar(255) NOT NULL UNIQUE,
`address1` varchar(500) NULL,
`address2` varchar(500) NULL,  
`address3` varchar(500) NULL,
`postcode` VARCHAR(10) NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 CHARSET=latin1;

INSERT IGNORE INTO `user` (`id`, `name`, `password`) VALUES (1, 'admin', '704b037a97fa9b25522b7c014c300f8a');
INSERT IGNORE INTO `user_details` (`id`, `email_address`) VALUES (1, 'sam.swift@fifteendigital.co.uk');
";

function cre($query)
{
    $link = new mysqli();
    $link->real_connect("localhost", "root", "", "overr");
    return $link->query($query) ? "true" : "false";
}

print cre($create_tables);

?>

And the AJAX is called as thus:

function _ajax( u, t )
{
    $.ajax( {
        url: u,
        type: "GET",
        success: function ( r )
        {
            $( "#test" ).html( r );
            console.log( r );
            return r;
        },
        beforeSend: function ()
        {
            $( t ).html( "Loading" );
        }
    } );
}

$( document ).ready( function ()
{
    _ajax( "aj/tables.php", "#null" );
} );

And when I call the query in phpmyadmin, it works perfect, but when I call it through the AJAX from the load page, the console gets logged "false", how can I make it so that the script executes and returns true, even when there is nothing in the database at all I am still getting "false" returned through to the client.

The purpose of this is to set up the environment before anything is done to ensure that everything is in place and working correctly, this system will change in the future, but for now I just need to get it working

</div>
  • 写回答

2条回答 默认 最新

  • doupaoyan6083 2015-09-14 14:38
    关注

    Use mysqli::multi_query() not mysqli::query():

    http://php.net/manual/en/mysqli.multi-query.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?