官网上面有面向对象的写法,您可以到php官网参考一下。
mysqli的面向过程基本上和mysql的思路差不多,也是先连接,选择数据库,查询,取结果...
$sql = "SELECT * FROM table"
/* 连接 */
$con = mysqli_connect("localhost","user","password","dbname")
if (!$con)
{
die("连接错误: " . mysqli_connect_error())
}
/* 查询 */
$Result = mysqli_query($con,$sql)
/* 遍历结果集 */
while($data = mysqli_fetch_assoc($Result))
{
$Data_Username = $data["username"]
$Data_Password = $data["password"]
echo "用户名:{$Data_Username},密码:{$Data_Password}"
}
/* 释放查询结果 */
mysqli_free_result($Result)
/* 关闭连接 */
mysqli_close($con)
import java.sql.*
public class SqlUtil
{
public static void main(String[] args)
{
try
{
String url="jdbc:mysql://localhost/mydb"
String user="root"//用户名
String pwd="123456"//密码
//加载驱动,这一句也可写为:Class.forName("com.mysql.jdbc.Driver")
Class.forName("com.mysql.jdbc.Driver").newInstance()
//建立到MySQL的连接
Connection conn = DriverManager.getConnection(url,user, pwd)
//执行SQL语句
Statement stmt = conn.createStatement()//创建语句对象,用以执行sql语言
ResultSet rs = stmt.executeQuery("select * from student")
//处理结果集
while (rs.next())
{
String name = rs.getString("name")
System.out.println(name)
}
rs.close()//关闭数据库
conn.close()
}
catch (Exception ex)
{
System.out.println("Error : " + ex.toString())
}技术问题可到itjob技术群交流
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)