safety Posted September 18, 2012 Share Posted September 18, 2012 Hello. First I'd like to say that I'm a beginner with php and mySQL. I've been learning from scratch with the help of a book called "PHP for absolute beginners". I've made good progress up until now I'm stuck on a chapter because I'm getting errors. I'm trying to make a fairly basic (not for me) way to display information stored in the SQL database. Here's the code, followed by a few of the errors I'm getting. I'd really appreciate some help as I'm at a dead end. http://pastebin.com/zbggBcN9 These are the errors I'm getting Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\blues\inc\functions.inc.php on line 29 Notice: Undefined variable: e in C:\xampp\htdocs\blues\inc\functions.inc.php on line 42 Thanks in advance for any help Quote Link to comment https://forums.phpfreaks.com/topic/268524-php-problem/ Share on other sites More sharing options...
Barand Posted September 18, 2012 Share Posted September 18, 2012 foreach is for processing arrays. You have a query resultset there. It needs to be lie this $sql = "SELECT id, title FROM entries ORDER BY created DESC"; $result = $db->query($sql); // Loop through returned results and store as an array while ($db->fetch_assoc($result)) // check manual - I'm guessing at your class method here { $e[] = $row; } 2nd error - $e does not get defined until line 46. Instead of is_array() use isset() Quote Link to comment https://forums.phpfreaks.com/topic/268524-php-problem/#findComment-1379049 Share on other sites More sharing options...
safety Posted September 18, 2012 Author Share Posted September 18, 2012 thanks for replying. I'm now getting this error: Fatal error: Call to undefined method PDO::fetch_assoc() in C:\xampp\htdocs\blues\inc\functions.inc.php on line 30 I wasn't sure what you meant with "// check manual - I'm guessing at your class method here" Quote Link to comment https://forums.phpfreaks.com/topic/268524-php-problem/#findComment-1379060 Share on other sites More sharing options...
Jessica Posted September 18, 2012 Share Posted September 18, 2012 :facewall: Quote Link to comment https://forums.phpfreaks.com/topic/268524-php-problem/#findComment-1379063 Share on other sites More sharing options...
Mahngiel Posted September 18, 2012 Share Posted September 18, 2012 :facewall: Is why i avoid this board like the plague Quote Link to comment https://forums.phpfreaks.com/topic/268524-php-problem/#findComment-1379065 Share on other sites More sharing options...
Barand Posted September 18, 2012 Share Posted September 18, 2012 I wasn't sure what you meant with "// check manual - I'm guessing at your class method here" What I meant was "Check the manual for the class you are using, as I wasn't sure what it was. You are looking for a method that fetches a row from a query result and puts it in an associative array" Quote Link to comment https://forums.phpfreaks.com/topic/268524-php-problem/#findComment-1379078 Share on other sites More sharing options...
safety Posted September 18, 2012 Author Share Posted September 18, 2012 Sorry I'm an idiot and I'm still not sure what you mean, tried $sql = "SELECT id, title FROM entries ORDER BY created DESC"; $result = $db->query($sql); // Loop through returned results and store as an array while ($db->fetch(PDO::FETCH_ASSOC)($result)) // check manual - I'm guessing at your class method here { $e[] = $row; } I'll understand if you've run out of patience with me. Quote Link to comment https://forums.phpfreaks.com/topic/268524-php-problem/#findComment-1379082 Share on other sites More sharing options...
Jessica Posted September 18, 2012 Share Posted September 18, 2012 What is $db? Quote Link to comment https://forums.phpfreaks.com/topic/268524-php-problem/#findComment-1379083 Share on other sites More sharing options...
safety Posted September 18, 2012 Author Share Posted September 18, 2012 $db = new PDO(DB_INFO, DB_USER, DB_PASS); Quote Link to comment https://forums.phpfreaks.com/topic/268524-php-problem/#findComment-1379084 Share on other sites More sharing options...
ManiacDan Posted September 18, 2012 Share Posted September 18, 2012 while ($db->fetch(PDO::FETCH_ASSOC)($result)) // check manual - I'm guessing at your class method here This syntax is wrong. Like it says in the manual, you want: while ( $row = $result->fetch(PDO::FETCH_ASSOC) ) -Dan Quote Link to comment https://forums.phpfreaks.com/topic/268524-php-problem/#findComment-1379089 Share on other sites More sharing options...
safety Posted September 18, 2012 Author Share Posted September 18, 2012 thanks dan, i did try that, but i got this error Fatal error: Call to a member function fetch() on a non-object in C:\xampp\htdocs\blues\inc\functions.inc.php on line 30 just wondering also, with the method you posted barand: will it store the results in an array with keys called 'id' and 'title'? Quote Link to comment https://forums.phpfreaks.com/topic/268524-php-problem/#findComment-1379092 Share on other sites More sharing options...
Barand Posted September 18, 2012 Share Posted September 18, 2012 Mine was an invention as I had no idea what class you were using - just give you an idea of the process required. Dan's should work though, and yes, FETCH_ASSOC will give an array with those keys Quote Link to comment https://forums.phpfreaks.com/topic/268524-php-problem/#findComment-1379093 Share on other sites More sharing options...
Jessica Posted September 18, 2012 Share Posted September 18, 2012 Your new error means your query failed. Quote Link to comment https://forums.phpfreaks.com/topic/268524-php-problem/#findComment-1379094 Share on other sites More sharing options...
safety Posted September 18, 2012 Author Share Posted September 18, 2012 ah right ok, why would my query fail then? This is full updated code: <?php function retrieveEntries($db, $id=NULL) { /* * If an entry ID was supplied, load the associated entry */ if(isset($id)) { $sql = "SELECT title, entry FROM entries WHERE id=? LIMIT 1"; $stmt = $db->prepare($sql); $stmt->execute(array($_GET['id'])); // Save the returned entry array $e = $stmt->fetch(); // Set the fulldisp flag for a single entry $fulldisp = 1; } /* * If no entry ID was supplied, load all entry titles */ else { $sql = "SELECT id, title FROM entries ORDER BY created DESC"; $result = $db->query($sql); // Loop through returned results and store as an array while ( $row = $result->fetch(PDO::FETCH_ASSOC) ) { $e[] = $row; } // Set the fulldisp flag for multiple entries $fulldisp = 0; /* * If no entries were returned, display a default * message and set the fulldisp flag to display a * single entry */ if(!isset($e)) { $fulldisp = 1; $e = array( 'title' => 'No Entries Yet', 'entry' => '<a href="/admin.php">Post an entry!</a>' ); } } // Add the $fulldisp flag to the end of the array array_push($e, $fulldisp); return $e; } function sanitizeData($data) { // If $data is not an array, run strip_tags() if(!is_array($data)) { // Remove all tags except <a> tags return strip_tags($data, "<a>"); } // If $data is an array, process each element else { // Call sanitizeData recursively for each array element return array_map('sanitizeData', $data); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/268524-php-problem/#findComment-1379098 Share on other sites More sharing options...
Jessica Posted September 18, 2012 Share Posted September 18, 2012 Try running the query in mysql/phpmyadmin Quote Link to comment https://forums.phpfreaks.com/topic/268524-php-problem/#findComment-1379101 Share on other sites More sharing options...
safety Posted September 18, 2012 Author Share Posted September 18, 2012 I was using the wrong table name.. embarassing, but thanks very much for help with the troubleshooting jesirose. barand and dan thanks for your help with the code. It's all working as expected now i'm very happy. would have been stuck forever if it wasn't for you. Wish there was something I could do in return. is there? Quote Link to comment https://forums.phpfreaks.com/topic/268524-php-problem/#findComment-1379103 Share on other sites More sharing options...
mikosiko Posted September 18, 2012 Share Posted September 18, 2012 now that you found out that the problem was your query, did you try to run your original code with the foreach? // Loop through returned results and store as an array foreach($db->query($sql) as $row) { $e[] = array( 'id' => $row['id'], 'title' => $row['title'] ); Quote Link to comment https://forums.phpfreaks.com/topic/268524-php-problem/#findComment-1379108 Share on other sites More sharing options...
safety Posted September 18, 2012 Author Share Posted September 18, 2012 yes, I still got the same error "Warning: Invalid argument supplied for foreach() " the code that Barand/Dan came up with works great tho. I'm not sure why it's used in the book I'm using since it doesn't work. I'd be interested to know why if anyone knows, if not i'm happy with what I've got now. Quote Link to comment https://forums.phpfreaks.com/topic/268524-php-problem/#findComment-1379111 Share on other sites More sharing options...
Christian F. Posted September 19, 2012 Share Posted September 19, 2012 If you're talking about the snippet posted by mikosiko, it's probably because the query fails. Thus $db->query () returns false, which is not the array which foreach () expects. Move the execution of the query out of the loop, and handle errors, and you'll see why. Quote Link to comment https://forums.phpfreaks.com/topic/268524-php-problem/#findComment-1379191 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.