2013-07-31 29 views
-2

我一直盯着這個錯誤已經有一個多小時了,而且我不能爲了我的生活看到什麼是錯的。解析錯誤我找不出

這裏是我的錯誤:

解析錯誤:語法錯誤,意外 '(' 在C:\網絡\ account.php上線42

這裏是我的代碼(Line42始於「$搜索字符串「)後的HTML中的第一個PHP的標籤。

<?php 

include_once 'header.php'; 
include_once 'functions.php'; 
require_once 'login_users.php'; 

$db_server = mysql_connect($db_hostname, $db_username, $db_password); 
if (!$db_server) die("Unable to connect to database:" . mysql_error()); 

mysql_select_db($db_database) 
    or die("Unable to find database:" . mysql_error()); 

?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 

<head> 
<meta content="en-us" http-equiv="Content-Language" /> 
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> 
<title>Weapon Build Creator</title> 

<link href="styles/main.css" rel="stylesheet" type="text/css" /> 

<style type="text/css"> 
.auto-style1 { 
    margin-top: 0px; 
} 
</style> 

</head> 

<body style="background-image: url('images/bg.jpg')"> 

<div id="form" style="left: 50%"> 
<div class="newsdiv"> 
    <br /> 
    <p class="title">MY BUILDS</p> 

<?php //search result table 



    $searchstring = "SELECT buildname,weapon,category,id,author,buildname FROM weapons WHERE author="($_SESSION['username'])" "; // Line 42 

$result = mysql_query($searchstring); 

if (!$result) die ("Database access failed: " . mysql_error()); 

$rows = mysql_num_rows($result); 

.... More code down here 

讓我知道如果你能看到它!

由於一噸!

+0

什麼是第42行?清楚地標記它。 –

+0

@SteelyDan - 你有很多答案。你真的應該把答案標記爲接受:-) – bestprogrammerintheworld

+0

沒有人說過它,所以我添加了強制性的:在php中的mysql函數是[棄用](http://php.net/manual/en/function。 mysql-db-query.php),請使用[MySqli](http://www.php.net/manual/en/book.mysqli.php)或[PDO](http://www.php.net/手冊/ en/ref.pdo-mysql.php)。並請使用[參數化查詢](http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php)。爲了您自己的安全。 – Whistletoe

回答

1

剛剛逃脫雙引號:

$searchstring = "SELECT buildname,weapon,category,id,author,buildname FROM weapons WHERE author=\"{$_SESSION['username']}\" "; 
+0

爲什麼要他爲什麼不應該跳過? –

+0

爲什麼不應該他?效果是一樣的。 – Ruben

+0

http://stackoverflow.com/questions/3316060/single-quotes-or-double-quotes-for-variable-concatenation –

0

我認爲你需要添加一段字符正確串接您的字符串。

I.e.

$searchstring = "SELECT buildname,weapon,category,id,author,buildname FROM weapons WHERE author=" . ($_SESSION['username']) . " "; 

括號是不實際需要要麼,所以你可能只是有:

$searchstring = "SELECT buildname,weapon,category,id,author,buildname FROM weapons WHERE author=" . $_SESSION['username'] . " "; 
2
$searchstring = "SELECT buildname,weapon,category,id,author,buildname FROM weapons WHERE author="($_SESSION['username'])" "; 

應該

$searchstring = "SELECT buildname,weapon,category,id,author,buildname FROM weapons WHERE author='" . $_SESSION['username'] . "'"; 
+0

這是做到了。非常感謝。我沒有意識到我必須爲查詢使用單引號。 – SteelyDan

+0

@SteelyDan - 不客氣!這只是解決它的一個選擇:-) – bestprogrammerintheworld

1

變化:

$searchstring = "SELECT buildname,weapon,category,id,author,buildname FROM weapons WHERE author="($_SESSION['username'])" "; // Line 42 

收件人:

$searchstring = "SELECT buildname,weapon,category,id,author,buildname FROM weapons WHERE author='".$_SESSION['username']."'"; // Line 42 

我做了什麼?我已經刪除括號並使用了連接,查看此鏈接以獲得串聯幫助:http://phphowto.blogspot.co.uk/2006/12/concatenate-strings.html

+0

謝謝,這工作完美。感謝您的參考。 – SteelyDan

+0

@SteelyDan這只是我從谷歌搜索中脫穎而出的第一個鏈接。搜索條件:「PHP字符串連接」應該提供更廣泛的教程/協助指南 –

2

您在sql中有未轉義的引號。在雙引號內使用單引號或使用dot連接。

$searchstring = "SELECT buildname,weapon,category,id,author,buildname FROM weapons WHERE author='($_SESSION['username'])'"; // i don't know why you used `(` to wrap username 

$searchstring = "SELECT buildname,weapon,category,id,author,buildname FROM weapons WHERE author='" . ($_SESSION['username']) . "'"; 
0

試試這個

$username= mysql_real_escape_string($_SESSION['username']); 
//You should scapes the variable, if the name was O'relly you get an error in sql syntax 

$searchstring = " 
SELECT buildname,weapon,category,id,author,buildname 
FROM weapons 
WHERE author='$username' "; // on Line 42 

個人而言,我更喜歡的變量,單引號,以避免內部雙引號 「\」。

注意:從PHP 5.5.0開始,不推薦使用mysql_ *擴展名,並且將來會被刪除。相反,應該使用MySQLi或PDO_MySQL擴展