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条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行