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
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.

Link to comment
Share on other sites

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.

}

}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.