Now let’s talk about php error handling
<?php $file=fopen("1.txt","r"); ?>
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"); } ?>
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('localhost','root',''); if (!$link) { die('Could not connect to MySQL: ' . mysql_error()); } echo 'Connection OK'; mysql_close($link); ?>
$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
Super simple
Enter phpmyadmin to see
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('localhost','root',''); if (!$link) { die('Could not connect to MySQL: ' . mysql_error()); } mysql_select_db('test',$link); mysql_query("insert into user (name,age) values ('harry',15)",$link); mysql_close($link); ?>
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
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>
<?php $name=$_POST['name']; $age=$_POST['age']; if(isset($name)&&isset($age)) { $link = mysql_connect('localhost','root',''); if (!$link) { die('Could not connect to MySQL: ' . mysql_error()); } mysql_select_db('test'); $sql="insert into user (name,age) values ('$name',$age)"; if(mysql_query($sql)) echo "Insert Success!"; else echo "Insert Fail!".mysql_error(); mysql_close($link); } ?>
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('localhost','root',''); if (!$link) { die('Could not connect to MySQL: ' . mysql_error()); } mysql_select_db('test'); //插入 $name=$_POST['name']; $age=$_POST['age']; if(isset($name)&&isset($age)) { $sql="insert into user (name,age) values ('$name',$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['id'] . "</td>"; echo "<td>" . $row['name'] . "</td>"; echo "<td>" . $row['age'] . "</td>"; echo "</tr>"; echo "</table>"; } mysql_free_result($result); mysql_close($link); ?> </body> </html>
$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>";
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.
//Inquire
$sql="select * from user"; $result=mysql_query($sql); while($row = mysql_fetch_row($result)) { echo "
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 号
重新写下
"; echo "
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('localhost','root',''); if (!$link) { die('Could not connect to MySQL: ' . mysql_error()); } mysql_select_db('test'); //插入 $name=$_POST['name']; $age=$_POST['age']; if(isset($name)&&isset($age)) { $sql="insert into user (name,age) values ('$name',$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['id']?> Name: <input type="text" name="_name" value="<?=$row['name']?>"/> Age: <input type="text" name="_age" value="<?=$row['age']?>" /> <input type="hidden" name="id" value="<?=$row['id']?>" /> <input type="submit" value="修改"/> <a href="index.php?uid=<?=$row['id']?>">删除</a> </form> <?php } echo "总共".mysql_num_rows($result)."记录<br/>"; echo "每行".mysql_num_fields($result)."字段"; //修改 $name=$_POST['_name']; $age=$_POST['_age']; $id=$_POST['id']; if(isset($name)&&isset($age)&&isset($id)) { $sql="update user set name='$name',age='$age' where id=$id"; if(mysql_query($sql)) header("location:index.php"); else echo "Update Fail!".mysql_error(); } //删除 $uid=$_GET['uid']; 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>
至此,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('localhost','root',''); if (!$link) { die('Could not connect to MySQL: ' . mysql_error()); } mysql_select_db('test'); //插入 $name=$_POST['name']; $age=$_POST['age']; if(isset($name)&&isset($age)) { $sql="insert into user (name,age) values ('$name',$age)"; if(mysql_query($sql)) echo "Insert Success!"; else echo "Insert Fail!".mysql_error(); } //分页查询 if(isset($_GET['pager'])) $pager=($_GET['pager']-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='index.php?pager=".$i."'>".$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['id']?> Name: <input type="text" name="_name" value="<?=$row['name']?>"/> Age: <input type="text" name="_age" value="<?=$row['age']?>" /> <input type="hidden" name="id" value="<?=$row['id']?>" /> <input type="submit" value="修改"/> <a href="index.php?uid=<?=$row['id']?>">删除</a> </form> <?php } echo "总共".$num."记录<br/>"; echo "每行".mysql_num_fields($result)."字段"; //修改 $name=$_POST['_name']; $age=$_POST['_age']; $id=$_POST['id']; if(isset($name)&&isset($age)&&isset($id)) { $sql="update user set name='$name',age='$age' where id=$id"; if(mysql_query($sql)) header("location:index.php"); else echo "Update Fail!".mysql_error(); } //删除 $uid=$_GET['uid']; 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>
暂时先到这里了
php后面内容还有很多,还有对象等知识,光数据操作就还有面向对象的
以上就是php学习正式起航(6)的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!