I am using the JQuery .post method to get data using an AJAX call. The PHP file that is rendering the data has the following code that inputs information into a database:
$query = "INSERT INTO questions(question, added_by) VALUES ('$question', '$user_id')";
$result = mysql_result($query);
//If db error
if(!$result )
{
$error = str_replace("'", "*", mysql_error());
$method = __METHOD__.'line: '.__LINE__;
return Error::db_error($method, $error, $this->ip, 'An internal error has occurred. Your question can not be added at this time.');
}
According to this code, a database error is occurring, yet mysql_error() is blank. When I use die($query), and copy and paste the literal query string into my mysql gui window (sqlyog) and run the query, it inserts just fine with no warnings or errors.
I have this general set-up for some other PHP functions and it works just fine.
I am really stumped by this. Any help would be greatly appreciated. Thanks.
Execute the query using mysql_query before Retrieving the contents of cells from a MySQL result set using mysql_result.
$result = mysql_query($query);
echo mysql_result($result);
Could it be an encoding issue? What is your contentType on the Ajax post?
Related
I'm trying to fill various html selects with their respective information. For that I feel the right way is to make a query for each of them. I do the first query with this php tag to fill the first select like this:
<?php
$sql = "call mydb.getPositions();";
$result = $conn->query($sql);
while($row = $result->fetch_row()){
echo "<option>".$row[0]."</option>";
}
?>
That's it. Simple. It works. The function getPositions() is a select. But when I tried to fill the next select/combobox with it's own query, it started throwing this error:
Fatal error: Call to a member function fetch_row() on a non-object in file.php in line 200
I searched for a lot of reasons for a mistake like typos in the query or fetching rows from a delete query.
But then to discard any mistake of those, I decided to copy the exact same php tag one after the other. So, if the first works, why wouldn't the second work? So there I saw that I was receiving the exact same mistake. What am I missing? I tried to close the $result, with no success.
Thanks in advance.
EDIT.
When I said "I decided to copy the exact same php tag one after the other" I really meant that like:
<?php
$sql = "call mydb.getPositions();";
$result = $conn->query($sql);
while($row = $result->fetch_row()){
echo "<option>".$row[0]."</option>";
}
?>
<?php
$sql = "call mydb.getPositions();";
$result = $conn->query($sql);
//It throws the error down here
while($row = $result->fetch_row()){
echo "<option>".$row[0]."</option>";
}
?>
And this throws the same error described before.
EDIT2.
The procedure is this:
CREATE DEFINER=`mainSoccer`#`%` PROCEDURE `getPositions`()
BEGIN
Select namePosition from mydb.Position;
END
It looks like your question is actually: how do I run the same MySQL query twice in a php script? If so, here's the answer:
<?php
$sql = "Select namePosition from mydb.Position;";
$result = $conn->query($sql);
while($row = $result->fetch_row()){
echo "<option>".$row[0]."</option>";
}
?>
<!-- presumably you have other code in between here -->
<?php
$sql = "Select namePosition from mydb.Position;";
$result = $conn->query($sql);
while($row = $result->fetch_row()){
echo "<option>".$row[0]."</option>";
}
?>
And in your comment to my original answer below, you asked "Why shouldn't I be creating a MySQL procedure?" Two answers:
Your initial code is creating the same procedure twice. That is surely throwing an error at the MySQL level. Generally one creates the procedure once for permanent re-use directly in your server, so that many different scripts can them simply invoke (not create) that procedure at any time.
You should use MySQL procedures for complex, large MySQL operations that have performance issues that can gain efficiency as a pre-compiled procedure. Your query here is very simple and does not call for a MySQL procedure.
Hope this helps.
Since you only posted your code that works, but not the code that doesn't work, you can't get specific help.
But I can tell you that the error you quote "Fatal error: Call to a member function fetch_row() on a non-object in file.php in line 200" is typically caused when you write invalid SQL.
First confirm that the SQL you are attempting runs on its own as raw SQL, for example in phpMyAdmin.
I'm getting data back from an AJAX call, which I then try and INSERT into a database. I'm using Silverstripe 3.1 (using DB::query()), but this seems to be a PHP thing.
If the transaction works then everything works as planned, however, if the query fails I get a PHP fatal error.
Basically I'm looking for a way to continue despite a fatal error so that I can throw a failed notification to a user. I'd just like to know if the query worked or not.
*Edit to include Code
$sql = "UPDATE $table SET ,,"; // Syntax error added purposefully
foreach ($update_array as $key => $value) {
$name = $formHelpers->some_filter($key);
$content = $formHelpers->some_filter($value);
$sql .= " $name='$content',";
}
$sql = rtrim($sql, ",");
$sql .= "WHERE id=$id";
$result = DB::query($sql);
I'd need to see your code to tell you exactly what you need to do. However, the concept you are looking for is called try, catch, throw. When you make your database query, add a catch statement. Then if an error is tossed, you can catch it and still do something. Here is a tutorial about Try, Catch, Through in PHP
http://www.w3schools.com/php/php_exception.asp
The following code takes data from a form to be retrieved from a remote database.
$find = mysqli_real_escape_string($connect, $_POST['name']);
echo ' '.$find;
$query_seek = mysqli_query($connect, "SELECT * FROM test_2 WHERE NAME = '$find' ");
if($query = $query_seek)
{
echo 'query successful';
}
When I run it, the query does not seem to resolve or echo any data. Could it be as simple as using a get method versus post? I prefer to not have the query pass through the URL for security but if can't be helped so be it. I assume the syntax for the SQL to be correct as I ran it on the server side through PHPMyAdmin's SQL client. Anything glaringly incorrect?
This block comes after a check where the input is verified for length and no white space.
How do I see what is returned from a sql statement in php? I have the following function to get user name from mysql database and I use echo in another php to see the result but nothing shown.
function get_user_name($id_user) {
return mysql_result(mysql_query("SELECT username FROM user WHERE id_user = '$id_user'"));
}
echo $id_user;
$a = get_user_name($id_user);
echo $a;
Can anyone help? Thanks.
Are you echoing the get_user_name(); function?? OR are you even connected to your database? these are two things you need to check before, (if the problem remains) including an error handling method i.e. or die(mysql_error()) at the end of your query to find out the problem.
return mysql_result(mysql_query("SELECT id_user FROM user WHERE id_user = '$id_user'")or die (mysql_error()));
The error handling construct?? in mysql mysql_error() should output the problem in fairly understandable way, as to what is preventing your query not to be shown
I created a debug function to email me the mysql error and query executed if a query is not successful.
I call it like this:
mysql_query($sql) or $this->debug->dbErrors($sql);
And the function is:
function dbErrors($sql = ''){
if($this->doDebug)
echo mysql_error()."<br/>".$sql;
else
#mail(hidden_email,$_SERVER['HTTP_HOST'].' Mysql Error','A error occured in '.$_SERVER['HTTP_HOST'].':<br/>'.mysql_error().'<br/>'.$sql);
}
The problem is that i'm receiving emails even when the query executes fine (at least the data is inserted and everything works out ok)
What i doing anything wrong?
Thanks
That 'or' construct may be causing issue, I would do something like:
$result = mysql_query($sql);
if (!$result) {
$this->debug->dbErrors($sql);
}
This way you are doing an explicit check to see if $result is a boolean false (query is invalid), or a resource (query is valid). The point is to only call on $this->debug->dbErrors() if there indeed is an issue, otherwise the way your code is written, every query will be emailed.
or something simple like:
mysql_query($sql) or die(dbErrors($sql));