Jump to content

php problem


safety

Recommended Posts

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

Link to comment
Share on other sites

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()

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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.  :)

Link to comment
Share on other sites

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'?

Link to comment
Share on other sites

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);
}
}

?>

Link to comment
Share on other sites

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 :D

 

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?

Link to comment
Share on other sites

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']
                );

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.