欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

PHP学习笔记:用mysqli连接数据库 有大用

PHP学习笔记:用mysqli连接数据库

小插曲,晚上把数据的my.ini编码改为utf-8,然后数据库一直不能启动,改回gbk就可以,有知道的告知下问题所在。

因为是链接数据库,也没什么好说明的,直接上代码吧。

复制代码
<?php

/* Connect to a MySQL server  连接数据库服务器 */
$link = mysqli_connect(
    'localhost',  /* The host to connect to 连接MySQL地址 */
    'jian',      /* The user to connect as 连接MySQL用户名 */
    '123456',  /* The password to use 连接MySQL密码 */
    'jian');    /* The default database to query 连接数据库名称*/

if (!$link) {
    printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());
    exit;
}else
    echo '数据库连接上了!';

/* Close the connection 关闭连接*/
mysqli_close($link);
?>
复制代码

这里的jian其实是其中一个名字为jian的库的管理员,不需要写数据库管理员,因为实际项目我们可能得到的就是一个库管理员的数据库账号、密码、还有主机地址。

测试结果:

 

原来的数据库表:

操作1:查询分数大于60的学生id,班级和分数

SELECT id,class,scores  FROM jian_scores WHERE scores>60

全部代码:

复制代码
<?php

/* Connect to a MySQL server  连接数据库服务器 */
$link = mysqli_connect(
    'localhost',  /* The host to connect to 连接MySQL地址 */
    'jian',      /* The user to connect as 连接MySQL用户名 */
    '123456',  /* The password to use 连接MySQL密码 */
    'jian');    /* The default database to query 连接数据库名称*/

if (!$link) {
    printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());
    exit;
}else
    echo '数据库连接上了!'. "<br/>";

if ($result = mysqli_query($link, 'SELECT id,class,scores  FROM jian_scores WHERE scores>60 ')) {

echo('id  班级 分数 '). "<br/>";

/* Fetch the results of the query 返回查询的结果 */
while( $row = mysqli_fetch_assoc($result) ){
    echo  $row['id'], "&nbsp;", $row['class'], "&nbsp;",  $row['scores'], "<br/>";
   // printf("%s (%s) ",  $row['id'],$row['class'], $row['scores']);
}

/* Destroy the result set and free the memory used for it 结束查询释放内存 */
mysqli_free_result($result);
}


/* Close the connection 关闭连接*/
mysqli_close($link);
?>
复制代码

结果:

 


来自 https://www.cnblogs.com/jianqingwang/p/5890111.html



php mysqli query 查询数据库后读取内容的方法

php mysqli query 查询数据库后读取内容的方法

<?php
$mysqli 
= new mysqli("localhost""my_user""my_password""world");

/* check connection */
if ($mysqli->connect_errno) {
    
printf("Connect failed: %s\n"$mysqli->connect_error);
    exit();
}

/* Create table doesn't return a resultset */
if ($mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
    
printf("Table myCity successfully created.\n");
}

/* Select queries return a resultset */
if ($result $mysqli->query("SELECT Name FROM City LIMIT 10")) {
    
printf("Select returned %d rows.\n"$result->num_rows);

    
/* free result set */
    
$result->close();
}

/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result $mysqli->query("SELECT * FROM City"MYSQLI_USE_RESULT)) {

    
/* Note, that we can't execute any functions which interact with the
       server until result set was closed. All calls will return an
       'out of sync' error */
    
if (!$mysqli->query("SET @a:='this will not work'")) {
        
printf("Error: %s\n"$mysqli->error);
    }
    
$result->close();
}

$mysqli->close();
?>

实例代码:

$sql="select * from qa limit 0,10";
 $result=$mysqli->query($sql);
 while ($row = mysqli_fetch_assoc($result))
  {
    echo "question_title : {$row['question_title']} <br>";
    echo "answer_content1: {$row['answer_content1']} <br>";
  }
 /* free result set */
    $result->close();

参考文档:http://www.php.net/manual/en/mysqli.query.php



来自  https://www.cnblogs.com/zdz8207/p/3745486.html

普通分类: