Jump to content

check for mysql results


Julius

Recommended Posts

Hello,

 

I'm trying to make my first database class. I thought I will create a function for select, delete, update and insert, but then I thought, what if I'm going to use join while selecting something? and then I realised, that I need only one function, called query. Now, the issue is that I don't know how to make function know that I'm selecting some data from database, or I'm updating data. If I select data, I want my result variable to be filled with results. I hope you understand my question-problem. I want this to be smart, so if you have any ideas - please share.

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/250558-check-for-mysql-results/
Share on other sites

A function/method that executes a query should only return the true (insert/update queries), false (any query that failed due to an error), or instance of a result class (select/show queries) from the query that was executed, nothing more. It is your main code that calls the query function/method that is aware of what type of query was being executed and knows what to test and to do with the returned true, false, instance of a result class. A result class would have functions/methods that allow you to actually fetch the data that a successful select/show query returns.

I'm pretty new in php oop and mysql. I don't know how to make my function so it could see the difference between select and insert, update, delete queries.

<?php

 

class DB {

public $results = array ( );

 

public function query ( $query )

{

    $qr = mysql_query ( $query );

    // now, if this was select query, i want to store results in $this -> results

    // otherwise do nothing or return true if query succeeded.

}

}

?>

All your code needs to do is TEST for a true, false, or result resource from the mysql_query() statement. It doesn't care or need to know what type of query was executed.

 

Edit: see is_resource to test if a resource was returned. If a resource was not returned, then you got a true or a false value and you can just return that value.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.