php mySQL返回一个空查询

时间:2013-07-24 15:43:00

标签: php mysql

我正在尝试创建一个结算历史记录页面,用户可以在其中以表格格式查看包含购买历史记录的表格。我正在尝试添加每行的链接,以便用户可以查看每个订单的完整发票详细信息。

当他们点击链接时(我在此脚本之后包含了概述页面的脚本),这个脚本应该执行查询,但我只是得到一个空消息查询。

有人能发现错误吗?

谢谢!

欧仁妮

<?php

include("mainfile.php");
include(XOOPS_ROOT_PATH."/header.php");


$host="localhost"; // Host name 
$username="user"; // Mysql username 
$password="pass"; // Mysql password 
$db_name="db"; // Database name 
$tbl_name="table"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$TicketID=$_GET['TicketID'];

// Retrieve data from database 
$sql="SELECT * FROM $tbl_name WHERE TicketID='$TicketID'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);


// Get a specific result from the "example" table
$result = mysql_query($Sql) or die(mysql_error());


echo "<table width='100' border='1' cellpadding='0' cellspacing='0' id='records'>";
print "<h3 align='center'><strong>Billing History</strong></h3><p>";
echo "<tr>  
<th width='110' align='center'>Billing Date</th>
<th width='80' align='center'>Ticket #</th>  
<th align='center'>Project Title </th>
<th width='80' align='center'>Total GBP</th>



</tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr height='22' width='100' bordercolor='343434' align='center'><td>"; 
echo $row['date'];
echo "</td><td> "; 
echo $row['TicketID'];

echo "</td><td> "; 
echo $row['project'];
echo "</td><td> "; 
echo $row['grandTotal'];


} 

echo "</table>";

include(XOOPS_ROOT_PATH."/footer.php");

?>

这是显示完整帐单历史记录的脚本,其中包含执行查询的脚本的链接(上图)。

<?php

include("mainfile.php");
include(XOOPS_ROOT_PATH."/header.php");


$host="localhost"; // Host name 
$username="username"; // Mysql username 
$password="password"; // Mysql password 
$db_name="db name"; // Database name 
$tbl_name="tbl name"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name  WHERE uid='";
$sql=$sql .  $xoopsUser->uid("s") . "'  AND Paid='Y'";


$result=mysql_query($sql);
?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>List data from mysql </strong> </td>
</tr>

<tr>
<td align="center"><strong>Billing Date</strong></td>
<td align="center"><strong>Invoice Number</strong></td>
<td align="center"><strong>Description</strong></td>
<td align="center"><strong>Total GBP</strong></td>
<td align="center"><strong>View</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td><? echo $rows['date']; ?></td>
<td><? echo $rows['TicketID']; ?></td>
<td><? echo $rows['project']; ?></td>
<td><? echo $rows['grandTotal']; ?></td>


<td align="center"><a href="http://website.co.uk/site/viewInvoice.php?TicketID=<? echo     $rows['TicketID']; ?>">View</a></td>
</tr>

<?php
}
?>

</table>
</td>
</tr>
</table>

<?php
mysql_close();
include(XOOPS_ROOT_PATH."/footer.php");
?>    

4 个答案:

答案 0 :(得分:2)

改变这个:

// Get a specific result from the "example" table
$result = mysql_query($Sql) or die(mysql_error());

到此:

$result = mysql_query($sql) or die(mysql_error());
在viewInvoice.php中

还要看一下其他关于安全等的评论。

答案 1 :(得分:0)

检查这些行:

// Retrieve data from database 
$sql="SELECT * FROM $tbl_name WHERE TicketID='$TicketID'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);

// Get a specific result from the "example" table
$result = mysql_query($Sql) or die(mysql_error());

您正在使用$result的返回值覆盖mysql_query变量,该值会获取不存在的参数:$Sql

PHP变量名称区分大小写!

答案 2 :(得分:0)

更改此

<a href="http://website.co.uk/site/viewInvoice.php?TicketID=<? echo $rows['TicketID']; ?>">View</a></td>

到这个

<?php 
echo "href=http://website.co.uk/site/viewInvoice.php?TicketID=" . $rows['TicketID'] . ">View</a>";
?>

答案 3 :(得分:-1)

<?php

include("mainfile.php");
include(XOOPS_ROOT_PATH."/header.php");


$host="localhost"; // Host name 
$username="user"; // Mysql username 
$password="pass"; // Mysql password 
$db_name="db"; // Database name 
$tbl_name="table"; // Table name

mysql_connect($host, $username, $password)or die("cannot connect"); 
mysql_select_db($db_name)or die("cannot select DB");

// get value of id that sent from address bar
$TicketID=$_GET['TicketID'];

// Retrieve data from database 
$sql="SELECT * FROM ' ".$tbl_name."'  WHERE `TicketID`='".$TicketID."'";
$result  = mysql_query($sql);



echo "<table width='100' border='1' cellpadding='0' cellspacing='0' id='records'>";
print "<h3 align='center'><strong>Billing History</strong></h3><p>";
echo "<tr>  
<th width='110' align='center'>Billing Date</th>
<th width='80' align='center'>Ticket #</th>  
<th align='center'>Project Title </th>
<th width='80' align='center'>Total GBP</th></tr>";
// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array($result)) {
    // Print out the contents of each row into a table
    echo "<tr height='22' width='100' bordercolor='343434' align='center'><td>"; 
    echo $row['date'];
    echo "</td><td> "; 
    echo $row['TicketID'];

    echo "</td><td> "; 
    echo $row['project'];
    echo "</td><td> "; 
    echo $row['grandTotal'];
} 

echo "</table>";

include(XOOPS_ROOT_PATH."/footer.php");

?>

第二个脚本:

<?php

include("mainfile.php");
include(XOOPS_ROOT_PATH."/header.php");


$host="localhost"; // Host name 
$username="username"; // Mysql username 
$password="password"; // Mysql password 
$db_name="db name"; // Database name 
$tbl_name="tbl name"; // Table name 

// Connect to server and select database.
mysql_connect($host, $username, $password)or die("cannot connect"); 
mysql_select_db($db_name)or die("cannot select DB");

$sql="SELECT * FROM `".$tbl_name."`  WHERE `uid`='".$xoopsUser->uid("s"). "'  AND `Paid`='Y'";
$result=mysql_query($sql);
?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>List data from mysql </strong> </td>
</tr>

<tr>
<td align="center"><strong>Billing Date</strong></td>
<td align="center"><strong>Invoice Number</strong></td>
<td align="center"><strong>Description</strong></td>
<td align="center"><strong>Total GBP</strong></td>
<td align="center"><strong>View</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td><? echo $rows['date']; ?></td>
<td><? echo $rows['TicketID']; ?></td>
<td><? echo $rows['project']; ?></td>
<td><? echo $rows['grandTotal']; ?></td>


<td align="center"><a href="http://website.co.uk/site/viewInvoice.php?TicketID=<? echo $rows['TicketID']; ?>">View</a></td>
</tr>

<?php
}
?>

</table>
</td>
</tr>
</table>

<?php
mysql_close();
include(XOOPS_ROOT_PATH."/footer.php");
?> 

SRY。愚蠢的我。 试试..