1

Ok so I have an array "$landing" in my header.php, then in my page.php I include the header.php but for some reason when I call the 'Name' field in the array in the page.php: echo $landing['Name']; it just doesn't work.

this is how the array is being filled up, and calling it in the header works perfectly.

$landing = array();

while ($row = mysql_fetch_array($result)) {
   $str = strtolower($row['Name']);
   if ($str == $name) {
       $landing = $row;
   }
}

To clarify, $row and $landing are both arrays, and both have multiple fields 'Name' 'Color' 'Info'.

What am I doing wrong here? Do I need to make it global somehow or what's going on?

1
  • 2
    Apart from you using deprecated MYSQL extension and not using the newer MYSQLi, there's nothing wrong with that code. You need to show us more. Commented Aug 27, 2013 at 19:51

3 Answers 3

3

The original code works somehow, now as the OP said in a comment.

But my old tips still hold:

  • Consider using MySQLi or PDO instead of the deprecated MySQL extension!
  • Why do you compare the dataset's column value on the client-side? You can do this on the MySQL side, it'll be faster!
Sign up to request clarification or add additional context in comments.

9 Comments

Sorry, to clarify, $row and $landing are both arrays, and both have multiple fields 'Name' 'Color' 'Info.
OK, I have to ask this, how is $landing = $row; making $landing a string?
@Dexa Yeah, I just realized it, too (I confused it with $str). /cc @Started...:Your original code should actually work. Try var_dump($landing) after the while loop.
ok so, a var_dump on $landing shows all the right variables, when called in header.php, but it's blank when var_dump on page.php still
wow nevermind, a var_dump is working on page.php now..It must have been a synthax error, or some glitch with wpengine, thanks for the help nonetheless!
|
0

You are, as ComFreek correctly stated, turning $landing into a string. Instead, if you're trying to add an entry to the landing array, use [] brackets which mean "add into new entry".

$landing = array();    
while ($row = mysqli_fetch_array($result)) {
   $str = strtolower($row['Name']);
   if ($str == $name) {
       $landing[] = $row;
   }
}

6 Comments

I was not correct because $row is an array (I confused it with $str) ;)
As we already established there was error in ComFreek answer, but anyway why would you make answer where you say "As other person answered... I'll copy answer here" :/ Doesn't make sense to me.
@ComFreek Right. Oops. Apart from the fact that $name is nowhere else to be found in the code, I don't see the problem.
@Dexa I actually answered the question instead of providing methods to debug and find the problem. At least I thought I did until we all find out we were wrong..
@Dexa LS97 probably thought that there were more than one matching result set. I think it was more an addition than a complete new answer (but it still justifies an answer in my opinion).
|
-1

I cant comment LS97 post, anyway you want to use $landing["Name"] you edit LS97 code into this:

$landing = array();    
while ($row = mysqli_fetch_array($result)) {
   $str = strtolower($row['Name']);
   if ($str == $name) {
       $landing["name"] = $row;
   }
}

If you want to use multiple names, LS97 code is fine.

The problem is what they said. (Using $landing = $row, landing will be a string.)

2 Comments

Why? What is the problem? Comment pls :/
@MarcoLopezAcierno The original code was never the problem. The OP had got some other errors in the outputting code (see his comment on my answer).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.