Hướng dẫn what is mysql_select_db() function? - Hàm mysql_select_db () là gì?
Hàm Show
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.freetuts.net, không được copy dưới mọi hình thức. Cú phápCú pháp: Trong đó::
Kết quả trả vềHàm sẽ trả về True nếu thay đổi database thành công, ngược lại hàm trả về False.True nếu thay đổi database thành công, ngược lại hàm trả về False. Bài viết này được đăng tại [free tuts .net] Ví dụCách sử dụng hàm Code $con=mysqli_connect("localhost","my_user","my_password","my_db"); // Check connection if (mysqli_connect_errno()){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // ...some PHP code for database "my_db"... // Change database to "test" mysqli_select_db($con,"test"); // ...some PHP code for database "test"... mysqli_close($con); Tham khảo: w3schools.com exit("Error access records"); ("Error access records"); exit("Connection Failed: " . $dbh); (odbc_fetch_row($result)) { Show
Bạn đã tạo một liên kết ODBC đến cơ sở dữ liệu Microsoft Access là Northwindfreetuts.net, không được copy dưới mọi hình thức. exit("Connection Failed: " . $dbh);Hàm Nội dung chính Show:
$contact_name = odbc_result($result, "ContactName"); = odbc_result($result, "ContactName");while (odbc_fetch_row($result)) { "Company Name (Contact Person): $company_name ($contact_name) "; True nếu thay đổi database thành công, ngược lại hàm trả về False. echo $e -> getMessage(); > ?>
$con=mysqli_connect("localhost","my_user","my_password","my_db"); // Check connection if (mysqli_connect_errno()){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // ...some PHP code for database "my_db"... // Change database to "test" mysqli_select_db($con,"test"); // ...some PHP code for database "test"... mysqli_close($con);
$con=mysqli_connect("localhost","my_user","my_password","my_db"); // Check connection if (mysqli_connect_errno()){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // ...some PHP code for database "my_db"... // Change database to "test" mysqli_select_db($con,"test"); // ...some PHP code for database "test"... mysqli_close($con);3 là tùy chọn, nó sử dụng cho ODBC user name mysql_create_db()
Ví dụ bên dưới đây giả sử: Bạn đang sử dụng hệ điều hành Windows Bạn đã tạo một liên kết ODBC đến cơ sở dữ liệu Microsoft Access là Northwind("Connection Failed: " . $dbh); exit("Connection Failed: " . $dbh); while (odbc_fetch_row($result)) { mysql_drop_db()Hàm này dùng để xoá cơ sở dữ liệu. int mysql_drop_db(string name, int [link_identifier]); Trong đó: - name: Tên của cơ sở dữ liệu cần xoá. - link_identifier: Mã số nhận dạng được cấp bởi hàm mysql_connect(). Chúng ta hoàn toàn có thể gửi câu lệnh SQL để xoá cơ sở dữ liệu thông qua hàm mysql_query(). mysql_select_db()Hàm này dùng để chọn cơ sở dữ liệu hoạt động trên website của bạn. int mysql_select_db(string database_name, int [link_identifier]); Trong đó: - name: Tên của cơ sở dữ liệu cần xoá. - link_identifier: Mã số nhận dạng được cấp bởi hàm mysql_connect(). Chúng ta hoàn toàn có thể gửi câu lệnh SQL để xoá cơ sở dữ liệu thông qua hàm mysql_query(). Hàm này dùng để chọn cơ sở dữ liệu hoạt động trên website của bạn.có một bộ sưu tập của các hàm MySQLi được xây dựng sẵn để thao tác cơ sở dữ liệu. int mysql_select_db(string database_name, int [link_identifier]);
mysqli_close function PHP Data Access Object PDO Cách thao tác với CSDL sử dụng hàm MySQLi trong PHPmysqli_connect được sử dụng để kết nối với một MySQL database server. 1. Hàm mysqli_connect trong PHP Hàm mysqli_connect được sử dụng để kết nối với một MySQL database server. mysqli_connect được sử dụng để kết nối với một MySQL database server. = mysqli_connect($db_server_name, $db_user_name, $db_password); Hàm mysqli có cú pháp sau:
$con=mysqli_connect("localhost","my_user","my_password","my_db"); // Check connection if (mysqli_connect_errno()){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // ...some PHP code for database "my_db"... // Change database to "test" mysqli_select_db($con,"test"); // ...some PHP code for database "test"... mysqli_close($con);7 là tên hoặc địa chỉ IP của hosting MySQL Server
2. Hàm mysqli_select_db Các hàm mysqli_select_db được sử dụng để chọn một cơ sở dữ liệu.mysqli_select_db được sử dụng để chọn một cơ sở dữ liệu.(connection, dbname); Hàm mysqli_select có cú pháp như sau:
mysqli_select_db()3 Xác định kết nối MySQL để sử dụng
3. Hàm mysqli_query trong PHP Các hàm mysqli_query() thực hiện một truy vấn đến databasemysqli_query() thực hiện một truy vấn đến database(connection, query, resultmode); Cú pháp hàm mysqli_query():
Đối với kết quả truy vấn SELECT, SHOW, DESCRIBE, hoặc EXPLAIN thành công sẽ trả về object. Các kết quả truy vấn thành công khác trả về mysqli_select_db()1. Nếu không thì trả về mysqli_select_db()2.SELECT, SHOW, DESCRIBE, hoặc EXPLAIN thành công sẽ trả về object. Các kết quả truy vấn thành công khác trả về mysqli_select_db()1. Nếu không thì trả về mysqli_select_db()2.> Các truy vấn được truyền vào là SQL. Nếu bạn chưa biết về SQL thì HỌC SQL CĂN BẢN tại đây nhé.HỌC SQL CĂN BẢN tại đây nhé.mysqli_num_rows() trả về số lượng hàng trong một tập kết quả. 4. Hàm mysqli_num_rows trong PHP
Cú pháp hàm mysqli_num_rows():
5. Hàm mysqli_fetch_array trong PHP Hàm mysqli_fetch_array() tìm nạp một hàng kết quả dưới dạng một mảng kết hợp, một mảng số hoặc cả hai.> Lưu ý: Các fieldname trả về từ hàm này là phân biệt hoa thường.mysqli_fetch_array() tìm nạp một hàng kết quả dưới dạng một mảng kết hợp, một mảng số hoặc cả hai.> Lưu ý: Các fieldname trả về từ hàm này là phân biệt hoa thường.(result, resulttype);
Hàm mysqli_fecth_array trả về một mảng chuỗi tương ứng với các lấy hàng. Null nếu không có thêm hàng nào trong kết quả trả về. 6. Hàm mysqli_close() trong PHPHàm mysqli_close() thực hiện việc đóng kết nối cơ sở dữ liệu đã kết nối trước đó.mysqli_close() thực hiện việc đóng kết nối cơ sở dữ liệu đã kết nối trước đó.mysqli_close() thực hiện việc đóng kết nối cơ sở dữ liệu đã kết nối trước đó. Cú pháp hàm mysqli_close(): mysqli_close(connection); (connection); (connection);
Giá trị trả về của hàm mysqli_close() là True nếu thành công và False nếu thất bạihàm mysqli_close() là True nếu thành công và False nếu thất bạihàm mysqli_close() là True nếu thành công và False nếu thất bại 7. Ví dụ sử dụng các hàm MySQLi đã cho để kết nối với cơ sở dữ liệu.Hãy cùng xem các ví dụ thực tế tận dụng các hàm MySQLi này. Trước tiên ta tạo một database bằng MySQL. Giả định bạn đã biết về MySQL và SQL. Nếu không thì bạn chỉ cần copy code là được. Chúng ta sẽ tạo một cơ sở dữ liệu đơn giản gọi là 3 bước để tạo cơ sở dữ liệu và bảng:
CREATE TABLE IF NOT EXISTS `my_contacts` ( `my_contacts` ( `my_contacts` ( `id` int(11) NOT NULL AUTO_INCREMENT, int(11) NOT NULL AUTO_INCREMENT, int(11) NOT NULL AUTO_INCREMENT, `full_names` varchar(255) NOT NULL, varchar(255) NOT NULL, varchar(255) NOT NULL, `gender` varchar(6) NOT NULL, varchar(6) NOT NULL, varchar(6) NOT NULL, `contact_no` varchar(75) NOT NULL, varchar(75) NOT NULL, varchar(75) NOT NULL, `email` varchar(255) NOT NULL, varchar(255) NOT NULL, varchar(255) NOT NULL, `city` varchar(255) NOT NULL, varchar(255) NOT NULL, varchar(255) NOT NULL, `country` varchar(255) NOT NULL, varchar(255) NOT NULL, varchar(255) NOT NULL, PRIMARY KEY (`id`)KEY (`id`)KEY (`id`) ) ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 5 ;5 ;5 ; INSERT INTO `my_contacts` (`my_contacts` (`my_contacts` ( `id`,,, `full_names`, , , `gender`,,, `contact_no`,,, `email`,,, `city`,,, `country`))) VALUES (1, 'Zeus', 'Male', '111', '', 'Agos', 'Greece'),1, 'Zeus', 'Male', '111', '', 'Agos', 'Greece'),1, 'Zeus', 'Male', '111', '', 'Agos', 'Greece'), (2, 'Anthena', 'Female', '123', '', 'Athens', 'Greece'), 2, 'Anthena', 'Female', '123', '', 'Athens', 'Greece'), 2, 'Anthena', 'Female', '123', '', 'Athens', 'Greece'), (3, 'Jupiter', 'Male', '783', '', 'Rome', 'Italy'),3, 'Jupiter', 'Male', '783', '', 'Rome', 'Italy'),3, 'Jupiter', 'Male', '783', '', 'Rome', 'Italy'), (4, 'Venus', 'Female', '987', 'venu @planet.pt.co', 'Mars', 'Italy');4, 'Venus', 'Female', '987', 'venu @planet.pt.co', 'Mars', 'Italy'); 4, 'Venus', 'Female', '987', 'venu @planet.pt.co', 'Mars', 'Italy'); ?> > > Bây giờ chúng ta có một cơ sở dữ liệu được thiết lập mà chúng ta sẽ thao tác từ PHP. Đọc các bản ghi từ cơ sở dữ liệu. Bây giờ chúng ta sẽ tạo một chương trình in các bản ghi từ cơ sở dữ liệu. $dbh = mysqli_connect('localhost', 'root', 'melody'); = mysqli_connect('localhost', 'root', 'melody'); = mysqli_connect('localhost', 'root', 'melody'); // Kết nối tới MySQL server if (!$dbh) (!$dbh) (!$dbh) die("Unable to connect to MySQL: " . mysqli_error());("Unable to connect to MySQL: " . mysqli_error());("Unable to connect to MySQL: " . mysqli_error()); // Nếu kết nối thất bại thì đưa ra thông báo lỗi if (!mysqli_select_db($dbh,'my_personal_contacts')) (!mysqli_select_db($dbh,'my_personal_contacts')) (!mysqli_select_db($dbh,'my_personal_contacts')) die("Unable to select database: " . mysqli_error()); ("Unable to select database: " . mysqli_error()); ("Unable to select database: " . mysqli_error()); // Thông báo lỗi nếu chọn CSDL thất bại $sql_stmt = "SELECT * FROM my_contacts"; = "SELECT * FROM my_contacts"; = "SELECT * FROM my_contacts"; // Câu lệnh select $result = mysqli_query($dbh,$sql_stmt); = mysqli_query($dbh,$sql_stmt); = mysqli_query($dbh,$sql_stmt); // Thực thi câu lệnh SQL if (!$result) (!$result) (!$result) die("Database access failed: " . mysqli_error()); ("Database access failed: " . mysqli_error()); ("Database access failed: " . mysqli_error()); // & nbsp; Thông & nbsp; báir & nbsp; lỗi & nbsp; $ hàng & nbsp; = & nbsp; mysqli_num_rows ($ result); & nbsp; & nbsp; = & nbsp; mysqli_num_rows ($ result); & nbsp; = mysqli_num_rows($result); // & nbsp; lấy & nbsp; if & nbsp; ($ rows) & nbsp; {& nbsp; ($ rows) & nbsp; {{ ($rows) { while ($row = mysqli_fetch_array($result)) { ($row = mysqli_fetch_array($result)) { ($row = mysqli_fetch_array($result)) { echo & nbsp; 'id: & nbsp;' & nbsp;. & nbsp; $ row ['id'] & nbsp;. & nbsp; ''; : & nbsp; '& nbsp;. & nbsp; $ row [' id '] & nbsp;. 'ID: ' . $row['id'] . ' echo & nbsp; 'full & nbsp; tên: & nbsp;' & nbsp;. & nbsp; $ row ['full_names'] & nbsp; 'full & nbsp; tên: & nbsp;' & nbsp;. 'Full Names: ' . $row['full_names'] . ' echo & nbsp; 'giới tính: & nbsp;' & nbsp;. & nbsp; $ row ['giới tính'] & nbsp;. & nbsp; ''; : & nbsp; '& nbsp;. & nbsp; $ row [' giới tính '] 'Gender: ' . $row['gender'] . ' echo 'Contact No: ' . $row['contact_no'] . ''; 'Contact No: ' . $row['contact_no'] . ''; 'Contact No: ' . $row['contact_no'] . ' echo & nbsp; 'email: & nbsp;' & nbsp;. & nbsp; $ row ['email'] & nbsp;. & nbsp; ''; & nbsp; 'Email: & nbsp;' & nbsp;. 'Email: ' . $row['email'] . ' echo 'City: ' . $row['city'] . ''; 'City: ' . $row['city'] . ''; 'City: ' . $row['city'] . ' echo & nbsp; 'quốc gia: & nbsp;' & nbsp;. & nbsp; $ row ['quốc gia'] & nbsp;. & nbsp; ''; ; $ row ['quốc gia'] & nbsp;. & nbsp; ' 'Country: ' . $row['country'] . ' ' ;} } mysqli_close ($ dbh); & nbsp; // & nbsp;($dbh); // Đóng kết nối CSDL ?> & nbsp;> & nbsp;> Bây Giờ Chún Ta Có Một Cơ Sở Dữ Liệu ĐượC Thiết Lập Mà Chún Ta Sẽ Thao Tác từ Php. & NBSP; Đ-c Các bản GHI Từ Cơ Sở Dữ LIệU. Bây GIờ chún ta sẽ tạo một chương trình trong các bản ghi từ cơ sở dữ liệu. // & nbsp; kết & nbsp; nối & nbsp; tới & nbsp; if & nbsp; (! $ dbh) & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; $dbh = mysqli_connect('localhost', 'root', 'melody'); = mysqli_connect('localhost', 'root', 'melody'); = mysqli_connect('localhost', 'root', 'melody'); // & nbsp; nếu & nbsp; kết & nbsp; nối & nbsp; thất & nbsp; if (!$dbh) (!$dbh) (!$dbh) chết ("không thể & nbsp; to & nbsp; select & nbsp; cơ sở dữ liệu: & nbsp;" & nbsp;. & nbsp; mysqli_error ()); .("Unable to connect to MySQL: " . mysqli_error()); // & nbsp; Thông & nbsp; báir & nbsp; lỗi & nbsp; if (!mysqli_select_db($dbh, 'my_personal_contacts')) (!mysqli_select_db($dbh, 'my_personal_contacts')) (!mysqli_select_db($dbh, 'my_personal_contacts')) chết ("không thể & nbsp; to & nbsp; select & nbsp; cơ sở dữ liệu: & nbsp;" & nbsp;. & nbsp; mysql_error ()); & nbsp; ("không thể & nbsp; ;("Unable to select database: " . mysql_error()); // & nbsp; Thông & nbsp; báir & nbsp; lỗi & nbsp; chết ("không thể & nbsp; to & nbsp; select & nbsp; cơ sở dữ liệu: & nbsp;" & nbsp;. & nbsp; mysql_error ()); & nbsp; ("không thể & nbsp; ; = "INSERT INTO `my_contacts` (`full_names`,`gender`,`contact_no`,`email`,`city`,`country`)"; $ sql_stmt & nbsp; = & nbsp; "chèn & nbsp; vào & nbsp;` my_contacts` & nbsp; & nbsp; .= "VALUES('Poseidon','Mail','541',' ','Troy','Ithaca')"; $ sql_stmt & nbsp;. = & nbsp; "giá trị ('poseidon', 'mail', '541', '& nbsp; & nbsp;', 'troy', 'ithaca')" 'Poseidon', 'mail', '541', '& nbsp; & nbsp;', 'troy', 'ithaca') "; & nbsp; = mysqli_query($dbh, $sql_stmt); // Thực thi câu lệnh SQL if (!$result) { (!$result) { (!$result) { $ result & nbsp; = & nbsp; mysqli_query ($ dbh, & nbsp; $ sql_stmt); /& nbsp; thực & nbsp; thi & nbsp; câu & nbsp; lệNH & nbsp; sql("Adding record failed: " . mysqli_error()); chết ("Thêm & nbsp; record & nbsp; không thành công: & nbsp;" & nbsp;. & nbsp; mysqli_error ()); & nbsp; . } else {else {else { echo "Poseidon has been successfully added to your contacts list"; "Poseidon has been successfully added to your contacts list"; "Poseidon has been successfully added to your contacts list"; } mysqli_close($dbh); // Đóng kết nối CSDL ($dbh); // Đóng kết nối CSDL ($dbh); // Đóng kết nối CSDL ?> > > Thực thi các câu lệnh trên cho kết quả “Poseidon has been successfully added to your contacts list”, quay trở lại ví dụ truy vấn đã chọn và truy xuất lại contacts. Xem nếu Poseidon đã được thêm vào danh sách. Cập nhật bản ghi, bây giờ hãy xem một ví dụ cập nhật một bản ghi trong cơ sở dữ liệu. Giả sử Poseidon đã thay đổi số điện thoại và địa chỉ email của mình. $dbh = mysqli_connect('localhost', 'root', 'melody'); = mysqli_connect('localhost', 'root', 'melody'); = mysqli_connect('localhost', 'root', 'melody'); // Kết nối tới MySQL Server if (!$dbh) (!$dbh) (!$dbh) die("Unable to connect to MySQL: " . mysqli_error()); ("Unable to connect to MySQL: " . mysqli_error()); ("Unable to connect to MySQL: " . mysqli_error()); // Thông báo lỗi nếu kết nối thất bại if (!mysqli_select_db($dbh,'my_personal_contacts')) (!mysqli_select_db($dbh,'my_personal_contacts')) (!mysqli_select_db($dbh,'my_personal_contacts')) die("Unable to select database: " . mysql_error()); ("Unable to select database: " . mysql_error()); ("Unable to select database: " . mysql_error()); // Thông báo lỗi nếu chọn CSDL thất bại $sql_stmt = "UPDATE `my_contacts` SET `contact_no` = '785',`email` = ''"; = "UPDATE `my_contacts` SET `contact_no` = '785',`email` = ''"; = "UPDATE `my_contacts` SET `contact_no` = '785',`email` = ''"; $sql_stmt .= "WHERE `id` = 5"; .= "WHERE `id` = 5"; .= "WHERE `id` = 5"; $result = mysqli_query($dbh,$sql_stmt); = mysqli_query($dbh,$sql_stmt); = mysqli_query($dbh,$sql_stmt); // Thực thi câu lệnh SQL if (!$result) { (!$result) { (!$result) { die("Deleting record failed: " . mysqli_error());("Deleting record failed: " . mysqli_error()); ("Deleting record failed: " . mysqli_error()); // Thông báo lỗi nếu thực thi thất bại } else {else {else { echo "ID number 5 has been successfully updated"; "ID number 5 has been successfully updated"; "ID number 5 has been successfully updated"; } mysqli_close($dbh); //close the database connection($dbh); //close the database connection($dbh); //close the database connection ?> > > Thực thi các câu lệnh trên cho kết quả “Poseidon has been successfully added to your contacts list”, quay trở lại ví dụ truy vấn đã chọn và truy xuất lại contacts.Xem nếu Poseidon đã được thêm vào danh sách. $dbh = mysqli_connect('localhost', 'root', 'melody'); = mysqli_connect('localhost', 'root', 'melody'); = mysqli_connect('localhost', 'root', 'melody'); // Kết nối tới MySQL Server if (!$dbh) (!$dbh) (!$dbh) die("Unable to connect to MySQL: " . mysqli_error()); ("Unable to connect to MySQL: " . mysqli_error()); ("Unable to connect to MySQL: " . mysqli_error()); // Thông báo lỗi nếu kết nối thất bại if (!mysqli_select_db($dbh,'my_personal_contacts')) (!mysqli_select_db($dbh,'my_personal_contacts')) (!mysqli_select_db($dbh,'my_personal_contacts')) die("Unable to select database: " . mysql_error()); ("Unable to select database: " . mysql_error()); ("Unable to select database: " . mysqli_error()); // Thông báo lỗi nếu chọn CSDL thất bại $id = 4; = 4; = 4; $sql_stmt .= "WHERE `id` = 5"; .= "WHERE `id` = 5"; // Thực thi câu lệnh SQL = "DELETE FROM `my_contacts` WHERE `id` = $id"; // Thông báo lỗi nếu thực thi thất bại $result = mysqli_query($dbh,$sql_stmt); = mysqli_query($dbh,$sql_stmt); = mysqli_query($dbh,$sql_stmt); // Thực thi câu lệnh SQL if (!$result) { (!$result) { (!$result) { chết ("xóa & nbsp; record & nbsp; không thành công: & nbsp;" & nbsp;. & nbsp; mysqli_error ()); ("xóa & nbsp;("Deleting record failed: " . mysqli_error()); // & nbsp; Thông & nbsp; báir & nbsp; lỗi & nbsp; } else {else {else { echo "ID number $id has been successfully deleted"; "ID number $id has been successfully deleted"; "ID number $id has been successfully deleted"; } mysqli_close ($ dbh); & nbsp; // & nbsp;($dbh); // Đóng kết nối CSDL ?> & nbsp;> & nbsp;> Xem nếu Poseidon Đạo Đan Thce Vào Danh Sách. Cập NHật Bản GHI, Bây Giờ Hãy Xem Một Ví Dụ Cập Nhật Một BảN GHI Trong Cơ Dữ LIệU. Giả Sử Poseidon Đạo Thay Đổi Số Điện Thoại Và Địa Chỉ Email Của Mình. try { { { $ dbh & nbsp; = & nbsp; mysqli_connect ('localhost', & nbsp; 'root', & nbsp; 'melody'); & nbsp; & nbsp; = & nbsp; new & nbsp; pdo ("mysql: host = localhost; dbname = my_personal_contacts", & nbsp; 'root', & nbsp; 'melody'); = new PDO("mysql:host=localhost;dbname=my_personal_contacts", 'root', 'melody'); $pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo -> exec('SET NAMES "utf8"');-> exec('SET NAMES "utf8"');-> exec('SET NAMES "utf8"'); $ sql_stmt & nbsp; = & nbsp; "select & nbsp;*& nbsp; from & nbsp;` my_contacts` "; = "SELECT * FROM `my_contacts`"; $ result & nbsp; = & nbsp; $ pdo-> truy vấn ($ sql_stmt); & nbsp; = & nbsp; $ pdo-> truy vấn ($ sql_stmt); = $pdo->query($sql_stmt); $result -> setFetchMode(PDO::FETCH_ASSOC);-> setFetchMode(PDO::FETCH_ASSOC);-> setFetchMode(PDO::FETCH_ASSOC); $data = array(); = array(); = array(); foreach & nbsp; ($ result & nbsp; as & nbsp; $ row) & nbsp; {& nbsp; ($ result & nbsp; as & nbsp; $ row) & nbsp; {{ ($result as $row) { $data[] = $row;[] = $row;[] = $row; } print_r($data);($data);($data); } catch (PDOException $e) { (PDOException $e) { (PDOException $e) { mysqli_close ($ dbh); & nbsp; // & nbsp; $e -> getMessage(); } mysqli_close ($ dbh); & nbsp; // & nbsp;>
$ dbh & nbsp; = & nbsp; mysqli_connect ('localhost', & nbsp; 'root', & nbsp; 'melody'); & nbsp; & nbsp; = & nbsp; new & nbsp; pdo ("mysql: host = localhost; dbname = my_personal_contacts", & nbsp; 'root', & nbsp; 'melody'); $ sql_stmt & nbsp; = & nbsp; "select & nbsp;*& nbsp; from & nbsp;` my_contacts` "; = odbc_connect($dsn, $user_name, $password);
?> & nbsp;
$dbh = odbc_connect('northwind', '', ''); = odbc_connect('northwind', '', ''); = odbc_connect('northwind', '', ''); if (!$dbh) { (!$dbh) { (!$dbh) {
} $sql_stmt = "SELECT * FROM customers"; = "SELECT * FROM customers"; = "SELECT * FROM customers"; $result = odbc_exec($dbh, $sql_stmt); = odbc_exec($dbh, $sql_stmt); = odbc_exec($dbh, $sql_stmt); if (!$result) { (!$result) { (!$result) {
} ODBC lÀ viết Tắt CủA Kết nối cơ sở dữ liệu mở. Nó Có Cú ph tòa cơ bản Sau. & Nbsp; = & nbsp; odbc_connect ($ dsn, & nbsp; $ user_name, & nbsp; $ password); & nbsp; (odbc_fetch_row($result)) { $company_name = odbc_result($result, "CompanyName"); = odbc_result($result, "CompanyName"); = odbc_result($result, "CompanyName"); $ Conn & nbsp; = & nbsp; odbc_connect ($ dsn, & nbsp; $ user_name, & nbsp; $ password); & nbsp; = odbc_result($result, "ContactName");
} odbc_close($dbh);($dbh);($dbh); mysqli_close ($ dbh); & nbsp; // & nbsp;> ?> & nbsp;> & nbsp;Xem nếu Poseidon Đạo Đan Thce Vào Danh Sách.hàm MySQLi cơ bản để sử dụng thao tác với CSDL trong PHP, cũng như cách thức sử dụng PDO để thay thế.
$conn = odbc_connect($dsn, $user_name, $password); LẬP TRÌNH WEB PHP. Khóa học do doanh nghiệp liên kết đào tạo nhân lực, học thực tế, tuyển dụng ngay cuối khóa.LẬP TRÌNH WEB PHP. Khóa học do doanh nghiệp liên kết đào tạo nhân lực, học thực tế, tuyển dụng ngay cuối khóa.
Ví dụ bên dưới đây giả sử: Bạn đang sử dụng hệ điều hành Windows Bạn đã tạo một liên kết ODBC đến cơ sở dữ liệu Microsoft Access là Northwind exit("Connection Failed: " . $dbh); |