企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# php操作数据库 ``` <pre class="calibre15">``` <span class="token"><</span><span class="token">?</span>php <span class="token6">//测试原生链接数据库</span> $servername <span class="token">=</span> <span class="token4">"localhost"</span><span class="token1">;</span> $userename <span class="token">=</span> <span class="token4">"ceshi"</span><span class="token1">;</span> $password <span class="token">=</span> <span class="token4">"ceshiceshi"</span><span class="token1">;</span> $dbname <span class="token">=</span> <span class="token4">"test"</span><span class="token1">;</span> <span class="token6">//创建链接</span> $con <span class="token">=</span> <span class="token3">mysqli_connect</span><span class="token1">(</span>$servername<span class="token1">,</span>$userename<span class="token1">,</span>$password<span class="token1">,</span>$dbname<span class="token1">)</span><span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span><span class="token">!</span>$con<span class="token1">)</span><span class="token1">{</span> echo <span class="token3">mysqli_connect_error</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token6">// /*******插入一个数据****************/</span> <span class="token6">// $sql = "insert into `ceshibiao` (username,password) values ('aaa','bbb')";</span> <span class="token6">// if (mysqli_query($con,$sql)) {</span> <span class="token6">// echo '新纪录插入成功';</span> <span class="token6">// }else{</span> <span class="token6">// echo mysqli_error($con);</span> <span class="token6">// }</span> <span class="token6">//插入多条数据 /*******插入多条数据****************/</span> <span class="token6">// $sql = "insert into `ceshibiao` (username,password) values ('a1a1','b1b1');";</span> <span class="token6">// $sql .= "insert into `ceshibiao` (username,password) values ('a2a2a2','b2b2b2');";</span> <span class="token6">// $sql .= "insert into `ceshibiao` (username,password) values ('a3a3a3','b3b3b3')";</span> <span class="token6">// if (mysqli_multi_query($con,$sql)) {</span> <span class="token6">// echo '新纪录插入成功';</span> <span class="token6">// }else{</span> <span class="token6">// echo mysqli_error($con);</span> <span class="token6">// }</span> <span class="token6">//读取数据 /*********** 读取多条数据 *********/</span> <span class="token6">// $sql = "select id,username,password from `ceshibiao`";</span> <span class="token6">// $result = mysqli_query($con,$sql);</span> <span class="token6">// echo mysqli_num_rows($result); //查询数量</span> <span class="token6">// while ($row = mysqli_fetch_assoc($result)) {</span> <span class="token6">// var_dump($row['username']);</span> <span class="token6">// }</span> <span class="token6">//读取数据 /*********** 查询一条数据 *********/</span> $sql <span class="token">=</span> <span class="token4">"select id,username,password from `ceshibiao` where id=1"</span><span class="token1">;</span> $result <span class="token">=</span> <span class="token3">mysqli_query</span><span class="token1">(</span>$con<span class="token1">,</span>$sql<span class="token1">)</span><span class="token1">;</span> <span class="token6">//$row = mysqli_fetch_assoc($result);//["id"]=> string(1) "1" ["username"]=> string(4) "1111" ["password"]=> string(4) "2222" } </span> <span class="token6">//$row = mysqli_fetch_row($result);//{ [0]=> string(1) "1" [1]=> string(4) "1111" [2]=> string(4) "2222" } </span> <span class="token3">var_dump</span><span class="token1">(</span>$row<span class="token1">)</span><span class="token1">;</span> echo <span class="token4">'链接成功'</span><span class="token1">;</span> <span class="token3">mysqli_close</span><span class="token1">(</span>$con<span class="token1">)</span><span class="token1">;</span> ``` ```