SoFunction
Updated on 2025-04-11

Several good example codes for getting started with PHP

1, php connection database

<?php

$dbhost = 'localhost'; 
$dbuser = 'root'; //Your mysql username
$dbpass = '123456'; //Your mysql password
$dbname = 'data'; //Your mysql library name

//Connect the local database
$GLOBALS["conn"] = mysql_connect($dbhost,$dbuser,$dbpass); 

//Open the database
mysql_select_db($dbname,$GLOBALS["conn"]);

?>

Read a field value in the database

<?php

//Read a column of data
$sql="select * from ec_admin";
$result = mysql_query($sql,$GLOBALS["conn"]);

printf("Username: %s<br>\n", mysql_result($result,3,"UserName"));
printf("Password: %s<br>\n", mysql_result($result,3,"UserPass"));

?>

3. PHP inserts a certain data

<?php

$sql="insert into ec_admin(UserName,UserPass) values('liugongxun2','jakemary2')";
$result=mysql_query($sql,$GLOBALS["conn"])or die(mysql_error());

?>

4, php while loop

<?php

$sql="select * from ec_admin";
$result = mysql_query($sql,$GLOBALS["conn"]);
while($myrow = mysql_fetch_row($result))
{
printf("Username %s%s Password<br />",$myrow[1],$myrow[2]);
}

?>

5,php do while loop

<?php

$sql="select * from ec_admin";
$result = mysql_query($sql,$GLOBALS["conn"]);
if($myrow = mysql_fetch_array($result))
{
 do
 {
printf("Username%s%s Password<br />",$myrow["UserName"],$myrow["UserPass"]);
 }while($myrow = mysql_fetch_array($result));
}
?>

6, php determines whether the form is submitted

<?php

if ($submit)

{}

?>