Warning: mysql_query() [function.mysql-query]: MySQL server has gone away in C:\xampp\htdocs\a_upload\upload.php on line 71                             

    Warning: mysql_query() [function.mysql-query]: Error reading result set's header in C:\xampp\htdocs\a_upload\upload.php on line 71
    GalleryUploadMessageProfileStatistticsChange PasswordDelete Account



                if(!$sql=mysql_query("INSERT INTO image VALUES(NULL, '$user_id_db', '$image', '$image_keyword_p' ,
                '$image_full_name', '$image_short_name_p', '$image_des_p','$file_size' ,'$image_resolution', 0)"))





    Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\a_upload\upload.php on line 130


    LINE 130: $image = mysql_fetch_assoc($image); //get access to image table










----------------------------------------------------------------------------------------------------------------
    upload.php
    <?php
    include("include/header.php");

    $file = isset($_FILES['fileupload']) ? $_FILES['fileupload']['tmp_name'] : false;
    $upload_error = "";

    //check, if user is loged in or not
    if(isset($_SESSION['username']))      /*** user is loged in ***/
    {
        //user get submit button
        if($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            if(empty($_POST['image_short_name']) || empty($_POST['image_des']))
            {
                $upload_error .= "Missing field! Please fill in  all field!";
            }
            else
            {
                $user = $_SESSION['username'];    //get name of who is loged in
                $id = $_SESSION['user_id'];
                $image_short_name_p =  $_POST['image_short_name'];
                $image_keyword_p    =  $_POST['image_keyword'];
                $image_des_p        =  $_POST['image_des'];

                //check for existing names
                $query = mysql_query("Select * FROM image WHERE image_short_name = '$image_short_name_p' AND user_id='$id'");       
                if(mysql_num_rows($query) >= 1)
                {
                    $upload_error .= "That image name is already taken";
                }
                else
                {
                    if($file)
                    {   
                        //scussess - store image in $image
                        $image = chunk_split(base64_encode(file_get_contents($_FILES['fileupload']['tmp_name'])));
                        //store image name in $image_full_name(.jpeg)
                        $image_full_name = $_FILES['fileupload']['name'];
                        //store image size in $image_size, change it to  100x100
                        $image_width_height = getimagesize($_FILES['fileupload']['tmp_name']);
                        $image_resolution = $image_width_height[0] . "X" . $image_width_height[1]; 
                        //get size of image in binary, change $file_size to b, kb, mb, or gd 
                        $file_size = filesize($file); 
                        if ($file_size < 1024) { $file_size = $file_size . " B"; }
                        else if($file_size < 1048576)  { $file_size = round($file_size / 1024, 2) . " KB"; }
                        else if($file_size < 1073741824) { $file_size = round($file_size / 1048576, 2) . ' MB'; } 
                        else if ($file_size < 1099511627776) { $file_size = round($file_size / 1073741824, 2) . ' GB'; }




                        //test to see if it is an image
                        if($image_width_height == FALSE)
                        {
                            $upload_error .= "Thats not a image";
                        }   
                        else
                        { 
                            //Image is too big, change it to KB's
                            if(strstr($file_size, 'M') || strstr($file_size, 'G'))
                            {
                                $upload_error .= "image is too big";
                            }
                                //get user id and sote in user_id_db(user thats loged in select his rows)
                                $queryget = mysql_query("SELECT user_id FROM user WHERE username = '$user'");
                                $row = mysql_fetch_assoc($queryget);    
                                $user_id_db = $row['user_id'];

                                //insert information into database
                                if(!$sql=mysql_query("INSERT INTO image VALUES(NULL, '$user_id_db', '$image', '$image_keyword_p' ,
                                    '$image_full_name', '$image_short_name_p', '$image_des_p','$file_size' ,'$image_resolution', 0)"))
                                {
                                    $upload_error .= "problem uploading image. please try again";
                                }
                                else
                                {   
                                }
                        }
                    }
                    else
                    {
                        $upload_error .= "Select an image!";    
                    }
                }
            }        
        }
    }
    else




    {
        header('Location: error.php');
    }
    $_SESSION['upload_error'] = $upload_error;
    ?>

MySQL server has gone away

Means you somehow lost connection to the server.

by server do mean it lost connection to the database?
bc i dont understant how i am losing connection. on line 71 :

if(!$sql=mysql_query("INSERT INTO image VALUES(NULL, '$user_id_db', '$image', '$image_keyword_p' , '$image_full_name', '$image_short_name_p', '$image_des_p','$file_size' ,'$image_resolution', 0)"))

i am just inserting in database. it only give me problem when i am uploading a image which is MB size.

Perhaps it loses the connection because the upload takes too long. Try connecting to the database, AFTER the file is done.

i tried connecting to the database right before

if(!$sql=mysql_query("INSERT INTO image VALUES(NULL, '$user_id_db', '$image', '$image_keyword_p' , '$image_full_name', '$image_short_name_p', '$image_des_p','$file_size' ,'$image_resolution', 0)"))

but no luck. do you what else could be the case of this.

Firewall, router... can be anything really. Most likely is a short wait timeout on the server. You can try to connect, query and close right after each other and see if that works.

any idea how to debug this

Does the code work if you omit the upload part ? If not, delete more until it works. Then start putting stuff back in again.

my upload.php page seem to work if i upload a small size image. but when i upload a lets a MB size. it give me those 3 erros. which i dont understant bc i using long blob to store the images in database.

It appears to be the upload. Try to connect, query and close each time as a single block, so the connection does not remain open (as stated before).

i tried closing the query and it gave me warring
Warning: mysql_close(): supplied resource is not a valid MySQL-Link resource in C:\xampp\htdocs\a_upload\upload.php on line 48

i add this link to my code after i was done with the query
mysql_close($query);

Show your changed code.

<?php
include("include/header.php");

$file = isset($_FILES['fileupload']) ? $_FILES['fileupload']['tmp_name'] : false;
$upload_error = "";

//check, if user is loged in or not
if(isset($_SESSION['username']))      /*** user is loged in ***/
{
    //user get submit button
    if($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        if(empty($_POST['image_short_name']) || empty($_POST['image_des']))
        {
            $upload_error .= "Missing field! Please fill in  all field!";
        }
        else
        {
            $user = $_SESSION['username'];    //get name of who is loged in
            $id = $_SESSION['user_id'];
            $image_short_name_p =  $_POST['image_short_name'];
            $image_des_p       =  $_POST['image_des'];

            //check for existing names
            $query = mysql_query("Select * FROM image WHERE image_short_name = '$image_short_name_p' AND user_id='$id'");       
            if(mysql_num_rows($query) >= 1)
            {
                $upload_error .= "That image name is already taken";
            }
            else
            {
                if($file)
                {   
                    //scussess - store image in $image
                    $image = chunk_split(base64_encode(file_get_contents($_FILES['fileupload']['tmp_name'])));
                    //store image name in $image_full_name(.jpeg)
                    $image_full_name = $_FILES['fileupload']['name'];
                    //store image size in $image_size, change it to  100x100
                    $image_width_height = getimagesize($_FILES['fileupload']['tmp_name']);
                    $image_resolution = $image_width_height[0] . "X" . $image_width_height[1]; 
                    //get size of image in binary, change $file_size to b, kb, mb, or gd 
                    $file_size = filesize($file); 
                    if ($file_size < 1024) { $file_size = $file_size . " B"; }
                    else if($file_size < 1048576)  { $file_size = round($file_size / 1024, 2) . " KB"; }
                    else if($file_size < 1073741824) { $file_size = round($file_size / 1048576, 2) . ' MB'; } 
                    else if ($file_size < 1099511627776) { $file_size = round($file_size / 1073741824, 2) . ' GB'; }

                    mysql_close($query);


                    //test to see if it is an image
                    if($image_width_height == FALSE)
                    {
                        $upload_error .= "Thats not a image";
                    }   
                    else
                    { 
                        //Image is too big, change it to KB's
                        if(strstr($file_size, 'M') || strstr($file_size, 'G'))
                        {
                            $upload_error .= "image is too big";
                        }
                            //get user id and sote in user_id_db(user thats loged in select his rows)
                            $queryget = mysql_query("SELECT user_id FROM user WHERE username = '$user'");
                            $row = mysql_fetch_assoc($queryget);    
                            $user_id_db = $row['user_id'];

                            //insert information into database
                            if(!$sql=mysql_query("INSERT INTO image VALUES(NULL, '$user_id_db', '$image', 
                                '$image_full_name', '$image_short_name_p', '$image_des_p','$file_size' ,'$image_resolution', 0)"))
                            {
                                $upload_error .= "problem uploading image. please try again";
                            }
                            else
                            {   
                            }
                    }
                }
                else
                {
                    $upload_error .= "Select an image!";    
                }
            }
        }        
    }
}
else
{
    header('Location: error.php');
}
$_SESSION['upload_error'] = $upload_error;
?>

Where do you connect? In header.php? You should move all together:

include 'header.php';
mysql_query();
// store the result you need in a variable
mysql_free_result();
mysql_close();

// do the upload part

include 'header.php';
// next block
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.