Jump to content

raneyron

New Members
  • Posts

    6
  • Joined

  • Last visited

Everything posted by raneyron

  1. I think I got it working everyone. Thanks. I searched the error and found a solution on another website. Instead of this: $user = $statement->fetch_assoc(); I used this bit of code: $row = $statement->fetch(PDO::FETCH_ASSOC); I think it is working but I need to figure out the code needed to set variables for each of the fields in the record. For example: $startDate = $row['startDate']; $mealsDay = $row['mealsDay']; $caloriesMeal = $row['caloriesMeal']; $costMeal = $row['costMeal'];
  2. Thanks Psycho! If I am correct, I am actually now using PDO. I am getting this error: Fatal error: Call to undefined method PDOStatement::fetch_assoc() in/home5/aihreaco/public_html/quitApp/private.php on line 74
  3. Updated code: error_reporting(E_ALL); ini_set('display_errors', 1); $userID = $_SESSION['user']['username']; echo $userID . "<br/>"; $query = " SELECT username, startDate, mealsDay, caloriesMeal, costMeal FROM diet_info WHERE username = :userID "; $statement = $db->prepare($query); $statement->bindValue(':userID', $userID); $statement->execute(); //$info = $statement->fetchAll(); $row_count = $statement->rowCount(); echo $row_count . "<br/>"; $statement->closeCursor(); while($row = $statement->fetch_assoc()){ //Do something with $row $startDate = $row['startDate']; $mealsDay = $row['mealsDay']; $caloriesMeal = $row['caloriesMeal']; $costMeal = $row['costMeal']; } echo $startDate . "<br/>"; echo $mealsDay . "<br/>"; echo $caloriesMeal . "<br/>"; echo $costMeal . "<br/>"; The row count returns "1" so that is correct for one record, right? I have one error when I run this code: Fatal error: Call to undefined method PDOStatement::fetch_assoc() in/home5/aihreaco/public_html/quitApp/private.php on line 60
  4. Thanks cyberRobot. I have made sure that the $userID is returning correctly. I did not have a $row_count returned at all, so I think the big problem was getting the connection made to the database. I do not have error code yet. Here is my code so far this morning after reading these replies (remember I am using fake database items): $userID = $_SESSION['user']['username']; echo $userID . "<br/>"; $query = " SELECT username, startDate, mealsDay, caloriesMeal, costMeal FROM diet_info WHERE username = :userID "; $statement = $db->prepare($query); $statement->bindValue(':userID', $userID); $statement->execute(); $info = $statement->fetchAll(); $row_count = $statement->rowCount(); echo $row_count . "<br/>"; $statement->closeCursor(); while($row = $info->fetch_assoc() { //Do something with $row $startDate = $row['startDate']; $mealsDay = $row['mealsDay']; $caloriesMeal = $row['caloriesMeal']; $costMeal = $row['costMeal']; } } echo $startDate . "<br/>"; echo $mealsDay . "<br/>"; echo $caloriesMeal . "<br/>"; echo $costMeal . "<br/>";
  5. Barand, well said. I wasn't clear about the results. The problem is I get a blank page. Psycho, my results are that nothing is happening. I will try to brush up on PDO and prepared statements. I will look into how to check for errors. I will try the while loop. Wezhind, thanks for the tips about string vs. integer and using the code tags. If anyone has any good links about this specific topic of extracting data from a MySQL database in PHP, I would appreciate it. I really just need to see some code that works so I can learn from it. I have found a whole lot of examples online that just didn't work properly.
  6. Hello. I'm a newbie so sorry if this isn't the best forum to post my problem. I am using a MySQL and PHP to create a web app. I have authentication, and I can register users. I also have a form that users provide information and it is successfully inserting data into a table in my database. I will use fictional fields for my database table called meal_info: username dateStartedDiet numberMealsPerDay costPerMeal Problem: Select user-specific data from the MySQL database, using Session username to select only the current user's data, then display it and do some calculations. Here is thecode at the top, and I am fairly sure it's working: session_start(); //execute commone code require("common.php"); //includes code to connect to database, etc. if(empty($_SESSION['user'])) { // If they are not, we redirect them to the login page. header("Location: login.php"); // Remember that this die statement is absolutely critical. Without it, // people can view your members-only content without logging in. die("Redirecting to login.php"); } Here is the part of the code that has to do with displaying user data: $userID = $_SESSION['user']['username']; //create a variable that is the session username which is identical to the field in our MySQL table $query = "SELECT * FROM meal_info WHERE username = $userID"; //our SELECT statement $result = db->query($query); //execute the query $row_count = $result->num_rows;//count the rows in the table and place in variable to use in incremental loop code for ($i = 0; $i < $row_count; $i++) : $row = $result->fetch_assoc(); //for each row in the table, fetch and create and array $dateStart = $row['dateStartedDiet']; $numberMeals = $row['numberMealsPerDay']; $costMeal = $row['costPerMeal']; echo $dateStart; echo $numberMeals; echo $costMeal;
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.