Review of PHP basics

  • 2020-05-19 04:18:50
  • OfStack

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

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

/*
mysql_query() returns only one resource identifier for SELECT, SHOW, EXPLAIN or DESCRIBE statements, and FALSE if the query was not executed correctly.
For other types of SQL statements, mysql_query() returns TRUE on success and FALSE on error.
A non-FALSE return value means that the query is valid and can be executed by the server. This does not say anything about the number of rows affected or returned. It is possible that a query executed successfully without affecting or returning any rows.
*/
$sql="SELECT * FROM user";
$result = mysql_query ($sql, $conn); // executes an MySQL query, which automatically reads and caches the recordset. To run a non-cached query, use mysql_unbuffered_query().
echo ' < hr/ > ';
echo 'query result set: '.$result; // returns the 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
//echo ' < hr/ > ';
/ / print_r (mysql_fetch_array ($result)); The // function gets 1 row from the result set as an associative array and a numeric array, and the mysql_fetch_row() function gets 1 row from the result set as a numeric array

/*
The mysql_fetch_array() function takes 1 line from the result set as an associative array, or as a numeric array, or as a combination of 2
Returns an array generated from the rows retrieved from the result set, or false if there are no more rows.
*/
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 'closes non-persistent MySQL connections: '.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); // calculates the number of cells in an array or the number of attributes in an object

echo ' < hr/ > ';
/*
The symbol "- > "Means: call the function and member variables of the 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);

Related articles: