PHP 抛出 500 错误
PHP Throws 500 Error
我正在尝试查询 SQL 服务器数据库并在屏幕上显示 return 结果。我的页面按预期加载,但是当我按下按钮查询 SQL 服务器时,如果我查看控制台,它会显示 500 错误。
我需要更改什么才能根据需要在屏幕上return显示有效结果?
<select name="peopleinfo[]" multiple style="min-width: 200px;" id="peopleinfo">
<option value="red">Red</option>
<option value="blue">Blue</option>
</select>
<div><input type="submit" value="Submit" id="ajaxButton" onclick="ReturnIt()"></div>
<div id="result_data"></div>
<script>
function ReturnIt(){
var peopleinfo = $('#peopleinfo').val();
jQuery.ajax({
url: "",
type: 'POST',
dataType: "html",
data: { peopleinfo: peopleinfo },
success : function(result) {
$('#result_data').empty();
$('#result_data').append(result);
} ,
error: function(){
}
});
}
</script>
$peopleinfo = implode(',',$_REQUEST['peopleinfo']);
$option = array(); //prevent problems
$option['driver'] = 'mssql'; // Database driver name
$option['host'] = 'Lockwood'; // Database host name
$option['user'] = 'root'; // User for database authentication
$option['password'] = 'sa'; // Password for database authentication
$option['database'] = 'test'; // Database name
$option['prefix'] = ''; // Database prefix (may be empty)
$db = JDatabase::getInstance( $option );
$result = $db->getQuery(true);
$result->select($db->quoteName(array(".$peopleinfo.")));
$result->from($db->quoteName('[redheadstepchild]'));
$db->setQuery($result);
$row = $db->loadRowList();
print_r($row);
编辑
这是开发控制台显示的内容
这一行有错误jquery-1.12.4.js:10254
这就是我点击它时的实际语法
// Do send the request
// This may raise an exception which is actually
// handled in jQuery.ajax (so no try/catch here)
xhr.send( ( options.hasContent && options.data ) || null );
我添加了一个 try and catch exception,这将 return 您的查询问题
$peopleinfo = implode(',',$_REQUEST['peopleinfo']);
$option = array(); //prevent problems
$option['driver'] = 'mssql'; // Database driver name
$option['host'] = 'Lockwood'; // Database host name
$option['user'] = 'root'; // User for database authentication
$option['password'] = 'sa'; // Password for database authentication
$option['database'] = 'test'; // Database name
$option['prefix'] = ''; // Database prefix (may be empty)
try{
$db = JDatabase::getInstance( $option );
$result = $db->getQuery(true);
$result->select($db->quoteName(array(".$peopleinfo.")));
$result->from($db->quoteName('[redheadstepchild]'));
$db->setQuery($result);
}catch(Exception $e){
echo $e->getMessage();
}
$row = $db->loadRowList();
print_r($row);
您好,我制作了一个模块来完成您正在做的事情,并且效果很好。我把例子放在这里可能对你有帮助。
模块:
<?php
defined('_JEXEC') or die;
include_once __DIR__ . '/helper.php';
// Instantiate global document object
$doc = JFactory::getDocument();
$js = <<<JS
(function ($) {
$(document).on('click', 'input[type=submit]', function () {
var value = $('input[name=data]').val(),
request = {
'option' : 'com_ajax',
'module' : 'ajax_search',
'data' : value,
'format' : 'raw'
};
$.ajax({
type : 'POST',
data : request,
success: function (response) {
$('.search-results').html(response);
}
});
return false;
});
})(jQuery)
JS;
$doc->addScriptDeclaration($js);
require JModuleHelper::getLayoutPath('mod_ajax_search');
?>
帮手:
<?php
defined('_JEXEC') or die;
class modAjaxSearchHelper
{
public static function getAjax()
{
include_once JPATH_ROOT . '/components/com_content/helpers/route.php';
$input = JFactory::getApplication()->input;
$data = $input->get('data', '', 'string');
$db = JFactory::getDbo();
$query = $db->getQuery(true);
// Build the query
$query
->select($db->quoteName(array('id','name','email')))
->from($db->quoteName('#__banner_clients'))
->where($db->quoteName('name') . ' LIKE '. $db->quote('%' . $data . '%'));
//->order('id ASC');
$db->setQuery($query);
$results = $db->loadObjectList();
// Get output
$output = null;
foreach($results as $result){
$output .= '<h4><a href="' . ContentHelperRoute::getArticleRoute($result->id, $result->email) . '">' . $result->name . '</a></h4>';
}
if($output == null or empty($data))
{
$output = 'Sorry! No results for your search.';
}
return $output;
}
}
?>
我正在尝试查询 SQL 服务器数据库并在屏幕上显示 return 结果。我的页面按预期加载,但是当我按下按钮查询 SQL 服务器时,如果我查看控制台,它会显示 500 错误。
我需要更改什么才能根据需要在屏幕上return显示有效结果?
<select name="peopleinfo[]" multiple style="min-width: 200px;" id="peopleinfo">
<option value="red">Red</option>
<option value="blue">Blue</option>
</select>
<div><input type="submit" value="Submit" id="ajaxButton" onclick="ReturnIt()"></div>
<div id="result_data"></div>
<script>
function ReturnIt(){
var peopleinfo = $('#peopleinfo').val();
jQuery.ajax({
url: "",
type: 'POST',
dataType: "html",
data: { peopleinfo: peopleinfo },
success : function(result) {
$('#result_data').empty();
$('#result_data').append(result);
} ,
error: function(){
}
});
}
</script>
$peopleinfo = implode(',',$_REQUEST['peopleinfo']);
$option = array(); //prevent problems
$option['driver'] = 'mssql'; // Database driver name
$option['host'] = 'Lockwood'; // Database host name
$option['user'] = 'root'; // User for database authentication
$option['password'] = 'sa'; // Password for database authentication
$option['database'] = 'test'; // Database name
$option['prefix'] = ''; // Database prefix (may be empty)
$db = JDatabase::getInstance( $option );
$result = $db->getQuery(true);
$result->select($db->quoteName(array(".$peopleinfo.")));
$result->from($db->quoteName('[redheadstepchild]'));
$db->setQuery($result);
$row = $db->loadRowList();
print_r($row);
编辑
这是开发控制台显示的内容
这一行有错误jquery-1.12.4.js:10254
这就是我点击它时的实际语法
// Do send the request
// This may raise an exception which is actually
// handled in jQuery.ajax (so no try/catch here)
xhr.send( ( options.hasContent && options.data ) || null );
我添加了一个 try and catch exception,这将 return 您的查询问题
$peopleinfo = implode(',',$_REQUEST['peopleinfo']);
$option = array(); //prevent problems
$option['driver'] = 'mssql'; // Database driver name
$option['host'] = 'Lockwood'; // Database host name
$option['user'] = 'root'; // User for database authentication
$option['password'] = 'sa'; // Password for database authentication
$option['database'] = 'test'; // Database name
$option['prefix'] = ''; // Database prefix (may be empty)
try{
$db = JDatabase::getInstance( $option );
$result = $db->getQuery(true);
$result->select($db->quoteName(array(".$peopleinfo.")));
$result->from($db->quoteName('[redheadstepchild]'));
$db->setQuery($result);
}catch(Exception $e){
echo $e->getMessage();
}
$row = $db->loadRowList();
print_r($row);
您好,我制作了一个模块来完成您正在做的事情,并且效果很好。我把例子放在这里可能对你有帮助。
模块:
<?php
defined('_JEXEC') or die;
include_once __DIR__ . '/helper.php';
// Instantiate global document object
$doc = JFactory::getDocument();
$js = <<<JS
(function ($) {
$(document).on('click', 'input[type=submit]', function () {
var value = $('input[name=data]').val(),
request = {
'option' : 'com_ajax',
'module' : 'ajax_search',
'data' : value,
'format' : 'raw'
};
$.ajax({
type : 'POST',
data : request,
success: function (response) {
$('.search-results').html(response);
}
});
return false;
});
})(jQuery)
JS;
$doc->addScriptDeclaration($js);
require JModuleHelper::getLayoutPath('mod_ajax_search');
?>
帮手:
<?php
defined('_JEXEC') or die;
class modAjaxSearchHelper
{
public static function getAjax()
{
include_once JPATH_ROOT . '/components/com_content/helpers/route.php';
$input = JFactory::getApplication()->input;
$data = $input->get('data', '', 'string');
$db = JFactory::getDbo();
$query = $db->getQuery(true);
// Build the query
$query
->select($db->quoteName(array('id','name','email')))
->from($db->quoteName('#__banner_clients'))
->where($db->quoteName('name') . ' LIKE '. $db->quote('%' . $data . '%'));
//->order('id ASC');
$db->setQuery($query);
$results = $db->loadObjectList();
// Get output
$output = null;
foreach($results as $result){
$output .= '<h4><a href="' . ContentHelperRoute::getArticleRoute($result->id, $result->email) . '">' . $result->name . '</a></h4>';
}
if($output == null or empty($data))
{
$output = 'Sorry! No results for your search.';
}
return $output;
}
}
?>