mySql Delete only removes the data not the entire entry - php

Hi all I have this line in my php code
`$insert = "DELETE FROM allocation_table WHERE job = '$jobnumber' " ;
Mysql_query ($insert) ;`
The problem is it will remove all the values from the one line in my table but not the entry itself. as you can see in the picture if I delete where job = 315 , it does not delete the line but does delete all the entries
Yet in this code that preceeds it (a different table) . it works fine and the whole line is removed
$insert = "DELETE FROM event WHERE jobnumber = '$jobnumber' " ;
mysql_query ($insert) ;enter code here
can anyone offer some advice please ?? alt text http://img34.imageshack.us/img34/4431/tablenj.jpg

Do you have any error handling routines in your code?
You might also want to add some debug output (or even better use a debugger like xdebug)
(oversimplified) example:
$insert = "DELETE FROM allocation_table WHERE job = '$jobnumber' " ;
echo '<pre>Debug: query=', htmlspecialchars($insert), '</pre>';
$rc = mysql_query($insert);
if ( !$rc ) {
echo '<pre>mysql_query failed: ', mysql_error(), '</pre>';
}
else {
echo '<pre>Debug: affected rows=', mysql_affected_rows(), '</pre>';
}
$insert = "DELETE FROM event WHERE jobnumber = '$jobnumber' " ;
echo '<pre>Debug: query=', htmlspecialchars($insert), '</pre>';
$rc = mysql_query($insert);
if ( !$rc ) {
echo '<pre>mysql_query failed: ', mysql_error(), '</pre>';
}
else {
echo '<pre>Debug: affected rows=', mysql_affected_rows(), '</pre>';
}

Maybe you have some weird setting on your MySQL table that is causing the query to function differently. Did you create the table yourself (with PMA?) or import it from somewhere else?
When you delete using PHPMyAdmin, what is the query that it runs?

Try deleting in PhpMyAdmin and see what happens. At least there you'll be able to easily see any error message.
Do you have foreign keys defined and/or table constraints?

Related

Issue with stored procedure in PHP

I have the following stored procedure that executes correctly when I run my program:
$insertIntoEmployeesProcedure = "
CREATE PROCEDURE EmployeeInsert(name VARCHAR(50),password VARCHAR(50), email VARCHAR(50))
BEGIN
INSERT INTO employees(name,password,email) values(name,password,email);
END";
$returnInsertIntoEmpProc = $conn->query($insertIntoEmployeesProcedure);
if(! $returnInsertIntoEmpProc )
{
die('Could not create insert procedure: ' . $conn->error);
}
else
{
echo "Insert Procedure created successfully<br/>";
}
I then call this procedure in another class when needed:
$insertEmp = mysqli_query($conn, "Call EmployeeInsert('$username','$password', '$email')");
$executeInsertEmp = $conn->query($insertEmp);
if(!$executeInsertEmp )
{
die('Employees not added: ' . $conn->error);
}
else
{
echo "Employees added<br/>";
}
The problem is, when I execute this code, I get the following error
Employees not added: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
The main issue I have with this is that even though it returns this error, the record is still added into the database and everything seems to be working fine. I guess I'm more curious as to why I'm getting this error as clearly I'm overlooking something.
Ah I see what I've done, I seem to have added an additional query which was unnecessary, the line:
$executeInsertEmp = $conn->query($insertEmp);
can be ommited, the check in the if statement is then done on the variable which holds the stored procedure. The following code works:
$insertEmp = mysqli_query($conn, "Call EmployeeInsert('$username','$password', '$email')");
if(!$insertEmp )
{
die('Employees not added: ' . $conn->error);
}
else
{
echo "Employees added<br/>";
}

Commands out of sync, even though first SQL query does not contain results

I have been reading about the Commands out of sync; you can't run this command now problem for some time now, and see that you cannot have any unread results left, which makes sense to me. However, in the following case, I don't see which results I am missing to free. I have left out the irrelevant things from my PHP and SQL code below.
# Set local variables
$sql = "
SET #STARTDATE = '2014-09-01';
SET #RANK = 0;
";
if (mysqli_multi_query($conn, $sql)) {
# Success: do nothing else
} else {
# Failure: output the error message
echo "Error: " . $sql . "<br>" . $conn->error;
}
# Fetch and store the results
$sql = "
SELECT * FROM MyTable
";
$result = mysqli_query($conn, $sql);
if (!$result) {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
The second query (the if (!$result) block) returns the infamous Commands out of sync error. If I comment out the first part, the second query runs no problem. If I change the first query into only one SET statement instead of two, the second query runs no problem. Therefore, it seems that I have to clear the 'success-flag' of every individual SQL statement from the first part. Is this correct? If so, how shall this be done?
EDIT: indeed it seems you have to flush all results in between. Adding the following line between part 1 and part 2 solves the problem.
while (mysqli_next_result($conn)) {;} // Flush multi_queries
I found this solution in a user comment on the PHP manual: http://nl3.php.net/manual/en/mysqli.multi-query.php
Quite simply, your first query
SET #STARTDATE = '2014-09-01';
SET #RANK = 0;
Will generate 2 result sets and until they have been processed, even though the result will be just a status you cannot continue.
So you need to do something like this :-
if (mysqli_multi_query($conn, $sql)) {
do {
/* unload result set */
if ($result = $mysqli->store_result()) {
// Check status
$result->free();
}
} while ($mysqli->next_result());
} else {
# Failure: output the error message
echo "Error: " . $sql . "<br>" . $conn->error;
}
Of course you should probably check for errors in that loop

mysql_fetch problems... I'm getting crazy

I'm sorry, probably somewhere there will be the answer to my question, but it's hours I'm looking for trying to resolve this problem:
Here is the code:
<?php
$con = mysql_connect("****","****","***");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("******", $con);
$query = "SELECT id FROM fq_questions";
$rowcnt = mysql_num_rows(mysql_query($query));
echo "Tot scenes: ".$rowcnt;
$id = rand (1,$rowcnt);
echo "<br>Id rand: ".$id."<br>";
flush();
$newquery = "SELECT question FROM fq_questions WHERE id=".$id;
$result = mysql_query($newquery);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $newquery;
die($message);
}
$row = mysql_fetch_assoc($result);
echo $row['question'];
mysql_close($con);
?>
The problem is that there is no output. I've tried everything, seems to be a problem into the query, but there is a result, it's not false, but even if it exists, nothing is outputted.
The code works till
echo "<br>Id rand: ".$id."<br>";
then it shows nothing.
It's a dummy problem, i'm getting crazy just because of it.
Uh, was forgetting... The website where I've got the problem: http://www.freelabs.it/filmquiz/game.php
Be careful, desc is a SQL keyword ! Your query may not compile because of that.
"desc" is a MySQL reserved word, you should just change that column name in your DB. Anyway, your method is not random at all, and it will fail as soon as you have a "hole" in your ids (when you delete one member).
Take a look at MySQL "ORDER BY RAND()"
$data = mysql_query("SELECT description FROM fq_questions ORDER BY RAND() LIMIT 1");
You used mysql_fetch_row() which returns a numerical array. You then try to access the array slot named 'desc'.
It doesn't exist. (My guess is that that produces a supressed error, preventing any output from showing up, or a supressed warning, preventing any output after that line from showing up.)
Try changing mysql_fetch_row() to mysql_fetch_assoc() (still DEPRECATED!) and that should be solved.
Sources: http://php.net/manual/en/function.mysql-fetch-row.php
And: http://php.net/manual/en/function.mysql-fetch-assoc.php

Having trouble displaying last update time of mySQL table

So I have a website and I am trying to display the last update time of the mySQL server, I've looked around but still having problems. Here is my code
$sql = 'SHOW TABLE STATUS FROM alumni LIKE "alumni_data"';
$tableStatus = mysqli_query($link, $sql);
if (!$tableStatus) {
$error = 'Error getting update status: ' . mysqli_error($link);
include 'error.html.php';
exit();
}
while ($array = mysqli_fetch_array($tableStatus)) {
$updatetime = $array['Update_time'];
}
echo '<center>Last Updated: ' . $updatetime . '</center>';
What happens is nothing prints out, its like it never found the update time. I have manually typed that query so I am pretty sure it works.
Thanks

using a PHP script to return values from SQL table

I am attempting to search a SQL table from a PHP script. the SQL table is a word list and in the PHP I call a Python script to do the permutations of a given word. Everything is working until I actually go to execute the mysql_query. I need some formatting advice on how to pass so many values to a Select statement. From what I've seen it needs to be in the form ('a','b','c',...) and this is how it is formatted but I'm not getting a return on the actual execution.
<?php
ini_set('display_errors','On');
error_reporting(E_ALL);
$con= mysql_connect("127.0.0.1","app","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("wordlookup");
//Retrieve the data.
$word = $_POST['data'];
$perm = exec("python comb.py $word");
$query="SELECT * FROM words WHERE IN (" .$perm. ")";
print $query;
$sql=mysql_query($query);
print $sql;
mysql_close($con);
?>
This is all of the PHP file, the output from the python call would be in the 'a','b','c'... format and the random prints are just debugging. The print $sql doesn't actually put out anything at the moment.
Thanks,
Kyle
SELECT * FROM words WHERE IN (...)
Your query is missing a condition. WHERE what IN ...? You need to fill in a column name there.
Further, if a query is not working, ask the database why it didn't work:
$result = mysql_query($query);
if (!$result) {
echo mysql_error();
}
Though solution is strange (use external script executed via exec to calculate permutations?), I'd answer your exact question.
$perm_arr = explode(' ', $perm);
function quote($str) { return "'$str'"; }
$perm_arr = array_map('quote', $perm_arr);
$sql = "select * from words where word in (" . join(', ', $perm_arr) . ")";

Categories