解析错误我无法弄清楚

时间:2013-07-31 01:37:40

标签: php mysql

我一直盯着这个错误超过一个小时了,我不能为我的生活看到错误。

这是我的错误:

解析错误:语法错误,意外'('在第42行的C:\ web \ account.php

这是我的代码(Line42以“$ searchstring”开头)在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

如果你能看到它,请告诉我!

非常感谢!

6 个答案:

答案 0 :(得分: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'] . "'";

答案 1 :(得分: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']) . "'";

答案 2 :(得分:1)

逃避双引号:

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

答案 3 :(得分: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

答案 4 :(得分:0)

我认为您需要添加句点字符才能正确连接字符串。

$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'] . " ";

答案 5 :(得分: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扩展