PHP learning officially sets sail (6)

黄舟
Release: 2023-03-04 11:38:02
Original
987 people have browsed it

Now let’s talk about php error handling

<?php
$file=fopen("1.txt","r");
?>
Copy after login

If the file does not exist, the system will directly report an error
You can write the error message yourself by using die. Die means death, which means death. Error

To prevent users from getting error messages similar to the above, we check whether the file exists before accessing it

<?php
if(!file_exists("1.txt"))
{
die("File not found");
}
else
{
$file=fopen("1.txt","r");
}
?>
Copy after login

If the file does not exist, File not found will be printed instead Errors that pop up in the system


In fact, PHP’s error handling is much more than that, there are exceptions, etc.
I’ll talk about it later

Now let’s start the main event, finally use it PHP operates the mysql database

First connect to the database

<?php
$link = mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);
if (!$link) {
die(&#39;Could not connect to MySQL: &#39; . mysql_error());
}
echo &#39;Connection OK&#39;; mysql_close($link);
?>
Copy after login

$link This is another resource variable, indicating the database connection status. You can try to print it. See what will happen
mysql_connect function is to connect to the mysql database
There are 3 functions, namely server address database user name database user password
mysql_error function can know the cause of the connection error
mysql_close is to close the database connection

Now create a database first
create database test;
Then create a table

PHP learning officially sets sail (6)

Super simple

Enter phpmyadmin to see

PHP learning officially sets sail (6)

PHP learning officially sets sail (6)

First we insert a few pieces of data and see

Now it will connect to mysql database, then we have to insert data into one of the mysql databases

<?php
$link = mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);
if (!$link) {
die(&#39;Could not connect to MySQL: &#39; . mysql_error());
}
mysql_select_db(&#39;test&#39;,$link);
mysql_query("insert into user (name,age) values (&#39;harry&#39;,15)",$link);
mysql_close($link);
?>
Copy after login

mysql_select_db is the function to select the database
mysql_query is the function to execute sql statements, any sql can be executed , including creating databases and tables
The second parameter of the two functions can be omitted
mysql_select_db('test');
mysql_query("insert into user (name,age) values ​​('harry',15 )");
If the second parameter does not specify a connection identifier, the last opened connection is used. If there is no open connection, mysql_connect() will be called without parameters to try to open one and use it

Now see if the insertion is successful

PHP learning officially sets sail (6)

ok has been inserted

Now we use the form to insert data

<html>
<body> <form action="" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form> </body>
</html>
Copy after login
<?php
$name=$_POST[&#39;name&#39;];
$age=$_POST[&#39;age&#39;];
if(isset($name)&&isset($age))
{
$link = mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);
if (!$link) {
die(&#39;Could not connect to MySQL: &#39; . mysql_error());
}
mysql_select_db(&#39;test&#39;);
$sql="insert into user (name,age) values (&#39;$name&#39;,$age)";
if(mysql_query($sql))
echo "Insert Success!";
else
echo "Insert Fail!".mysql_error();
mysql_close($link);
}
?>
Copy after login

mysql_query() returns TRUE when the sql statement is executed successfully, and returns FALSE when an error occurs

Now let’s display the data

##

<html>
<body> <form action="" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
<?php
//数据库连接
$link = mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);
if (!$link) {
die(&#39;Could not connect to MySQL: &#39; . mysql_error());
}
mysql_select_db(&#39;test&#39;); //插入
$name=$_POST[&#39;name&#39;];
$age=$_POST[&#39;age&#39;];
if(isset($name)&&isset($age))
{ $sql="insert into user (name,age) values (&#39;$name&#39;,$age)";
if(mysql_query($sql))
echo "Insert Success!";
else
echo "Insert Fail!".mysql_error(); }
//查询
$sql="select * from user";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "<table>";
echo "<tr>";
echo "<td>" . $row[&#39;id&#39;] . "</td>";
echo "<td>" . $row[&#39;name&#39;] . "</td>";
echo "<td>" . $row[&#39;age&#39;] . "</td>";
echo "</tr>";
echo "</table>";
} mysql_free_result($result);
mysql_close($link); ?>
</body>
</html>
Copy after login

$result=mysql_query($sql); $result is also a resource variable. When mysql_query executes the query sql statement, the return result is It is no longer true and false, but a result set. You can imagine that the result of the mysql_query query returns a table to $result, and $result is a table (result set)

The mysql_fetch_array function is specifically To process the result set, get a row from the result set as an associative array, or a numeric array, or both, mysql_query($sql); itself is the result set
Why do you need to loop, because mysql_fetch_array can only be in The result set (table) takes a row of data
and then gives this row of data to $row in the form of a one-dimensional associative array. $row is a one-dimensional associative array
mysql_fetch_array has a pointer pointing to each row. Whenever the execution is completed When this function is executed, the pointer will automatically point to the next line. If the function is executed again, the data of this line can be obtained. Until the last line, the pointer will point to null, so the loop will also end because of null
Don't think that $row is a two-dimensional array. It is always one-dimensional and is reassigned every time it loops.
Some people may doubt that arrays can also be used as loop conditions? Yes
$a=array(1,2)
while($a)
This can be looped, but it is an infinite loop
Because there is only something that is not 0 in the while brackets It can all be looped


$row['name'] It just takes the value from the array. Remember the associative array?
In fact, you can also use array subscripts. I didn't say it before. In fact, associative arrays also have the characteristics of ordinary arrays and are more powerful
So this is possible

echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
Copy after login

mysql_free_result is to release resources

It is not necessary to use it, it only needs to be considered if the return is large How much memory will be occupied by the result set when called. All associated memory will be automatically released after the script ends.



In addition, mysql_fetch_row can also query the results, but compared with mysql_fetch_array, it is weak. Here is just an introduction to what has happened in the past. Such a thing. . .


//Inquire

$sql="select * from user";
$result=mysql_query($sql);
while($row = mysql_fetch_row($result))
{
echo "";
echo "";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "";
echo "
"; }
Copy after login

mysql_fetch_row() 从和指定的结果标识关联的结果集中取得一行数据并作为数组返回。每个结果的列储存在一个数组的单元中
则row不再是关联数组而是普通数组,所以只能用数组下标


下面说说几个常用的数据显示函数
int mysql_num_rows ( resource $result )
mysql_num_rows() 返回结果集中行的数目。此命令仅对 SELECT 语句有效
int mysql_num_fields ( resource $result )
mysql_num_fields() 返回结果集中字段的数目
int mysql_insert_id ([ resource $link_identifier ] )
mysql_insert_id() 返回给定的 link_identifier 中上一步 INSERT 查询中产生的 AUTO_INCREMENT 的 ID 号

重新写下

 
 
Name: Age:
"; echo ""; echo "<td>" . $row[0] . "</td>"; echo "<td>" . $row[1] . "</td>"; echo "<td>" . $row[2] . "</td>"; echo ""; echo ""; } echo "总共".mysql_num_rows($result)."记录
"; echo "每行".mysql_num_fields($result)."字段"; mysql_free_result($result); mysql_close($link); ?>
Copy after login

header("Content-Type:text/html;charset=gbk"); 这个是表明文件编码格式,显示中文需要这样
error_reporting(0); 屏蔽一切系统提示的注意,警告,和错误

现在完成 修改和删除部分

<?php
header("Content-Type:text/html;charset=gbk");
error_reporting(0);
?> <html>
<body> <form action="" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form> <?php
//数据库连接
$link = mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);
if (!$link) {
die(&#39;Could not connect to MySQL: &#39; . mysql_error());
}
mysql_select_db(&#39;test&#39;);
//插入
$name=$_POST[&#39;name&#39;];
$age=$_POST[&#39;age&#39;];
if(isset($name)&&isset($age))
{ $sql="insert into user (name,age) values (&#39;$name&#39;,$age)";
if(mysql_query($sql))
echo "Insert Success!";
else
echo "Insert Fail!".mysql_error(); } /
/查询
$sql="select * from user";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
?>
<form action="" method="post">
ID: <?=$row[&#39;id&#39;]?>
Name: <input type="text" name="_name" value="<?=$row[&#39;name&#39;]?>"/>
Age: <input type="text" name="_age" value="<?=$row[&#39;age&#39;]?>" />
<input type="hidden" name="id" value="<?=$row[&#39;id&#39;]?>" />
<input type="submit" value="修改"/>
<a href="index.php?uid=<?=$row[&#39;id&#39;]?>">删除</a>
</form> <?php
} echo "总共".mysql_num_rows($result)."记录<br/>";
echo "每行".mysql_num_fields($result)."字段";
//修改
$name=$_POST[&#39;_name&#39;];
$age=$_POST[&#39;_age&#39;];
$id=$_POST[&#39;id&#39;];
if(isset($name)&&isset($age)&&isset($id))
{ $sql="update user set name=&#39;$name&#39;,age=&#39;$age&#39; where id=$id";
if(mysql_query($sql))
header("location:index.php");
else
echo "Update Fail!".mysql_error(); }
//删除
$uid=$_GET[&#39;uid&#39;];
if(isset($uid))
{ $sql="delete from user where id=$uid";
if(mysql_query($sql))
header("location:index.php");
else
echo "Delete Fail!".mysql_error(); }
mysql_close($link); ?>
</body>
</html>
Copy after login


至此,php对mysql数据库的增删改查操作就全在这一个页面了,灰常的简单

加了个简单的分页,超简单的。。。。暂时就不讲解怎么个来的了,加了简单的注释,大家应该能看懂代码

<?php
header("Content-Type:text/html;charset=gbk");
error_reporting(0);
?> <html>
<body> <form action="" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form> <?php
//数据库连接
$link = mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);
if (!$link) {
die(&#39;Could not connect to MySQL: &#39; . mysql_error());
}
mysql_select_db(&#39;test&#39;); //插入
$name=$_POST[&#39;name&#39;];
$age=$_POST[&#39;age&#39;];
if(isset($name)&&isset($age))
{ $sql="insert into user (name,age) values (&#39;$name&#39;,$age)";
if(mysql_query($sql))
echo "Insert Success!";
else
echo "Insert Fail!".mysql_error(); } //分页查询
if(isset($_GET[&#39;pager&#39;]))
$pager=($_GET[&#39;pager&#39;]-1)*5;
else
$pager=0; $sql="select * from user";
$result=mysql_query($sql);
$num=mysql_num_rows($result); //总共记录数
$page=ceil($num/5); //总共多少页 这里每页5条记录 ceil函数四舍五入的。。
for($i=1;$i<=$page;$i++)
echo "<a href=&#39;index.php?pager=".$i."&#39;>".$i."</a>"; $
sql="select * from user limit $pager,5";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
?>
<form action="" method="post">
ID: <?=$row[&#39;id&#39;]?>
Name: <input type="text" name="_name" value="<?=$row[&#39;name&#39;]?>"/>
Age: <input type="text" name="_age" value="<?=$row[&#39;age&#39;]?>" />
<input type="hidden" name="id" value="<?=$row[&#39;id&#39;]?>" />
<input type="submit" value="修改"/>
<a href="index.php?uid=<?=$row[&#39;id&#39;]?>">删除</a>
</form> <?php
} echo "总共".$num."记录<br/>";
echo "每行".mysql_num_fields($result)."字段"; //修改
$name=$_POST[&#39;_name&#39;];
$age=$_POST[&#39;_age&#39;];
$id=$_POST[&#39;id&#39;];
if(isset($name)&&isset($age)&&isset($id))
{ $sql="update user set name=&#39;$name&#39;,age=&#39;$age&#39; where id=$id";
if(mysql_query($sql))
header("location:index.php");
else
echo "Update Fail!".mysql_error(); }
//删除
$uid=$_GET[&#39;uid&#39;];
if(isset($uid))
{ $sql="delete from user where id=$uid";
if(mysql_query($sql))
header("location:index.php");
else
echo "Delete Fail!".mysql_error(); }
mysql_close($link); ?>
</body>
</html>
Copy after login

暂时先到这里了
php后面内容还有很多,还有对象等知识,光数据操作就还有面向对象的

 以上就是php学习正式起航(6)的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template