SoFunction
Updated on 2025-04-04

Review of PHP basic knowledge

header("Content-Type:text/html;charset=UTF-8");
$conn = mysql_connect('localhost','root','');//link server (non-permanent)
if(!$conn){
echo '<hr/>';
die('error:'.mysql_error());
}else{
echo '<hr/>';
echo 'link server:'.$conn;
}

$db = mysql_select_db('test',$conn);//Select database
if(!$db){
echo '<hr/>';
die('error:'.mysql_error());
}else{
echo '<hr/>';
echo 'link database:'.$db;
}

/*
mysql_query() returns only a resource identifier for SELECT, SHOW, EXPLAIN, or DESCRIBE statements, and FALSE if the query is executed incorrectly.
For other types of SQL statements, mysql_query() returns TRUE when the execution is successful and FALSE when the error occurs.
A non-FALSE return value means that the query is legal and can be executed by the server. This does not indicate any concerns about the number of rows affected or returned. It is very likely that a query was executed successfully but did not affect or did not return any rows.
*/
$sql="SELECT * FROM user";
$result = mysql_query($sql,$conn);//Execute a MySQL query, and the function automatically reads and caches the record set. To run a non-cache query, use mysql_unbuffered_query().
echo '<hr/>';
echo 'Query result set: '.$result;//Return resource identifier
//echo '<hr/>';
//print_r(mysql_fetch_array($result,MYSQL_ASSOC));//The function takes a row from the result set as an associative array
//echo '<hr/>';
//print_r(mysql_fetch_array($result,MYSQL_NUM));//The function takes a row from the result set as an array of numbers as a number
//echo '<hr/>';
//print_r(mysql_fetch_array($result));//The function takes a row from the result set as an associative array and a number array, and the mysql_fetch_row() function takes a row from the result set as an array of numbers.

/*
The mysql_fetch_array() function takes a row from the result set as an associative array, or an array of numbers, or both
Returns an array generated from rows obtained from the result set, and if there are no more rows, it returns false.
*/
echo '<hr/>';
echo '<table border="1" cellspacing="0" cellpadding="5">';
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo '</table>';

echo '<hr/>';
echo 'Close a non-persistent MySQL connection: '.mysql_close();

$name = array('fruits' => array('orange', 'banana', 'apple'),
'veggie' => array('carrot', 'collard','pea'));;
echo '<hr/>';
print_r($name['fruits'][1]);
echo '<hr/>';
echo count($name);//Calculate the number of units in the array or the number of attributes in the object

echo '<hr/>';
/*
The symbol "->" means: the function and member variables of the calling class
*/
class className{
function funName(){
echo "dggdgdgd";
}
}
$classOne = new className();
$classOne->funName();

echo '<hr/>';
$i=0;
do{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<5);