Php check if query success
Lets say I have a categories table and then I have products table. Show Each product belongs to some category. Now, I want to remove a category which is assigned to some products. This will cause an error, because I can not remove something from database because of database relations. My code below removes a row from a table (using jquery) but it removes the category for real only if it no product is using it. I am
trying to create some error warning when I click on EDIT: I have tried to use
Here is script:
And sql query: (PHP 4, PHP 5) mysql_query — Send a MySQL query Descriptionmysql_query(string Parametersquery An SQL query The query string should not end with a semicolon. Data inside the query should be properly escaped. link_identifier
The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() had been called with no arguments. If no connection is found or established, an
Return Values For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns
The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data. Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement. mysql_query() will also fail and return ExamplesExample #1 Invalid Query The following query is syntactically invalid, so mysql_query() fails and returns
Example #2 Valid Query The following query is valid, so mysql_query() returns a resource.
See Also
jack dot whoami at gmail dot com ¶ 15 years ago
halion at gmail dot com ¶ 15 years ago
nikhil-php at nols dot com ¶ 22 years ago
Anonymous ¶ 16 years ago
mysql_query($query); Mr. Tim ¶ 14 years ago
davidc at edeca dot net ¶ 19 years ago
ialsoagree ¶ 13 years ago
ddlshack [at] gmail.dot.com ¶ 11 years ago
mysql_query('INSERT INTO `'.$table.'` (`'.implode('`,`', $keys).'`) VALUES (\''.implode('\',\'', $values).'\')'); masteracc0 at aol dot com ¶ 14 years ago
rob desbois ¶ 16 years ago
veyita_angi at hotmail dot com ¶ 16 years ago
php at arcannon dot com ¶ 17 years ago
Anonymous ¶ 19 years ago
rogier ¶ 14 years ago
cc+php at c2se dot com ¶ 16 years ago
mysql_query_params__callback( $at ) { ix at nivelzero dot ro ¶ 17 years ago
fernandoleal at loytek dot com ¶ 14 years ago
mwwaygoo at hotmail dot com ¶ 10 years ago
jon at websandbox dot net ¶ 17 years ago
Anonymous ¶ 13 years ago
Predrag Supurovic ¶ 18 years ago
allen a brooker gb net ¶ 19 years ago
Richie (at) RichieBartlett.com ¶ 13 years ago
= mysql_connect('YOUR_DB_ADDRESS','YOUR_DB_USER','YOUR_DB_PASS') or die("Database error");
chris at hotmail dot com ¶ 19 years ago
wjyong at sh263 dot net ¶ 17 years ago
How do I know if a PHP query is successful?php check if query succeeded. // peform a query.. $query = "SELECT `*` FROM user";. $results = mysqli_query($databaseConnection, $query);. if (mysqli_num_rows($results) == 0) {. // The query returned 0 rows!. } else {. // the query returned ATLEAST one row.. How do I check if SQL query is correct in PHP?Linked. check if the query results empty row mysqli.. Run a PHP function if returned SQL data matches a specific value.. How to Display a message when no results found in PHP MySQL search.. Check whether mysql query returned an empty resultset.. Query result is true instead of false.. How can we check insert is successful in SQL PHP?To check if your INSERT was successful, you can use mysqli_affected_rows() . Returns the number of rows affected by the last INSERT, UPDATE, REPLACE or DELETE query. And check for errors against your query and for PHP.
How check MySQL update query was successful in PHP?Use if(mysqli_affected_rows($mysqli) >0 ) or no comparison at all. Sidenote: ==1 is only comparing for 1, as opposed to >0 which you may be trying to update more than one row. However and on the rare occasion, >0 is required where this has also happened to me before; that is the reason of my answer.
|