excecute if $mysqli->query successfull

jlknauff

New member
Aug 25, 2008
237
1
0
I'm trying to execute something only of the data is successfully inserted into the database, but I'm clearly doing something wrong. I've done as bit of digging around, and it looks like the solutions to similar issues are for older versions of php...I could be wrong through, I'm still relatively new to programming php. Anyway, I've tried to piece together the code based on what I've found, but it doesn't seem to be working - in this case, it executes whether the data is successfully inserted into the database or not.
PHP:
$mysqli->query("INSERT INTO ag_pages (page_date, page_type, page_url, page_keyword, page_title, page_content) VALUES ('$page_date', '$page_type', '$new_page_url', '$new_page_keyword', '$new_page_title', '$new_page_content')");

        if($mysqli){
            stuff to excecute here
}
If I change it to

PHP:
  if($mysqli->query){


Then it doesn't execute at all. I'm at a loss as to what to do here. Any advice?
 


You talking about SQL Transactions. Where you have a set of queries that only "commit" if they are all successfully executed, otherwise they "rollback".

Give this link a read: PHP + MySQL transactions examples - Stack Overflow

You should also look into using the PDO module of PHP so that you can use ODBC for your database calls rather than using specific functions for mySQL in PHP. That way your code is much more portable, as well as less prone to error.

ODBC has their own set of standard proceedures for SQL and can handle just what you're after and will return true if the query executed successfully.

Anyway, you'll be wanting to execute the transaction in a try/catch statement, as with anything, to catch the errors so that you can roll back.
 
Are you familiar with any object-oriented programming (OOP)?
What's happening is, $mysqli is an object instance, and the stuff after the little arrow -> is a function (or 'method' in OOP jargon) of that object, called query().

The following line won't execute:

PHP:
  if($mysqli->query){

Since there's no parenthesis at the end of 'query', the program thinks you're trying to get information about a variable (or 'property') that belongs to the $mysqli object instance. Trouble is, no property called "query" exists.

This statement is actually calling the method correctly:

PHP:
$mysqli->query("long query blah blah blah");

Have you studied return values yet? Almost every function or method in PHP returns a value showing the result of the function.. Pretty much all database querying methods will return a boolean FALSE value if the query doesn't work. That's what you need, and that's what will solve your problem.

To get a return value from any method or function, assign it to a variable.
Say you had an object instance called $hotgirl, with a method askDate, that returns a value. Here's how you'd do it.

PHP:
$isVirgin = $hotGirl->askDate('Please??');

So knowing this, how would you save the result of the query method - whether or not the query was executed successfully?

Pardon me if this seemed too Programming 101, I'm breaking things down for you very simply so you can understand what's going on. I hope that helps.
 
^ so going with what that guy said, you should do:

PHP:
$myQuery = $mysqli->query("INSERT INTO ag_pages (page_date, page_type, page_url, page_keyword, page_title, page_content) VALUES ('$page_date', '$page_type', '$new_page_url', '$new_page_keyword', '$new_page_title', '$new_page_content')");

if ($myQuery) {
// stuff to execute here
}
Just thought I'd make that clear in case you didn't understand.