This article mainly shares the PHP database operation records with you, hoping to help everyone.
header('Content-Type:text/html;charset=utf-8'); define('DB_HOST','localhost') define('DB_USER','root'); define('DB_PWD','密码'); -----》你的数据库登入密码 define('DB_NAME','数据库名称'); ----》指明哪个数据库 $conn = @mysql_connect(DB_HOST, DB_USER, DB_PWD) or die('数据库连接失败'.mysql_error()); @mysql_select_db(DB_NAME) or die('数据库错误'.mysql_error()); @mysql_query('SET NAMES UTF8') or die('字符集错误'.mysql_error());
If there is no problem with the above, the database connection is successful
Insertion into the database
$query="INSERT INTO user(user,pass,email,sex,birthday,date ) values('{$_POST['user']}','{$_POST['pass']}','{$_POST['email']}','{$_POST['sex']}' ,'{$_POST['birthday']}',NOW())";
The above is the sql statement. When you need to insert a variable in values, it should be written as '{variable name}'
mysql_query($query) or die('Add failed'.mysql_error());
echo mysql_affected_rows(); What is returned here is the database affected Number of items affected
Database query
$query_userId= "select id from user where user='{$_POST['userName']}'"; $result = mysql_query($query_userId) or die("新增失败" .mysql_error()); while($row = mysql_fetch_array($result)) { $row_userId= $row['id']; }
$row stores one row of data. If the data set has multiple rows, it needs to be traversed through a while loop.
It is worth noting that when your demand is not what the data set is, but whether the data set has data:
This requires using another function mysql_num_rows($result) This is to return how many rows there are
Related recommendations:
Related recommendations:
PHP Basic introduction to database operation
php database operation and database connection tutorial
php database operation model class (using __call method)
The above is the detailed content of php database operation record. For more information, please follow other related articles on the PHP Chinese website!