我多年来一直试图解决这个问题,但我不能这样做。
$sqluser = "--";
$sqlpass = "--";
$hostname = "localhost";
$clientuser = $_COOKIE['user'];
//connection to the database
$dbhandle = mysql_connect($hostname, $sqluser, $sqlpass)
or die("Unable to connect to MySQL");
//select a database to work with
$selected = mysql_select_db("clhtechn_si1",$dbhandle)
or die("Could not select database!");
//execute the SQL query
$idfinder = mysql_query("SELECT id FROM `si_customers` WHERE username='$clientuser'") or die(mysql_error());
$clientid = mysql_fetch_assoc($idfinder);
$query = "SELECT invoice_id, date, SUM(total) as invoiceTotal
FROM si_invoices
LEFT JOIN si_invoice_items ON si_invoices.id = si_invoice_items.invoice_id
WHERE si_invoices.customer_id='$clientid'
GROUP BY invoice_id
ORDER BY invoice_id";
$result = mysql_query($query);
$row=0;
while ($record = mysql_fetch_assoc($result))
{
$class = 'd' . ($row % 2);
$inv_date=substr($record['date'], 0, -9);
$inv_total=$record['invoiceTotal'];
$invoice_total=number_format($inv_total, 2);
echo "<tr class=\"{$class}\">";
echo "<td valign='top'>{$inv_date}</td>";
echo "<td valign='top'>{$record['invoice_id']}</td>";
echo "<td valign='top'>$invoice_total</td>";
echo "<td><a href='invoices/Invoice_{$record['invoice_id']}.pdf' target='_blank'>";
echo "<img src='urlhere' width='17' height='17' alt='PDF File' border='0' /></a></td>";
echo "</tr>";
$row++;
}
//close the connection
mysql_close($dbhandle);
以下是我的customers表格的对齐方式: Link to picture.
我的代码有什么问题,我可以使用哪些代码来修复它? 感谢帮助。
答案 0 :(得分:1)
$ clientid是一个数组,因为mysql_fetch_assoc()返回此类型。
$row = mysql_fetch_assoc($idfinder);
$clientid = $row['id'];
另外,您需要在将$ clientuser放入查询之前正确格式化它 所以,完整的代码将是
$user = mysql_real_escape_string($_COOKIE['user']);
$sql = "SELECT id FROM `si_customers` WHERE username='$user'";
$res = mysql_query($sql) or trigger_error(mysql_error());
$row = mysql_fetch_assoc($res);
$clientid = $row['id'];
为了使它成为你想要的方式,你必须使用一些抽象库
你将把你的id放在一行(实际上只是为了便于阅读,它是2行)
$sql = "SELECT id FROM si_customers WHERE username=?s";
$clientid = $db->getOne($sql,$_COOKIE['user']);
您可以在此处看到此类Mysql abstraction library的示例。