简体   繁体   中英

No database found!! mysql error

     //require_once("../StoredProcedure/connect.php");
  $conn=mysql_connect("localhost","root","") or die(mysql_error);
  mysql_select_db("politicalforum",$conn); 

 function updateThread($threadID, $content) 
 {
     mysql_query("UPDATE threads
                  SET content='$content'  
                  WHERE thread_id=$threadID") ;
    // $res = mysql_query($sql) or trigger_error(mysql_error().$sql);
     mysql_close();
 }

I get this each time..what am I doing wrong?

No database selected.

I am calling that function from an outside file...like this..

if(isset($_GET['threadID']) && isset($_POST["reply"]) && isset($_POST['textareas']))
   {

        updateThread($_GET['threadID'], $_POST['textareas']); 

       $postValid=TRUE;
   }

Your connection is likely going out of scope, causing the mysql_query to run against the context's database object which doesn't exist by the time updateThread fires.

In this case you need to pass the connection into the mysql_query function inside updateThread . Architecturally there are two ways you can go about this:

1) Open and close the connection inside your updateThread function:

function updateThread($threadID, $content) 
{
   $conn=mysql_connect("localhost","root","") or die(mysql_error);
   mysql_select_db("politicalforum",$conn); 
   mysql_query("UPDATE threads
                SET content='$content'  
                WHERE thread_id=$threadID", $conn) ;
   mysql_close($conn);
   $conn = null;
}

2) Pass the connection as a variable to updateThread, in case you want to use the same connection in other functions. PHP will automatically close and dispose your MySQL connection when the script finishes:

function updateThread($threadID, $content, $conn) 
{
   mysql_query("UPDATE threads
                SET content='$content'  
                WHERE thread_id=$threadID", $conn) ;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM