| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Reguarding SQL
say im using the select * to grab al fiends in a table is there a way short of hand naming ieach field that PHP can grab the field ID in the SQL file?
__________________
24y-CW, 8y-HTML, 5y-PS, 8m-PHP And I never knew Photoshop could do HTML! You learn something every day. http://lexxboard.lurkerlordx.com/listcatagories.php |
|
#2
|
|||
|
|||
|
Sure, you'll use the mysql_fetch_assoc function.
Code:
$query = "select * from users"; $result = mysql_query($query); $first_row = mysql_fetch_assoc($result); $user_id = $first_row['user_id']; $password = $first_row['password']; or, to loop through it: Code:
$query = "select * from users";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
$user_id = $row['user_id'];
$password = $row['password'];
print "user: $user_id, password: $password
";
}
|
|
#3
|
|||
|
|||
|
i know about that what i trying to do is automate the
$first_row['user_id']; $first_row['password']; see what i basicly want to do is dynamicly genererate a header row in a table, as it stands now i have to tell type out 37 lines of code for 37 fields i want to stop doing <td>field title 1</td> <td>field title 2</td> <td>field title 3</td> <td>field title 4</td> ... <td>field title 37</td> and find a way to automate and print that "$first_row[something];" field the value in row one of Field A but the actual name of field A, grabed dynamicly |
|
#4
|
|||
|
|||
|
oh I gotcha, you want to print out the name of the field, not the contents of it.
well when you do the mysql_fetch_assoc, it creates an array with an array element for each field, and that array element has the field name. so: Code:
$array_size = count($array);
$count = 1;
print "<table><tr>";
while ($count <= $array_size) {
print "<td>".$array[$count-1]."</td>";
$count++;
}
print "</tr></table>";
|
|
#5
|
|||
|
|||
|
thanks as it stands now i use 37 function calls one for each TD....
would this also worl for making a blank row (as a spacer) Code:
echo '<TR><TD colspan='.$array_size.'></<TD></<TR>'; |
|
#6
|
|||
|
|||
|
It should, give it a shot.
|
|
#7
|
|||
|
|||
|
oddy it only partiall workms maybe im doing it wrong but heres the relevent code, note i kept my function calls as a compair against at the moment
Code:
echo '<Table border=1><TR>';
$query = "SELECT * FROM nannies Order by Applicant_ID_Number";
$result = mysql_query($query, $db) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$array_size = count($row);
$count = 1;
while ($count <= $array_size) {
echo "<td>".$row[$count-1]."</td>";
$count++;
}
print "</TR><TR>";
HeadRepeater("ID Number");
...A heck of a lot more...
HeadRepeater("Agency List");
echo '</TR><TR><TD colspan='.$array_size.'></TD></TR>';
the blank array_size row works, and the header repeat functions work but not the field name display |
![]() |
| Viewing: Tutorialized Forums > Databases > SQL Basics > Reguarding SQL |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|