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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Staff online

      • Ghan
        Administrator - Servers are fun

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top