Jump to content

Problem with creating an array from fetched data


Schaarschmidt

Recommended Posts

I am an absolute novice to PHP and am following a tutorial in the Sitepoint book PHP Novice to Ninja. When I followed everything exactly, it worked. Now I'm working through again using my own files and names and am absolutely stuck on my last line of code. I clearly don't understand something.

 

I am trying to retrieve data from my mySQL database. The code in the tutorial is:

  {
  $jokes[] = array ('id'=>$row['id'], 'text' => $row['joketext']);
  }

 

When I was using their data, that worked just fine.  Now I'm trying to make it work with my data.  Here is the code I'm using now:

 

while ($row = $result->fetch())
  {
  $parks[] = array('id' => $row['A'], 'text' => $row['B']);
  }

The columns in my database are called A and B

 

What am I doing wrong?

Link to comment
Share on other sites

How do you know it isn't working?

 

Do you have error_reporting and display errors on full in your php.ini?  It should be for development.

Of course you can set these at runtime with.

error_reporting(-1);
ini_set('display_errors',1);

 

Now on to your particular problem.  Does the book use mysqli or just mysql?  I cannot tell with your snippet.  I am assuming that you are using mysqli as you didn't define the fetch properties for PDO (they both have a fetch() method).  So, did you bind the results prior to calling the fetch() method?

 

It would be easier to help you if you posted the entire script you are working with.  Minus any passwords or usernames of course.

Link to comment
Share on other sites

Thanks for your attempt at help.  I feel like I don't even know enough to ask intelligent questions. Sometimes I feel like I'm going to be great at this, and sometimes I feel too dumb even to speak.

 

However, in this case, I was actually able to identify the problem.  I was asking for information from an associative array, but earlier I was only defining one parameter of the association.  I'm sure that I'll be able to say all of this in a way that makes sense at some point, but at least at this point I'm past this and off to other problems.

 

In the future I'll be sure to post the entire script and try to be more specific in my question.

 

Thanks again for your help.

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.