sax0nNZ Posted August 12, 2009 Share Posted August 12, 2009 So I am trying to use a session variable as one of the values in my mysql query but it has spaces in it and I'm pretty sure that's what's causing the problem. So how do I get it to treat the spaces as extra characters in the value. All help greatly apreciated!!! Quote Link to comment https://forums.phpfreaks.com/topic/169891-solved-mysql-query-help/ Share on other sites More sharing options...
Adam Posted August 12, 2009 Share Posted August 12, 2009 Show us your code... If the value's contained within quotes it shouldn't matter if it there's spaces. Quote Link to comment https://forums.phpfreaks.com/topic/169891-solved-mysql-query-help/#findComment-896262 Share on other sites More sharing options...
sax0nNZ Posted August 12, 2009 Author Share Posted August 12, 2009 If this code looks really stupid then I'm sorry, I'm still learning... <?php session_start(); ?> <html> <head><title> test </title> <link rel="stylesheet" type="text/css" href="style1.css" /> </head> <body> <?php require ('mysql_connect.php'); include('header.php'); ?> <div class='testbody'> <?php echo $_GET['gig']; if (isset($_SESSION['currentgig'])) echo "You are already at a gig! <br> <a href='/battle.php'> Continue... </a>"; else { $_SESSION['currentgig']=$_GET['gig']; echo $_SESSION['currentgig']; $result = mysql_query("SELECT '$_SESSION[`currentgig`]' FROM $gigstable ") or die(mysql_error()); $_SESSION['battlegig'] = mysql_fetch_array($result); $result2 = mysql_query("SELECT '$_SESSION[`username`]' FROM userdata"); $_SESSION['playerstats'] = mysql_fetch_array($result2); echo "Get ready to rock out!!! <br> <a href='/battle.php'> Continue... </a>"; } ?> </body> </html> The error I get is: Parse error: syntax error, unexpected '`', expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/vol1/byethost32.com/b32_3527373/htdocs/gigbattle.php on line 18 Quote Link to comment https://forums.phpfreaks.com/topic/169891-solved-mysql-query-help/#findComment-896268 Share on other sites More sharing options...
deansatch Posted August 12, 2009 Share Posted August 12, 2009 I notice you don't have your {} for your if statement Quote Link to comment https://forums.phpfreaks.com/topic/169891-solved-mysql-query-help/#findComment-896271 Share on other sites More sharing options...
deansatch Posted August 12, 2009 Share Posted August 12, 2009 and should: "SELECT '$_SESSION[`currentgig`]' FROM $gigstable " not be "SELECT '".$_SESSION['currentgig']."' FROM $gigstable " ?? Quote Link to comment https://forums.phpfreaks.com/topic/169891-solved-mysql-query-help/#findComment-896273 Share on other sites More sharing options...
Adam Posted August 12, 2009 Share Posted August 12, 2009 The problem is with: '$_SESSION[`currentgig`]' Indexes must have single or double quotes around them, not the 'grave accent'. There's a few variations you could change this to, for example: '".$_SESSION['currentgig']."' There's another occurrence of this a few lines down by the way. Edit: Deansatch beat me too it! Edited again: Didn't realise at first the value was for a table name, should be: `".$_SESSION['currentgig']."` I may be wrong but I don't think you can create tables with spaces? Quote Link to comment https://forums.phpfreaks.com/topic/169891-solved-mysql-query-help/#findComment-896274 Share on other sites More sharing options...
Adam Posted August 12, 2009 Share Posted August 12, 2009 I may be wrong but I don't think you can create tables with spaces? Oh, would seem you can! Quote Link to comment https://forums.phpfreaks.com/topic/169891-solved-mysql-query-help/#findComment-896278 Share on other sites More sharing options...
sax0nNZ Posted August 12, 2009 Author Share Posted August 12, 2009 I tried that before, it gave me this: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/169891-solved-mysql-query-help/#findComment-896281 Share on other sites More sharing options...
Adam Posted August 12, 2009 Share Posted August 12, 2009 Could you post the code exactly as you have it while it's generating that error. Quote Link to comment https://forums.phpfreaks.com/topic/169891-solved-mysql-query-help/#findComment-896284 Share on other sites More sharing options...
sax0nNZ Posted August 12, 2009 Author Share Posted August 12, 2009 <?php session_start(); ?> <html> <head><title> test </title> <link rel="stylesheet" type="text/css" href="style1.css" /> </head> <body> <?php require ('mysql_connect.php'); include('header.php'); ?> <div class='testbody'> <?php echo $_GET['gig']; if (isset($_SESSION['currentgig'])) echo "You are already at a gig! <br> <a href='/battle.php'> Continue... </a>"; else { $_SESSION['currentgig']=$_GET['gig']; echo $_SESSION['currentgig']; $result = mysql_query("SELECT `".$_SESSION['currentgig']."` FROM $gigstable ") or die(mysql_error()); $_SESSION['battlegig'] = mysql_fetch_array($result); $result2 = mysql_query("SELECT `".$_SESSION['username']."` FROM userdata"); $_SESSION['playerstats'] = mysql_fetch_array($result2); echo "Get ready to rock out!!! <br> <a href='/battle.php'> Continue... </a>"; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/169891-solved-mysql-query-help/#findComment-896285 Share on other sites More sharing options...
Dathremar Posted August 12, 2009 Share Posted August 12, 2009 I recommend You print out the sql string b4 you execute it, something like: $string = "SELECT ".$_SESSION['currentgig'." FROM $gigstable"; echo $string; Just to see what's the query and what are You missing. Quote Link to comment https://forums.phpfreaks.com/topic/169891-solved-mysql-query-help/#findComment-896288 Share on other sites More sharing options...
deansatch Posted August 12, 2009 Share Posted August 12, 2009 And maybe you need some backticks on the other variable e.g.`$gigstable` In fact I don't actually see where $gigstable is declared. Quote Link to comment https://forums.phpfreaks.com/topic/169891-solved-mysql-query-help/#findComment-896293 Share on other sites More sharing options...
sax0nNZ Posted August 12, 2009 Author Share Posted August 12, 2009 Just about to come here and post but deansatch beat me to it . I have forgoten to declare $gigstable. Thanks guys! Couldn't have figured it out without you! Quote Link to comment https://forums.phpfreaks.com/topic/169891-solved-mysql-query-help/#findComment-896296 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.