mrlankee Posted December 31, 2014 Share Posted December 31, 2014 Hi, I'm trying to figure this out.. I am creating a user search on my website thats connected to mysql. What I have does not work :\ <form action="useraccount.php" method="post"> <input type="text" name="username"/> <input type="submit" value="submit"/> </form> <?php $username = $_POST['nome']; $pdo = new PDO('mysql:host=myhost;dbname=mydb', 'user', 'pw'); if ($pdo->query("SELECT * from TABLENAME where username='".$username."'")->fetchColumn() > 0){ foreach($pdo->query("SELECT * from TABLENAME where username='".$username."'") as $row) { echo $row['Player_Name'] . $row['Time_Online']; } $pdo = null; } else{ echo "Sorry, that username does not exist in our database."; } ?> Quote Link to comment Share on other sites More sharing options...
Zane Posted December 31, 2014 Share Posted December 31, 2014 What do you mean it doesn't work? Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted December 31, 2014 Share Posted December 31, 2014 Actually it is working perfectly. It is doing exactly what it is suppose to do. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 31, 2014 Share Posted December 31, 2014 <input type="text" name="username"/> $username = $_POST['nome]; Quote Link to comment Share on other sites More sharing options...
mrlankee Posted December 31, 2014 Author Share Posted December 31, 2014 Sorry what i ment is .. If i enter username Alpha instead of Bravo.. it'll show Alpha's row's... it's basically drawing data from a game MySql Quote Link to comment Share on other sites More sharing options...
mrlankee Posted December 31, 2014 Author Share Posted December 31, 2014 www.rpproject.net/useraccount.php As you can see.. no matter what you enter.. the "else" does not show up as "Sorry, that username does not exist in our database.";" I cannot figure out whats going on.. exact code. <form action="account.php" method="GET"> In-Game Name:<br> <input type="text" name="username" value="Username"> <br> <input type="submit" value="Submit"> </form> <?php $username = $_POST['username']; $pdo = new PDO('mysql:host=xxxxx;dbname=xxxx', 'xxxx', 'xxxx'); if ($pdo->query("SELECT * from TABLENAME where username='".$username."'")->fetchColumn() > 0){ foreach($pdo->query("SELECT * from TABLENAME where username='".$username."'") as $row) { echo $row['Player_Name'] . $row['Time_Online']; } $pdo = null; } else{ echo "Sorry, that username does not exist in our database."; } ?> Any ideas? Quote Link to comment Share on other sites More sharing options...
hansford Posted January 1, 2015 Share Posted January 1, 2015 (edited) Sorry what i ment is .. If i enter username Alpha instead of Bravo.. it'll show Alpha's row's There is a problem in your logic. If you enter any value in the form that exists in the database - it's going to return that row's value because you asked it to with this select statement. SELECT * from TABLENAME where username='".$username."'" Edited January 1, 2015 by hansford Quote Link to comment Share on other sites More sharing options...
mrlankee Posted January 1, 2015 Author Share Posted January 1, 2015 how do i fix it? :\ lol Quote Link to comment Share on other sites More sharing options...
hansford Posted January 1, 2015 Share Posted January 1, 2015 Try: $username = $_POST['username']; $pdo = new PDO('mysql:host=xxxxx;dbname=xxxx', 'xxxx', 'xxxx'); $sql = "SELECT COUNT(*) from TABLENAME where username='".$username."'"; if ($result = $pdo->query($sql) { if ($result->fetchcolumn() > 0) { $sql = "SELECT * from TABLENAME where username='".$username."'"; foreach ($pdo->query($sql) as $row) { echo $row['Player_Name'] . ' ' . $row['Time_Online']; } } } else { echo "Sorry, that username does not exist in our database."; } Quote Link to comment Share on other sites More sharing options...
mrlankee Posted January 1, 2015 Author Share Posted January 1, 2015 (edited) Parse error: syntax error, unexpected '{' in ......."removed".../account.php on line 96 Which is the first { after "if" Edited January 1, 2015 by mrlankee Quote Link to comment Share on other sites More sharing options...
hansford Posted January 1, 2015 Share Posted January 1, 2015 Sorry, I can't test the code for errors and I don't know from your file which line the error occurred. Looking at it - I see: if ($result->fetchcolumn() > 0) fetchcolumn - should be fetchColumn with a capital 'C'. Quote Link to comment Share on other sites More sharing options...
mrlankee Posted January 1, 2015 Author Share Posted January 1, 2015 (edited) still shooting the error. Parse error: syntax error, unexpected '{' in ......."removed".../account.php on line 96 Edited January 1, 2015 by mrlankee Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 1, 2015 Share Posted January 1, 2015 Why do a fetchcolumn at all? Check the result of the operation, don't waste time on a retrieval of one piece of data. Of course if your db does not support rowcount() then stick with what you have, but here is how I would write your code: <form action="useraccount.php" method="post"> <input type="text" name="username"/> <input type="submit" value="submit"/> </form> <?php $username = $_POST['username']; // you retrieve form data using the html's name= attribute $pdo = new PDO('mysql:host=myhost;dbname=mydb', 'user', 'pw'); // assign your query statement to a var so you can view it if you need to debug it $q = "SELECT * from TABLENAME where username='$username'"; if (!$pdo->query($q) { echo "Query failed - query is $q";// only do this during development - not in prod. exit(); } if ($pdo->rowcount()== 0) { echo "Sorry, that username does not exist in our database."; exit(); } // now get your results while ($row = $pdo->fetch_assoc(PDO::FETCH_ASSOC)) { echo $row['Player_Name']," ",$row['Time_Online']; } exit(); Quote Link to comment Share on other sites More sharing options...
hansford Posted January 2, 2015 Share Posted January 2, 2015 ginerjm - posted the preferred way to code this - smooth, elegant - with error handling. Just test that rowCount() works on the particular DB you're using. I use rowCount(), but because of the manual "warning", I am in the habit of using fetchColumnt() - however, it is an extra query and slows things down and usually unnecessary. Quote Link to comment Share on other sites More sharing options...
mrlankee Posted January 2, 2015 Author Share Posted January 2, 2015 (edited) I switched up the code still getting the error Parse error: syntax error, unexpected '{' in /home/xxxxxxxxxxxxxxx/xxxxx/account.php on line 100 I did add a ?> to close out the PHP after the image was taken. Edited January 2, 2015 by mrlankee Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 2, 2015 Share Posted January 2, 2015 You're missing a right paren Quote Link to comment Share on other sites More sharing options...
mrlankee Posted January 2, 2015 Author Share Posted January 2, 2015 this is hurting my brain lol where i cannot figure it out? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 2, 2015 Share Posted January 2, 2015 The error message says unexpected { at line 100. That is because the line above doesn't have a ) at its end, thus making the { unexpected. My bad. Quote Link to comment Share on other sites More sharing options...
mrlankee Posted January 2, 2015 Author Share Posted January 2, 2015 ah =] thanks! I appreciate all the help! Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 2, 2015 Share Posted January 2, 2015 So are we any closer to a total solution? 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.