1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

mysql

Discussion in 'PHP' started by ssimon171078, May 16, 2015.

  1. #1
    i wrote php script to connect to database ,how can i change script that script will show also names of fields ?
    /* Connecting, selecting database */
            $CON = MYSQLI_CONNECT("localhost","user1","12345","videostir_2");
               IF (MYSQLI_CONNECT_ERRNO())                             //  CHECK CONNECTION
                 {
                    ECHO "FAILED TO CONNECT TO MYSQL: " . MYSQLI_CONNECT_ERROR();
                  }
               $query = "SELECT * FROM stats";                             /* Performing SQL query */
                $result = mysqli_query($CON,$query) or die("Query failed");
                print "<table>\n";                                                            // Printing results in HTML
                while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) {
                       print "\t<tr>\n";
                       foreach ($line as $col_value) {
                               print "\t\t<td>$col_value</td>\n";
                            }
                       print "\t</tr>\n";
                    }
                  print "</table>\n";
                  mysqli_free_result($result);            // Free result set
                  mysqli_close($link);                            // Closing connection  
               ?>
    Code (markup):

     
    ssimon171078, May 16, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    PoPSiCLe, May 16, 2015 IP
  3. raffahacks

    raffahacks Greenhorn

    Messages:
    35
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    20
    #3
    I don't quite understand you, hope this helps.
    http://php.net/manual/en/function.each.php

    If it doesn't just tell me, I'll try to help :)
     
    raffahacks, May 18, 2015 IP
  4. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #4
    Using mysqli, html tables, login info in script, and using die() to return an error..... I don't know...I think you should start over by writing modern secure php code..
     
    NetStar, May 18, 2015 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #5
    1) Stop using the functional version, it's just a wrapper that adds overhead to it.

    2) why are you using print?

    3) why are you declaring your \n when you could just format the output?

    4) which would mean you could have proper output instead of that double-quote asshattery.

    5) methods are not uppercase, there are namespace 'rules' you should probably be using for PHP, going ape-shit with caps isn't it. Lemme guess, used to write Visual Basic for a living?

    I'm guessing wildly here, but if I'm following what you want, it would go something like this:

    <?php
    
    $mysqli = new mysqli('localhost','user1','12345','videostir_2');
    
    if ($mysqli->connect_errno()) die(
    	'MySQL Connection Failed: ' . $mysqli->connect_error()
    );
    
    if ($result = mysqli_query('SELECT * FROM stats')) {
    
    	if ($row = $result->fetchObject()) {
    		
    		echo '
    		<table>
    			<caption>Contents of <b>stats</b> Table</caption>
    			<thead>
    				<tr>';
    				
    		foreach ($row as $name => $data) echo '
    					<th scope="col">', $name, '</th>';
    		
    		echo '
    				</tr>
    			</thead><tbody>';
    			
    		do {
    	
    			echo '
    				<tr>';
    		
    			foreach ($row as $data) echo '
    					<td>', $data, '</td>';
    				
    			echo '
    				</tr>';
    				
    		} while ($row = $result->fetch_object());
    	
    		echo '
    			</tbody>
    		</table>';
    		
    	}
    		
    	$result->close();
    	
    }
    
    $mysqli->close();
    
    ?>
    Code (markup):
    You may want to add an "else" statement when the row fetch fails to say "nothing to show".

    The big magic is the do/while construct and running foreach twice, and understanding how to parse the result using =>

    That should give you a properly organized/constructed table with the formatted whitespace, with proper headings and scope.
     
    Last edited: May 18, 2015
    deathshadow, May 18, 2015 IP