ARRAYS


#1

I’m really new to PHP, I’m working with a dummies book and I’m having a problem with arrays according to the book. it say’s to run a query this will put the data in a temp location in this case with the variable $RESULT.

Where can I find this temp location.

The statement
$query= “SELECT * FROM PET”;
$result = mysql_query($query)

or die (“Couldn’t execute query.”);

(should the above be a PHP file ? (Like I said I’m really new to this)


#2

as far as i can see, all it does is store the data in the variable. since a variable IS a temporary data holder, that would be what it’s talking about. if you’re looking to get the date selected into an array, you can try making a new variable like this and using mysql_fetch_array:

[php]<?php
$query= “SELECT * FROM PET”;
$result = mysql_query($query) or die (“Couldn’t execute query.”);
$array = mysql_fetch_array( $result );
?>[/php]

Then you could do something like the below to print out the output. I’ll create a few fake column names since I don’t know what you have in your table:

[php]
$output = "ID: ".$array[‘id’];
$output .= "Name: ".$array[‘name’];
$output .= "Address: ".$array[‘address’];
print $output;
[/php]

What mysql_fetch_array does is make the variable the data is stored in an associate array with the table’s column names as keys (‘id’, ‘name’, ‘address’, etc.) Similarly, if you read a bit ahead and learn about objects, you could use mysql_fetch_object, in which case your item would be stored as an object:

[php]

<?php $query= "SELECT * FROM PET"; $result = mysql_query($query) or die ("Couldn't execute query."); $object = mysql_fetch_object( $result ); $output = "ID: ".$object->id; $output .= "Name: ".$object->name; $output .= "Address: ".$object->address; print $output; ?>

[/php]

If you’re trying to access multiple mysql rows of data with that statement, you can do something like this:
[php]

<?php $query= "SELECT * FROM PET WHERE id ='".$id"'"; $result = mysql_query($query) or die ("Couldn't execute query."); while( $object = mysql_fetch_object( $result ) ) { $output = "ID: ".$object->id; $output .= "Name: ".$object->name; $output .= "Address: ".$object->address; print $output; }; ?>

[/php]

that help?


#3

I don’t mean this to be hateful or hurtful, but you should try to learn and understand programming concept and basics first. Trust me…Most programming languages run on the same concepts just written differently. I think if you had a clear understanding of just even the basics of programming things would go a lot easier for you.


#4

Actually the $result is NOT the temporary location. If you were to ECHO $result to the screen you would likely get something like Resource id 1. That is because the location the data is in is just that a TEMPORARY location which is identifed by $result (aka Resource Id 1).

As gogglecollector points out, you need another command to “Fetch” the data such as mysql_fetch_array() or mysql_fetch_assoc(). (Please check http://www.php.net for specifics and syntax). Then you can manipulate the data to display.

Finally to Ragster00’s point, if you are jumping into MYSQL questions, you should have a basic concept of programming in general. Dealing with Resource Id’s and Arrays, is in the realm of web programming, more on advanced topics. If you are serious about learning PHP I would reccomend that you find a good book on PHP. I personally have used Beginning PHP 4 http://www.wrox.com/WileyCDA/WroxTitle/productCd-0764543644.html by WROX Press and found it to have solid examples and a very good book. Although PHP is now on Version 5 and there are differences, it will get you through the basics. (Not that I don’t like the “For Dummies” books, but I just found some of them to be “Dumbed Down” WAY to much that makes things even harder.)

Any way good luck and I hope these have answered your question.


#5

I would actually recommend the book below also from WROX, but that is only because a college buddy of mine is one of the authors and he gets a cut from every book sold! :) Jeremy Stolz is his name.

http://www.wrox.com/WileyCDA/WroxTitle/ … 57440.html


#6

Thanks to all that replied, I got it working, I am new to PHP but I do understand Queries, my whole problem was that I was using _ in my table and it was not working.