Darkranger85 Posted February 14, 2013 Share Posted February 14, 2013 hey guys, I'm trying to switch my mysql connections over to PDO but I'm having a hard time. I've read a section on it in the php book I'm reading, I watched the phpacademy,org tutorial on it, I've read the section on it here at w3schools and I can't get it working. I've also posted it on 2 other forums and no one has yet to figure out where the problem is and it's eaten up 2 days of my time as well. If I can't get it figured out today I'm seriously thinking of going back to using the old mysql functions because they worked right the first time and have never given me an ounce of trouble. It's just very frustrating that my registration/login system was working perfect and then I upgrade and nothing works and no one can figure it out. . . It says the error is on line 142, which is the fetchAll() function line. Thanks for any help you guys can offer! Here is the connection script <?php// Connects to Our Database$db_host = "localhost";$db_user = "root";$db_password = "";$db_database = "game_db";try{ $conn = new PDO ('mysql:host=localhost;dbname=game_db', $db_user, $db_password);$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);} catch (PDOException $e) { echo 'ERROR: ' . $e->getMessage();}?> And the query thats giving me the trouble $sql = "INSERT INTO `player_data` (`username`, `empire_name`, `ruler_name`, `founding_group`, `government`, `population`, `currency`, `p_currency`, `scrap`, `food`, `fuel`, `salvage_wpn`, `scrap_wpn`, `morale`, `initiative`, `influence`, `farms`, `housing`, `scrap_yards`, `barracks`, `taverns`, `scouts`, `militia`, `infantry`, `heavy_infantry`, `technicals`, `tanks`, `gate`, `tower`, `wall`, `tunnels`, `dogs`) VALUES (:user, :empire_name, :ruler_name, :founding_group, :government, 25, 1000, 100, 1000, 1000, 0, 0, 5, 10, 100, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)"; $query = $conn->prepare($sql); $query->bindValue(':user', $user, PDO::PARAM_STR); $query->bindValue(':empire_name', $empire_name, PDO::PARAM_STR); $query->bindValue(':ruler_name', $ruler_name, PDO::PARAM_STR); $query->bindValue(':founding_group', $founding_group, PDO::PARAM_STR); $query->bindValue(':government', $government, PDO::PARAM_STR); try { $query->execute(); } catch (Exception $e) { exit($e); } $row_count = $query->fetchAll(PDO::FETCH_ASSOC); Link to comment Share on other sites More sharing options...
justsomeguy Posted February 14, 2013 Share Posted February 14, 2013 What's the error message? Link to comment Share on other sites More sharing options...
Darkranger85 Posted February 14, 2013 Author Share Posted February 14, 2013 PDOException: SQLSTATE[HY000]: General error in C:\wamp\www\Projects\Planet Ruin\php\regval.php on line 142 Link to comment Share on other sites More sharing options...
thescientist Posted February 14, 2013 Share Posted February 14, 2013 (edited) what are the surrounding lines? ideally with a comment indicating line 142 Edited February 14, 2013 by thescientist Link to comment Share on other sites More sharing options...
justsomeguy Posted February 14, 2013 Share Posted February 14, 2013 Hmm, I was hoping for more than that, if you search for that error message you can see that it should also return the actual error code and message from MySQL: http://www.google.com/search?client=opera&q=PDOException:+SQLSTATE%5BHY000%5D:+General+error Instead of sending the exception to exit, which will cast it as a string, handle it like the exception when you connect and use getMessage. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 14, 2013 Share Posted February 14, 2013 Hold on, if the error is on the "fetchAll" line, it's probably because you're running an insert query. An insert query doesn't return anything to fetch. 1 Link to comment Share on other sites More sharing options...
Darkranger85 Posted February 15, 2013 Author Share Posted February 15, 2013 Wow. . . I was watching the tutorial and I just ended up adding it to the mysql statement I had up at the time, which was the wrong one. I should have noticed that obviously an inset query wouldn't return rows. . . Now that thats finished, could you guys help me out getting the hang of this. I'm trying to "translate" this code into PDO but I'm having a hard time getting the hang of accessing the array that is returned. $query_select = "SELECT 'username', 'empire_name', 'ruler_name' FROM `player_data` WHERE `username`='$user'"; $result_select = mysql_query($query_select); $rows_select = mysql_fetch_assoc($result_select); $num_rows_select = mysql_num_rows($result_select); $val_empire = $rows_select['empire_name']; $val_ruler = $rows_select['ruler_name']; if ($num_rows_select === 1 || !empty($rows_select['username'])){ $errors[] = "Username is already taken."; } if ($val_empire === $empname){ $errors[] = "Empire name is already taken."; if ($val_ruler === $ruler){ $errors[] = "Ruler name is already taken."; } } Link to comment Share on other sites More sharing options...
justsomeguy Posted February 15, 2013 Share Posted February 15, 2013 That's where you would use fetchAll, check the tutorial here for examples: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now