skateme Posted October 3, 2008 Share Posted October 3, 2008 I'm having a lot of trouble displaying a value from a database in one of my input fields. My code is: <?php session_start(); print "<pre>";print_r($_SESSION);print "</pre>"; ?> <html> <head> <title>My Account<title> </head> <body> <?php $con = mysql_connect("localhost", "admin", "adminpass") or die("Could not connect to database"); $database = mysql_select_db("db") or die("<br />Could not select database"); $username = $_SESSION['username']; $query=sprintf("select email,location from accountinfo where username='%s'",mysql_real_escape_string($username)); $result=mysql_query($query); while($row=mysql_fetch_array($result)) { $email=$row['email']; $location=$row['location']; } ?> <p>Modify account information:</p> <form action="modifyaccount.php" method="post"> Username: <input type="text" readonly="readonly" value="<?php echo $username; ?>" /><br /> Password: <input type="password" name="password" id="password" /><br /> Email: <input type="text" name="email" id="email" value="<?php echo $email; ?>" /><br /> Location: <input type="text" name="location" id="location" value="<?php echo $location; ?>" /><br /> <input type="submit" value="Modify" /> </form> </body> </html> and the error I get is: Parse error: syntax error, unexpected T_VARIABLE in /Library/WebServer/Documents/MDR/myaccount.php on line 27 Line 27 is basically the whole while loop.: while($row = mysql_fetch_array($result)) { $email = $row['email']; $location = $row['location']; } What am I doing wrong? Thanks in advance! Quote Link to comment Share on other sites More sharing options...
AV1611 Posted October 3, 2008 Share Posted October 3, 2008 is there more than one return? while{ suggests there is more than one return. If{ would be better if you expect only one return. Also, select email,location from accountinfo where username='%s' is bad. % is a wildcard, and you have to use like not = but you want a single return, so you need a better query... Finally, just for fun do: )); $result=mysql_query($query) or die('Croaked: '.mysql_error()); and see if the query is valid. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 3, 2008 Share Posted October 3, 2008 Actually using %s is allowed since the OP is using sprintf(). But, as you said, why are using a while loop OP when you would surely only be returning 1 row from the database. Just perform mysql_fetch_array() outside the loop then set the variables. Quote Link to comment Share on other sites More sharing options...
skateme Posted October 3, 2008 Author Share Posted October 3, 2008 Do you mean something like this: $query = sprintf("select email from userinfo where username='%s' ", mysql_real_escape_string($username)); $locquery = sprintf("select location from userinfo where username='%s' ", mysql_real_escape_string($location)); $result = mysql_query($query); $locresult = mysql_query($locquery); $email = mysql_fetch_array($result); $location = mysql_fetch_array($locresult); while($result=mysql_query($query)) { $email = $row['email']; }; while($locresult=mysql_query($locresult)) { $location = $row['location']; }; I get this error when I use the code above: Parse error: syntax error, unexpected T_VARIABLE in /Library/WebServer/Documents/MDR/myaccount.php on line 25 Line 25 is the while loop again. Quote Link to comment Share on other sites More sharing options...
skateme Posted October 4, 2008 Author Share Posted October 4, 2008 Still need help guys Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 4, 2008 Share Posted October 4, 2008 Try this: $query = sprintf("SELECT email, location FROM userinfo WHERE username='%s'", mysql_real_escape_string($username)); $result = mysql_query($query) or die("Error: ".mysql_error()); $data = mysql_fetch_array($result); $email = $data['email']; $location = $data['location']; echo "Email: " . $email . "<br />Location: " . $location; Quote Link to comment Share on other sites More sharing options...
skateme Posted October 4, 2008 Author Share Posted October 4, 2008 Thanks! That worked for the most part. I get an undefined function error: Fatal error: Call to undefined function  sprintf() in /Library/WebServer/Documents/MDR/myaccount.php on line 7 My code: <?php session_start(); print "<pre>".$_SESSION."</pre>"; $con = mysql_connect("localhost", "admin", "adminpass") or die("Could not connect to database"); $database = mysql_select_db("db") or die("<br />Could not select database"); $username = $_SESSION['username']; $query = sprintf("SELECT email, location FROM accountinfo WHERE username = '.$username.'", mysql_real_escape_string($username), $con); $result = mysql_query($query); $data = mysql_fetch_array($result); $email = $data['email']; $location = $data['location']; ?> <html> <head> <title>My Account<title> </head> <body> <p>Modify account information:</p> <form action="modifyaccount.php" method="post"> Username: <input type="text" readonly="readonly" value="<?php echo $username; ?>" /><br /> Password: <input type="password" name="password" id="password" /><br /> Email: <input type="text" name="email" id="email" value="<?php echo $email; ?>" /><br /> Location: <input type="text" name="location" id="location" value="<?php echo $location; ?>" /><br /> <input type="submit" value="Modify" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 4, 2008 Share Posted October 4, 2008 Hm, well try this then: <?php session_start(); echo "<pre>", print_r($_SESSION), "</pre>"; $con = mysql_connect("localhost", "admin", "adminpass") or die("Could not connect to database"); $database = mysql_select_db("db") or die("<br />Could not select database"); $username = $_SESSION['username']; $query = "SELECT email, location FROM accountinfo WHERE username = '".mysql_real_escape_string($username)."'"; $result = mysql_query($query) or die(mysql_error()); $data = mysql_fetch_array($result); $email = $data['email']; $location = $data['location']; ?> <html> <head> <title>My Account<title> </head> <body> <p>Modify account information:</p> <form action="modifyaccount.php" method="post"> Username: <input type="text" readonly="readonly" value="<?php echo $username; ?>" /><br /> Password: <input type="password" name="password" id="password" /><br /> Email: <input type="text" name="email" id="email" value="<?php echo $email; ?>" /><br /> Location: <input type="text" name="location" id="location" value="<?php echo $location; ?>" /><br /> <input type="submit" value="Modify" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
skateme Posted October 4, 2008 Author Share Posted October 4, 2008 I'm still getting another error message on line 7. Line 7 is the $query line: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /Library/WebServer/Documents/MDR/myaccount.php on line 7 That error disappears when I remove the quotation marks around SELECT email.., but I get new error saying: Parse error: syntax error, unexpected ',' in /Library/WebServer/Documents/MDR/myaccount.php on line 7 When I remove ",location" from the query, the new error I get is: Parse error: syntax error, unexpected '=' in /Library/WebServer/Documents/MDR/myaccount.php on line 7 Quote Link to comment 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.