MYSQL No valid object - but does it all?!

Anachron

New Member
Reaction score
53
MSQL Error.. Mysql fetch object is no valid.. but it does the full action it should?!

Ok the loop ($i) are threads I want to delete.. The question database is a database with questions of the thread ( The threads are surveys ).. I just want to know which syntax I've broken...

The tables:

Threads
id Primary key
name (Varchar)
text (Varchar)
owner (Varchar)
date (Varchar)
ended (bool)

questions
id Primary key
thread (Varchar)
text (Varchar)
PHP:
<?php
echo "Die Befragungen werden gelöscht... <br>";

for ($i=1;$i<=$count;$i++)
{ 
if ($_POST["lehr_fb_d$i"]!="" and $_POST["lehr_fb_u$i"]!="")
{
$lehr_fb_d=$_POST["lehr_fb_d$i"];

$db_serv="localhost";
$db_table_n="lfb_threads";
$db_dbname="lfb_databases";
$mysql_user_n="root";
$mysql_user_p="";
include ("inc_dbcon.inc.php");
$action="SELECT * FROM ".$db_table_n. " WHERE date='$lehr_fb_d'";
$action_done=mysql_query($action);
while($row=mysql_fetch_object($action_done) )
  {
    $data_get[0]=$row -> date;
    $data_get[1]=$row -> id;
    if ($data_get[0]==$lehr_fb_d)
    {
        $tid=$data_get[1];    
        $db_serv2="localhost";
        $db_table_n2="lfb_questions";
        $db_dbname2="lfb_databases";
        $mysql_user_n2="root";
        $mysql_user_p2="";

        $db_handle2=mysql_connect($db_serv2, $mysql_user_n2, $mysql_user_p2);
        $db_database2=mysql_select_db($db_dbname2);
        $action2="SELECT * FROM ".$db_table_n2." WHERE thread=$tid";
//This line should be infected with no valid resource.. but it still does the whole job        $action_done2=mysql_query($action2);
        while($row2=mysql_fetch_object($action_done2) )
        {
        $data_get1[0]=$row2 -> thread;
        if ($data_get1[0]==$tid)
        { 
        $action3="DELETE FROM ".$db_table_n2." WHERE thread=$tid";
        $action_done2=mysql_query($action3);
        }
        }
        mysql_close($db_handle2);
    }
  }
mysql_close($db_handle);
$db_serv="localhost";
$db_table_n="lfb_threads";
$db_dbname="lfb_databases";
$mysql_user_n="root";
$mysql_user_p="";
include ("inc_dbcon.inc.php");
$action="DELETE FROM ".$db_table_n." WHERE date='$lehr_fb_d'";
$action_done = mysql_query($action);
mysql_close($db_handle);
}
}
?>
 

enouwee

Non ex transverso sed deorsum
Reaction score
240
Mind explaining as text what you're trying to do (yes, German is fine too)? This is the worst piece of code I've seen to date.

Lots of SQL injection. Are you even aware that you're opening and closing multiple DB connections per iteration? This script will be damn slow and put the DB and the server under heavy load just by connecting/disconnecting.

Are you sure that you have a valid DB handle when you first reach that select statement? Aren't you killing one DB connection with the next one?

Code:
$db_serv="localhost";
$db_table_n="lfb_threads";
$db_dbname="lfb_databases";
$mysql_user_n="root";
$mysql_user_p="";
include ("inc_dbcon.inc.php");
$action="SELECT * FROM ".$db_table_n. " WHERE date='$lehr_fb_d'";
$action_done=mysql_query($action);


EDIT: wild guess, but you probably could reduce your whole code to a single query ... if only I had a clue what you're trying to achieve.
EDIT2:sorry, 2 queries.
Code:
begin transaction;

delete from lfb_questions where id in (
  select id from lfb_threads where date in (?);
);
delete from lfb_threads where date in (?);

commit;
 

Anachron

New Member
Reaction score
53
Mind explaining as text what you're trying to do (yes, German is fine too)? This is the worst piece of code I've seen to date.

Lots of SQL injection. Are you even aware that you're opening and closing multiple DB connections per iteration? This script will be damn slow and put the DB and the server under heavy load just by connecting/disconnecting.

Are you sure that you have a valid DB handle when you first reach that select statement? Aren't you killing one DB connection with the next one?

Code:
$db_serv="localhost";
$db_table_n="lfb_threads";
$db_dbname="lfb_databases";
$mysql_user_n="root";
$mysql_user_p="";
include ("inc_dbcon.inc.php");
$action="SELECT * FROM ".$db_table_n. " WHERE date='$lehr_fb_d'";
$action_done=mysql_query($action);


EDIT: wild guess, but you probably could reduce your whole code to a single query ... if only I had a clue what you're trying to achieve.
EDIT2:sorry, 2 queries.
Code:
begin transaction;

delete from lfb_questions where id in (
  select id from lfb_threads where date in (?);
);
delete from lfb_threads where date in (?);

commit;
Well a student has already looked over it and he said he couldn't find the MYSQL error. I know that that is a lot of useless stuff and it can be done easier but I just have PHP at school the first year now. Everything works fine execept he says one error lol,...?!

Edit: OK now again, I am trying to delete the questions of a survey from thread and the thread afterwards... So for example: Thread 1 has been ordered to be deleted. I check in all questions for thread ID of Thread 1 and if yes, it should delete all of them.
 

enouwee

Non ex transverso sed deorsum
Reaction score
240
Well a student has already looked over it and he said he couldn't find the MYSQL error. I know that that is a lot of useless stuff and it can be done easier but I just have PHP at school the first year now. Everything works fine execept he says one error lol,...?!

Given the code quality and your approach of "everything works", why not prefix every mysql_() function by a @ so it masks the error messages? Poof, no more errors. :rolleyes:

Edit: OK now again, I am trying to delete the questions of a survey from thread and the thread afterwards... So for example: Thread 1 has been ordered to be deleted. I check in all questions for thread ID of Thread 1 and if yes, it should delete all of them.

I don't have a MySQL-enabled PHP, nor a MySQL DB, so I can't really test the code. The query works fine on Oracle and SQLite.
PHP:
# you'd better sanitize your $_POST and store them somewhere ... let's say inside an array $dates
# depending on your "date", you have to apply quoting, etc...
$dates = array('123', '234', '345');
$dates = implode (',', $dates);

# the queries
$del_questions = 'delete from lfb_questions where thread in ( select id from lfb_threads where date in (' . $dates . '))';
$del_thread = 'delete from lfb_threads where date in (' . $dates . '))';

# your recycled code... I'd suggest using PDO Mysql (from the PHP 5.2.x branch and bind variables)
$db_serv="localhost";
$db_dbname="lfb_databases";
$mysql_user_n="root";
$mysql_user_p="";

# ??? include ("inc_dbcon.inc.php");


$db_handle=mysql_connect($db_serv, $mysql_user_n, $mysql_user_p);
if (!mysql_select_db($db_dbname, $db_handle))
{
  print "Failed to connect to database.\n";
  exit(-1);
}

if (!mysql_query($del_questions, $db_handle))
{
  print "Failed to delete questions.\n";
  mysql_close($db_handle);
  exit(-2);
}
if (!mysql_query($del_thread, $db_handle))
{
  print "Failed to delete thread.\n";
  mysql_close($db_handle);
  exit(-3);
}
mysql_close($db_handle);
 

Anachron

New Member
Reaction score
53
# ??? include ("inc_dbcon.inc.php");

Thats the connect.. you can remove the connect part with
handle=
database=...^^
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top