-->

PHP资讯

您的当前位置:首页 > 资讯 > PHP基础

php基础-操作数据库

2020-03-23 10:29:49  来源:admin 点击:797

5.5前链接库

mysql_connect(servername,username,password);

<?php

$con = mysql_connect("localhost","peter","abc123");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

// some code

mysql_close($con);

?>

创建表

<?php

$con = mysql_connect("localhost","peter","abc123");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

if (mysql_query("CREATE DATABASE my_db",$con))

  {

  echo "Database created";

  }

else

  {

  echo "Error creating database: " . mysql_error();

  }

mysql_close($con);

?>

插入

INSERT INTO table_name VALUES (value1, value2,....)

查询

SELECT column_name(s) FROM table_name

更新

UPDATE table_name SET column_name = new_value WHERE column_name = some_value

删除

DELETE FROM table_name WHERE column_name = some_value


7.0后

$host = "yourHost";

$user = "yourUser";

$pass = "yourPass";

$db   = "test";

$port = 3306;

try

{

  $dbh = new PDO("mysql:host=$host;dbname=$db", $user, $pass);

  echo "Connected<p>";

}

catch (Exception $e)

{

  echo "Unable to connect: " . $e->getMessage() ."<p>";

}

$sql = "select  SQL_NO_CACHE * from test_user;";

$tmp = array();

for ($i=1; $i<=500000; $i++) {

  $ret = $dbh->query($sql); 

  foreach ($ret as $row) {

    $tmp['id']   = $row['id'];

    $tmp['name'] = $row['name'];

  }

}

相关文章更多 >

© 2024版权所有 水水网

电话:171780793