Guides:SQL/How do I/PHP and MySQL

From CoderGuide

Jump to: navigation, search

Return to the SQL How Do I Index

Here is the new method for PHP5. Procedural method example:

<html>
<head>
<title>MySQL with PHP5 test</title>
</head>
<body>
<?php
$connect =mysqli_connect("server","username","password");
mysqli_select_db($connect,"test");
$result = mysqli_query($connect,"SELECT * FROM tt;");
?>
 
<table border="1">
<tr><th>ID</th><th>Data A</th><th>Data B</th></tr>
<?php
while( $row = mysqli_fetch_array($result, MYSQLI_NUM)){
?>
<tr>
<?
	foreach ($row as $col)
		print "<td>$col</td>";
?>
</tr>
<?php
}
?>
</table>
</body>
</html>

MYSQLI_NUM tells mysqli_fetch_array to return a array referenced by numbers, MYSQLI_ASSOC tells it to use an associated array (referenced by labels, also incorrectly called a hash in Perl), and MYSQLI_BOTH is for those that can't make up their mind (allows you to refer to the elements by column ID or by the order in which they are returned by the query).

Personal tools