通过$ .ajax调用运行PDO语句的问题 [英] Issue with running a PDO statement from a $.ajax call

查看:80
本文介绍了通过$ .ajax调用运行PDO语句的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有以下ajax呼叫:

Hey guys I have the following ajax call:

function sendUserfNotes()
{
    $.ajax({
    type: "POST",
    dataType: "json",
    url: '/pcg/popups/getNotes.php',
    data:
    {
        'nameNotes': notes_name.text(),
    },
    success: function(response) {
        $('#notes_body').text(response.the_notes);
        alert(response.the_notes);
        //$('#notes_body').html(data);
    }
});

一旦运行,它将转到该文件: .......

Once this runs and it goes to this file: .......

try {  
  # MySQL with PDO_MYSQL  
  $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);  
  $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

}  
catch(PDOException $e) { 
    echo "I'm sorry, I'm afraid I can't do that.";  
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);   
}

$username_notes = $_POST['nameNotes'];

$sql = "SELECT notes FROM csvdata WHERE username = :username";
$getmeminfo = $DBH->prepare($sql);
$getmeminfo->execute(array(':username' => $username_notes));
$row = $getmeminfo->fetch(PDO::FETCH_ASSOC);
$notes = $row['notes'];

$returnArray = array( 'the_notes' => $row['notes']);

echo json_encode($returnArray);
$DBH = null;

现在在这里,一旦返回json数组,$('#notes_body').text(response.the_notes);将使用返回的响应更改div,但是我的问题是我无法使其返回.它总是空的吗?

Now in here once the json array is returned the $('#notes_body').text(response.the_notes); will change a div with the response that was returned, but my issue is I cannot get it to return back. It is always null?

我不确定什么是错的,所以如果您愿意的话,请给我一些时间:)

I am not to sure what is wrong so if you would be so kind and to give me some of your time :)

大卫

更新:

所以一件事就是我已经对pdo语句进行了测试,并且可以正常工作.

So one thing is I have tested the pdo statement on it's own and it works.

如果我只是给$ username_notes一个直接名称,例如"BillCosby",它将为该人返回正确的值.

if I were to just give $username_notes a direct name like "BillCosby" it would return the right value for that person.

推荐答案

没有类似"$ .ajax中带有PDO的问题"之类的东西. PDO是服务器端,而$ .ajax是客户端.

There is no such thing like "issue with PDO in $.ajax". PDO is server side while $.ajax is client side.

这就像pdo语句甚至没有运行?

It's like the pdo statement does not even run?

没人知道.
除了您之外,没有人可以访问您的代码.
因此,您必须回答自己.调试您的代码.

Nobody knows.
Nobody have access to your code but you.
So, you have to answer yourself. Debug your code.

对于客户端调试,Firebug是无价的.
打开网络"选项卡,查看是否有HTTP调用.如果是-它返回了什么吗?
打开控制台,查看是否有任何错误.

For the client-side debugging Firebug is priceless.
Open it's Net tab and see if there was an HTTP call at all. If it was - did it return anything?
Open Console and see if there are any errors.

这篇关于通过$ .ajax调用运行PDO语句的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆