[英]Debugging MySQL query in PHP when called from other page
Page1 has an input form. Page1有一个输入表单。 I validate the input field with a JavaScript:
我使用JavaScript验证输入字段:
<input type="text" name="frmBrand" size="50" onkeyup="BrandCheck();" maxlength="100" id="frmBrand" />
<span id="frmBrand_Status">Enter existing or new brand</span>
In the JavaScript I then call a PHP script: 然后在JavaScript中,我调用一个PHP脚本:
function BrandCheck()
{
var jsBrandName = document.forms["AddPolish"]["frmBrand"].value;
if (jsBrandName !==null || jsBrandName !== "")
{
document.getElementById("frmBrand_Status").textContent = jsBrandName
// alert(jsBrandName);
var xmlhttp = new XMLHttpRequest();
var url = "CheckBrand.php";
var vars = "jsBrandName="+jsBrandName;
xmlhttp.open("POST",url,true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
var return_data = xmlhttp.responseText;
document.getElementById("frmBrand_Status").innerHTML = return_data;
}
}
xmlhttp.send(vars);
document.getElementById("frmBrand_Status").innerHTML = "processing.....";
}
}
So far so good. 到现在为止还挺好。 I do get results from the CheckBrand.php because it changes the frmBrand_Status.
我确实从CheckBrand.php获得结果,因为它更改了frmBrand_Status。 But I can't get any database results from the PHP page.
但是我无法从PHP页面获得任何数据库结果。
<?php
if(mysqli_connect_errno()) { //if connection database fails
echo("Connection not established ");
}
//by now we have connection to the database
else
{
if(isset($_POST['jsBrandName']))
{ //if we get the name succesfully
$jsBrandName = $_POST['jsBrandName'];
$dbBrandName = mysql_real_escape_string($jsBrandName);
if (!empty($dbBrandName))
{
$dbBrandName = $dbBrandName . "%";
$sqlQuery = "SELECT `BrandName` FROM `Brand` WHERE `BrandName` like '$dbBrandName' ORDER BY `BrandName`";
$result = mysqli_query($con, $sqlQuery);
$NumRows = mysqli_num_rows($result);
// $BrandName_result = mysql_fetch_row($BrandName_query);
echo "Result " . $dbBrandName . " ----- ". $jsBrandName . "Number rows " .$NumRows. " BrandName = " .$result. " SQL " .$sqlQuery;
if( $BrandName_result = mysql_fetch_row($BrandName_query))
{
While ($BrandName_result = mysql_fetch_row($BrandName_query))
{
echo "Brand = " .$BrandName_result[0];
}
}
}
else
{
echo "dbBrandName = empty" . $dbBrandName;
}
}
}
?>
When doing this, the html page shows the constant change of the normal variables. 这样做时,html页面显示普通变量的不断变化。 For example when the input field holds "Clu" I get the following output the span ID frmBrand_Status:
例如,当输入字段包含“ Clu”时,我得到以下输出,跨度ID frmBrand_Status:
Result Clu% ----- CluNumber rows BrandName = SQL SELECT `BrandName` FROM `Brand` WHERE `BrandName` like 'Clu%' ORDER BY `BrandName`
Which looks good as the brandname gets the %
appended, but the Number of rows is not shown (empty field?), the SQL Query is shown and looks good, but I don't get any results. 随商标名称添加
%
看起来不错,但是未显示“行数”(空字段?),显示了SQL Query,看起来不错,但没有任何结果。
And the if( $BrandName_result = mysql_fetch_row($BrandName_query))
section will not be reached, so there definitely is something going wrong in calling the query. 而且无法到达
if( $BrandName_result = mysql_fetch_row($BrandName_query))
部分,因此在调用查询时肯定出现了问题。
When I run that same query through PHPMyAdmin, i do get the result I expect, which is 1 row with a brandname. 当我通过PHPMyAdmin运行相同的查询时,确实得到了我期望的结果,该结果是带有商标名的1行。
I'm using firebug to try and troubleshoot the SQL Query, but I can't find where I can check this and I probably can't since PHP is serverside. 我正在使用Firebug尝试对SQL查询进行故障排除,但是我找不到可以检查的地方,因为PHP是服务器端的,所以我可能找不到。 correct?
正确? But how should I then trouble shoot this?
但是我该怎么解决呢?
Found what was wrong. 发现出了什么问题。 The $con string I was using to open the database was no longer available.
我用来打开数据库的$ con字符串不再可用。 On other pages in the site, the $con is available, I load the database using an include script on my index page.
在站点的其他页面上,$ con可用,我在索引页面上使用包含脚本加载数据库。 But it seems that the variable gets lost when it is called through the XMLHttpRequest().
但是似乎变量通过XMLHttpRequest()调用时会丢失。 Which is logical now I think of it, since this can also be a call to a remote server.
我现在认为这是合乎逻辑的,因为这也可以是对远程服务器的调用。 So my CheckBrand.php page was just missing the $con var to connect to the database.
所以我的CheckBrand.php页面只是缺少$ con var来连接数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.