Jump to content

mysql_fetch_array


denoteone

Recommended Posts

Is there anyway that I can shorten the fallowing code?  lets say i didn't know the column names?

 

if($table == "TBPlayer"){
					 	if($parm1 == '' or $parm2 == ''){

						}else{
						$query  = "SELECT * FROM '$table' WHERE '$parm1'  = '$var1' and '$parm2' = '$var2'";
						$result = mysql_query($query)or die (mysql_error());

							echo "<table width=100%>"
							while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
							echo "<tr>
							<td>{$row['entryID']}</td>
							<td>{$row['playerID']}</td>
							<td>{$row['locationID']}</td>
							<td>{$row['playerName']}</td>
							<td>{$row['playerOrientation']}</td>
							<td>{$row['playerResolution']}</td>
							<td>{$row['playerModel']}</td>
							<td>{$row['ipAddress']}</td>
							<td>{$row['signCount']}/td>
							<td>{$row['postDate']}</td></tr>";
							}

							echo "</table>";
						}

				{

Link to comment
Share on other sites

$row is an array, right?

 

If you just want to display each column value in the order that the columns are in the table, you can iterate over the $row array using a foreach() loop.

 

If the order that you want display the information in is different than the column order, you either need to specify the columns in the SELECT term or have an array with the key names in the order that you want and iterate over the array of key names and access the corresponding elements in the $row array.

Link to comment
Share on other sites

So would something like this work? I am kinda confused about the $row = mysql_fetch_array($result, MYSQL_ASSOC)) and how it creates the array...

 

 

$query  = "SELECT * FROM '$table' WHERE '$parm1'  = '$var1' and '$parm2' = '$var2'";
$result = mysql_query($query)or die (mysql_error());

                echo "<table width=100%>"
                        while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
                              echo "<tr>";
                                      i = 0;
                                     foreach($row as $value){
                                     echo "<td>$row[i]</td>";
                                      i = i +1;  
                                      }
                              echo "</tr>";
                         }
                          echo "</table>";

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.