<!doctype html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta charset="utf-8"> <title>my sql</title> <meta name="keywords" content="geovindu"> <meta name="description" content="涂聚文"> </head> <body> <?php // php 7.27 mysql 5.7 geovindu 涂聚文 $mysql_conf = array( 'host' => '127.0.0.1:3306', 'db' => 'sakila', 'db_user' => 'root', 'db_pwd' => '888888', ); $link = mysqli_connect($mysql_conf['host'],$mysql_conf['db_user'], $mysql_conf['db_pwd'],$mysql_conf['db']); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s ", mysqli_connect_error()); exit(); } $query = "select * from city ORDER BY city_id LIMIT 5;"; // 获取表格字段属性 if ($result = mysqli_query($link, $query)) { /* Get field information for all fields */ while ($finfo = mysqli_fetch_field($result)) { printf("Name: %s ", $finfo->name); printf("Table: %s ", $finfo->table); printf("max. Len: %d ", $finfo->max_length); printf("Flags: %d ", $finfo->flags); printf("Type: %d ", $finfo->type); echo("<br/>"); } mysqli_free_result($result); } // echo("<table border='1' align='center'><tr align='center'>"); // 显示字段名 if ($result = mysqli_query($link, $query)) { while($field=mysqli_fetch_field($result)) { echo("<td>".$field->name."</td>"); } mysqli_free_result($result); } echo("</tr>"); // 显示行数据 if ($resultrow = mysqli_query($link, $query)) { while($row=mysqli_fetch_row($resultrow)) { echo("<tr>"); for($i=0;$i<mysqli_field_count($link);$i++) { echo("<td>".$row[$i]."</td>"); } echo("</tr>"); } } echo("</table>"); /* close connection */ mysqli_close($link); ?> </body> </html>
<!doctype html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta charset="utf-8"> <title>my sql</title> <meta name="keywords" content="geovindu"> <meta name="description" content="涂聚文"> </head> <body> <?php // php 7.27 mysql 5.7 /* character_set_client utf8 character_set_connection utf8 character_set_database utf8 -- 安装mysql时要设置的,所要考虑的 character_set_filesystem binary character_set_results utf8 character_set_server utf8 character_set_system utf8 */ session_start(); $mysql_conf = array( 'host' => '127.0.0.1:3306', 'db' => 'sakila', 'db_user' => 'root', 'db_pwd' => '770214', ); // 连接无效 //http://php.net/manual/zh/function.mysql-connect.php 本扩展自 PHP 5.5.0 起已废弃,并在自 PHP 7.0.0 开始被移除 /* $mysqli = mysqli_connect($mysql_conf['host'],$mysql_conf['db_user'], $mysql_conf['db_pwd'],$mysql_conf['db']); if(!$mysqli) { echo "Error: Unable to connect to MySQL." . PHP_EOL; echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL; echo "Debugging error: " . mysqli_connect_error() . PHP_EOL; exit;//诊断连接错误 } $mysqli->set_charset('utf8'); mysqli_query($mysqli,'set names utf8'); */ //Verbindungen zu mySQL aufbauen $mysqli = new mysqli($mysql_conf['host'],$mysql_conf['db_user'], $mysql_conf['db_pwd'],$mysql_conf['db']); //Verbindung überprüfen if ($mysqli -> connect_errno) { printf("无法建立连接数据库: %s ", $mysqli->connect_error); exit(); } //1. /* if (!$select_db) { die("could not connect to the db: " . $mysqli->error); } */ //$mysqli->set_charset('utf8'); //mysqli_query($mysqli,'set names utf8'); $mysqli->query("set names 'utf8';");//编码转化 $sql = "select * from city where city_id = 601;"; // $res = $mysqli->query($sql); //mysqli_query($con,"SELECT * FROM Persons"); //$result = $mysqli -> prepare($sql); //$result -> execute(); $fcoun=mysqli_field_count($mysqli); printf("geovindu"); echo("字段数:".$fcoun); // 显示字段名称 where city_id = 601 echo("<table border='1' align='center'><tr align='center'>"); $sql = "select * from city where city_id = 601;"; //$res = $mysqli->query($sql); /* if ($res=mysqli_query($mysqli,$sql)) { // 获取所有列的字段信息 while ($fieldinfo = mysqli_fetch_field($res)) { printf("字段名: %s ", $fieldinfo->name); echo "<br>"; printf("数据表: %s ", $fieldinfo->table); echo "<br>"; printf("最大长度: %d ", $fieldinfo->max_length); echo "<br>"; } // 释放结果集 mysqli_free_result($res); } */ /**/ if ($resultd = mysqli_query($mysqli, $sql)) { // Get field information for all fields while ($finfo = mysqli_fetch_field($resultd)) { printf("Name: %s ", $finfo->name); printf("Table: %s ", $finfo->table); printf("max. Len: %d ", $finfo->max_length); printf("Flags: %d ", $finfo->flags); printf("Type: %d ", $finfo->type); echo("<br/>"); } mysqli_free_result($resultd); } // 显示字段名 if ($resfield = mysqli_query($mysqli, $sql)) { while($field=mysqli_fetch_field($resfield)) { echo("<td>".$field->name."</td>"); } mysqli_free_result($resfield); echo("</tr>"); } //显示某行 where city_id = 601; $sql = "select * from city"; $res =$mysqli->query($sql);// mysql_query($sql); while($rowd=$res->fetch_row()) { echo("<tr/>"); for($i=0;$i<$res->field_count;$i++) { //echo("<td>".$res->fields['city']."</td>"); echo("<td>".$rowd[$i]."</td>"); } echo("</tr>"); } echo("</table>"); //echo("hi,geovindu"); // $sql = "select * from city"; $res = $mysqli->query($sql);//mysql_query($sql); // while ($rowfield = mysqli_fetch_array($res,MYSQLI_ASSOC)) //MYSQLI_BOTH MYSQLI_NUM { echo($rowfield['city'] . ' ' . $rowfield['country_id']."<br/>"); } $res->free(); $mysqli->close(); mysqli_close($mysqli); ?> </body> </html>