測試選擇查詢是否找到結果 (Testing if a Select Query found results)


問題描述

測試選擇查詢是否找到結果 (Testing if a Select Query found results)

Hi I'm new to PHP and would really appreciate if someone could tell me where I'm going wrong in this code.  To put the code in context, I'm using the facebook to authenticate users and adding their signup details to my app database.  In the following code intend to check if the user already exists in the database and if not add them.  For some reason I can't test the $result 

I've checked the query variables and they echo out without a problem

@ $con = new mysqli('localhost', 'username', 'password', 'database');
if(mysqli_connect_errno()){
    echo 'Error: Could not connect to the database. Please try again later';
    exit;
}

$query = "SELECT * FROM users WHERE oauth_provider = 'facebook' AND oauth_uid ='".$uid."'";

$result = $con->query($query);


if($result === FALSE){
    $insertquery = "INSERT INTO ('oauth_provider', 'oauth_uid', 'username') VALUES ('facebook', '".$uid."', '".$username."')";
    $result = $con->query($query);
}

I should probably add I have the code working using the older mysql approach. But I have read it is better to use the object-oriented mysqli approach.

Here's the old working code

if($session){
$con = mysql_connect('localhost', 'user', 'password');
$select_db = mysql_select_db('database');

if(!$con || !$select_db){
    die('Could not connect: ' . mysql_error());
}
else{
    echo "connected to database and table selected";
}

$query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'facebook' AND oauth_uid = ". $user['id']);
$result = mysql_fetch_array($query);

if(empty($result)){
    $query = mysql_query("INSERT INTO users (oauth_provider, oauth_uid, username) VALUES ('facebook', {$user['id']}, '{$user['name']}')");
    $query = mysql_query("SELECT * FROM users WHERE id = " . mysql_insert_id());
    $result = mysql_fetch_array($query);
}

}

Any help you can give me is appreciated.


參考解法

方法 1:

Use the MySQLi_Result::num_rows property to get the number of rows of a MySQLi_Result object:

if ($result->num_rows > 0) {
    // …
}

方法 2:

It can be done as simple as:

if($result)
{
// Do code when there are results
} else {
// Do code when there are no results
}

方法 3:

if( $result instanceof mysqli_result && $result->num_rows==0)

break down:

$result instanceof mysqli_result

this is to ensure query go through and results return

$result->num_rows==0

this is to check the previous query matching any existing records

(by willmcneillyGumboCoen Jacobsajreal)

參考文件

  1. Testing if a Select Query found results (CC BY-SA 3.0/4.0)

#mysqli #PHP






相關問題

如何第一次創建 mysqli 數據庫? (how can I creates a mysqli database for first the first time?)

使用 jquery autosuggest 從多個表中選擇 (select from multiple tables with jquery autosuggest)

無法連接 - 數據庫或編碼錯誤 (Could Not Connect - Database or Coding Error)

mysqli忽略表中的第一行 (mysqli ignoring the first row in a table)

從數據庫導入的鏈接以 UTF8 截斷 (imported link from database cut off at UTF8)

試圖獲取非對象的屬性 (Trying to get property of non-object)

獲取 php 和 mysqli 中插入數據的 ID (Get Id of inserted data in php and mysqli)

WAMP 和 mysqli::real_connect(): (HY000/2002)? (WAMP and mysqli::real_connect(): (HY000/2002)?)

如何將 MySQLi 準備好的語句的結果放入關聯數組中? (How can I put the results of a MySQLi prepared statement into an associative array?)

測試選擇查詢是否找到結果 (Testing if a Select Query found results)

為什麼我的正確連接嘗試在 apache2 下失敗但在 cli 下失敗? (Why is my correct connection attempt failing under apache2 but not under cli?)

在帶有數組的 IN 語句之後,在 WHERE 語句中使用更多佔位符 (Use more placeholders in WHERE statement after IN statement with arrays)







留言討論