Php check ว า ม ข อม ลหร อเปล า

Khon Kaen University Library 123 Mittraphap Road, Nai Mueang Subdistrict, Mueang District, Khon Kaen Province 40002 Tel: 080-0722554 , 42625 Office 093-5922556 Information Service 097-9322557 Counter Service Fax: 0 4320 2543 Email: [email protected] Internal phone number click here

เข้าชมทั้งหมด: 705,238 ครั้ง วันนี้: 0 ครั้ง | เดือนนี้: 0 ครั้ง | เดือนที่แล้ว: 0 ครั้ง

ใน PHP Programming มีฟังก์ชันที่เกี่ยวข้องกับฐานข้อมูลมากมาย เพื่อให้นักพัฒนาสามารถนำไปใช้ใช้งานได้ง่าย สำหรับความนี้จะอธิบาถึงฟังก์ชัน mysqli_select_db() ซึ่งมีประโยน์อย่างมาก

ฟังก์ชัน mysqli_select_db() เป็นฟังก์ชัน PHP ที่ใช้เพื่อเปลี่ยนฐานข้อมูลเริ่มต้นสำหรับการเชื่อมต่อ

มีรูปแบบ syntax ดังนี้

mysqli_select_db ( mysqli $link , string $dbname ) : bool

$link : ตัวระบุลิงก์ที่ส่งคืนโดยฟังก์ชัน mysqli_connect() หรือ mysqli_init()

dbname : ชื่อฐานข้อมูล

สำหรับการคืนค่านั้น ฟังก์ชันนี้จะส่งคืนค่า TRUE เมื่อสำเร็จหรือ FALSE เมื่อล้มเหลว

ควรใช้ฟังก์ชันนี้เพื่อเปลี่ยนฐานข้อมูลเริ่มต้นสำหรับการเชื่อมต่อเท่านั้น คุณสามารถกำหนดฐานข้อมูลเริ่มต้นได้ที่พารามิเตอร์ที่ 4 ใน mysqli_connect ()

ตัวอย่างโปรแกรม ด้านล่างนี้

<?php  
$link = mysqli_connect("localhost", "my_user", "my_password", "test");
/* check connection */  
if (mysqli_connect_errno()) {  
    printf("Connect failed: %s\n", mysqli_connect_error());  
    exit();  
}
/* return name of current default database */  
if ($result = mysqli_query($link, "SELECT DATABASE()")) {  
    $row = mysqli_fetch_row($result);  
    printf("Default database is %s.\n", $row[0]);  
    mysqli_free_result($result);  
}
/* change db to world db */  
mysqli_select_db($link, "world");
/* return name of current default database */  
if ($result = mysqli_query($link, "SELECT DATABASE()")) {  
    $row = mysqli_fetch_row($result);  
    printf("Default database is %s.\n", $row[0]);  
    mysqli_free_result($result);  
}
mysqli_close($link);  
?>

ผลลัพธ์ที่ได้เมื่อรันแล้ว

Default database is test.  
Default database is world.

จากผลลัพธ์เพื่อให้เห็นว่าก่อนใช้ฟังก์ชันนี้ ฐานข้อมูลเริ่มต้นที่เชื่อมต่อคือ test และเมื่อเราทำการเปลี่ยนฐานข้อมูลเริ่มต้นด้วยฟังก์ชันนี้แล้ว ฐานข้อมูลจะเปลี่ยนเป็น world ครับ

ใน php programming เมื่อเราต้องการรู้จำนวนแถวข้อมูลหรือชุดข้อมูลที่ได้การ query จากฐานข้แอมูล หลายๆคนอาจจะใช้งานคำสั่ง COUNT() ใน MySQL ซึงสามารถให้ข้อมูลตัวเลขได้ แต่ถ้าต้องการให้แสดงข้อมูลด้วย ก็คงต้องเขียนคำสั่ง select จาก MySQL Database อีกครั้ง แต่ในบทความนี้ จะอธิบายถึงฟังก์ชัน mysqli_num_rows ()

mysqli_num_rows () ฟังก์ชั่นเป็นฟังก์ชั่น PHP ใช้ในการส่งกลับจำนวนแถวที่มีอยู่ในชุดผลลัพธ์หรือฐานข้อมูลที่ได้ Query มา โดยทั่วไปจะใช้เพื่อตรวจสอบว่ามีข้อมูลอยู่ในฐานข้อมูลหรือไม่ ในการใช้ฟังก์ชั่นนี้จำเป็นต้องตั้งค่าการเชื่อมต่อกับฐานข้อมูล MySQL ก่อน

รูปแบบ syntex

mysqli_num_rows ( mysqli_result $result ) : int

ส่วนการเขียนในรูปแบบ OOP

int $mysqli_result->num_rows;

พารามิเตอร์ : ฟังก์ชั่นนี้จะรับพารามิเตอร์ $result มันเป็นพารามิเตอร์บังคับและแสดงให้เห็นถึงชุดผลลัพธ์ที่ส่งกลับโดยแบบสอบถามดึงข้อมูลใน MySQL

Return Value : ส่งคืนจำนวนแถวที่มีอยู่ในชุดผลลัพธ์หรือฐานข้อมูล

ตัวอย่างโค้ดการใช้งาน mysqli_num_row()

<?php   
  // Setting up connection with database Geeks   
  $connection = mysqli_connect("localhost", "root", "","mindphp");   
  // Check connection   
  if (mysqli_connect_errno())   
  {   
    echo "Database connection failed.";   
  }   
  // query to fetch Username and Password from   
  // the table geek   
  $query = "SELECT Username, Password FROM geek";   
  // Execute the query and store the result set   
  $result = mysqli_query($connection, $query);   
  if ($result)   
  {   
    // it return number of rows in the table.   
    $row = mysqli_num_rows($result);   
    printf("Number of row in the table : " . $row);   
    // close the result.   
    mysqli_free_result($result);   
  } 
  // Connection close   
  mysqli_close($connection);   
?>   

ผลลัพธ์ที่ได้

Number of row in the table :5

จากผลลัพธ์ที่ได้ คือจำนวนแถวข้อมูลที่ได้ Query มาซึ่งเราสามารถใช้เพื่อตรวจสอบ เรามีข้อมูลจากการ query ไหม เช่น