Hello!!!
My english is very bad, so forgive me...
My problem is: I have a mysql database with some fields like Name, Number and Password (just examples), and i need to store in a array the result of a select of the Name field. Why? I need to print these names in diferents positions on the page. In each these locations, the code may be "echo '$array[0]';" , for example. My database have 77 entries, and i need to show all of these in the site. But I don´t want to do 77 Queryes!!! Can anyone help me???
Thanks!!!
Aidmar Junior
PS: Se alguém souber português e puder me responder em português, ficarei muito grato!!!! Rs...
I kind of get what you mean so ill try and help you out.
Do this:
| Code: |
$sql = mysql_query("SELECT * FROM users");
while($row = mysql_fetch_array($sql)){
echo "<p>$row[username]</p>";
} |
That should do what your asking alothoughyou might have to change some little things to fit your needs,
Goodluck.
Thank you!!!!
But i had another problem...
I type this:
<?php
$terr = mysql_query ("select NOME from territorios");
while($row = mysql_fetch_array($terr)){
$result = array($row['NOME']);
} ?>
In other location, I type <?php echo $result[0]; ?>, and it shows the first record. But when I type <?php echo $result[1]; ?>, or any different number, a message error appears: "Undefined offset: 1". What I have done wrong???
Thanks again!!!
If you look at the code its "mysql_fetch_ARRAY" and when you use that to echo out the info you do this:
| Code: |
| echo $row[COLUMN NAME]; |
With what you are doing you use "mysql_fetch_ROW" and then use the numbers in the array to echo it out. Is that what you want?
Hi!!!
I solved the problem with a function named "mysql_result". I typed this:
| Quote: |
| <?php $query = mysql_query ("select FIELDNAME from TABLENAME order by OTHERFIELD"); ?> |
And in each position that i need to show a entrie of 1 row and 1 field, I typed this (77 times!!!):
| Quote: |
| <?PHP echo mysql_result($query, 7, 'FIELDNAME'); ?> |
The "7" number is the row that i want show. I preferred to do this with array, cause it would be more easy for the server. I can't use your solution because the results will not be showed in a single table. But Thank You Very Much!!!
And forgive me for the bad bad english... 
Use a loop that loops through that piece of code with 77 different indexes for the array and echoes out with <tr> and <td> tags?
I don't know how to write a for loop in php, but in pseudocode it would be something like:
for every number in [0 to 77]:
<?PHP echo "<tr><td>"
<?PHP echo mysql_result($query, number, 'FIELDNAME'); ?>
<?PHP echo "</td></tr>"
In python:
| Code: |
for i in range(0,78):
print "<tr><td>%s</td></tr>"%(mysql_result(query,i,'FIELDNAME')) |
I don't think python has that function, but this is probably the easiest way to go about your problem (A simple for loop).
i have nooooooooooooooooooooooooo idea what you are talking about! Can you bbe a bit more clear in the situation? Can you show us a screen shot of what is happening. I do not understand the probelm. I have never encountered it myself...........
perhaps you could try a specific thing called PHPMYadmin- i heard that it may be able to do "loops"
Thanks for all!!!
I will continue using mysql_result, because I already know how to use it. I never did a loop before, and my attempts have not worked!!! But, if someone could teach me, i will be very happy!!! This is what i need:
I have a database with these fields: ID, NAME and PASSWORD. I want to store in a array all records that are in the field NAME only! Just it.
Well, is this!!!
Thanks,
Aidmar Junior
I don't know php arrays or the functions that work on them, but that should be simple and easy to set up.
| PHP.net wrote: |
| Code: | <?PHP
for ($i = 1; $i <= 10; $i++) {
echo $i;
}?> |
|
What does this do? It first assigns 1 to the variable $i. This happens only the first round in the loop. Then it checks if $i is smaller than or equal to 10. It is, so $i is incremented by 1, and then echoed. Now the for loop starts over again; it checks whether $i is smaller than or equal to 10, it still isn't, so it increments $i by one again, and so on, until $i is greater than 10.
What you need, I imagine is something like:
| Code: |
<?PHP
for ($i = 1; $i <78; $i++) {
echo "<tr><td>";
echo mysql_result($query, $i, 'NAME');
echo "</td></tr>"
}
?> |
That should give you some sort of 'table' with all the 77 names in it, and it's an example of a simple usage of a loop.
For information about arrays, I suggest you use this.
I imagine that something like: | Code: |
<?PHP
$somearray = array()
for ($i = 1; $i <78; $i++) {
$somearray[$i] = mysql_result($query, $i, 'NAME')
}
?> |
As I've already said, I don't know php and whether this would be exactly corre ct, but I imagine that this is just about how you need to do it.
Edit: There is probably a way to load all the information from a sql query into an array, and then you could use this method to print it to a html table. I wouldn't know how though.
ThankYou again, my friend... That is what i need! But i think theres no way to do this without the mysql_result. Using loop and array, I need to use this function. But, just one more question: What is the way that consumes less resources of the server?
| Quote: |
<?php
$sql = ("select NAME from table");
echo mysql_result($sql, 3, 'NAME');
echo mysql_result($sql, 7, 'NAME');
?>
|
or
| Quote: |
<?php
$sql = ("select NAME from table");
$somearray = array();
for ($i = 1; $i <78; $i++) {
$somearray[$i] = mysql_result($sql, $i, 'NAME');
}
echo $somearray[3];
echo $somearray[7];
?>
|
???
EDIT: Use fire boars :S
Last edited by Manofgames on Wed Apr 25, 2007 7:11 pm; edited 1 time in total
You could use something like...
| Code: |
$query = "select NAME from table";
$result = mysql_query($query);
// Set up an array to hold everything...
$array = array();
// Loop through each row...
while ($row = mysql_fetch_array($query))
{
$array[] = $row['NAME'];
}
/*
Now all the rows are stored in $array. So first row would be $array[0], second would be $array[1], etc. You could then do something like this:
*/
echo "<table>";
for ($i = 0; $i < count($array); $i++)
{
echo "<tr><td>".$array[$i]."</td></tr>";
}
echo "</table>"; |