0

I am trying to delete a file or copy a row into a new table, depending on a $_GET. The $_GET works fine, and I'm not including all the code, I know it isn't relevant. The table copy works, but the select statement that gets called when the $_GET is a different value returns nothing, except when I copy the query directly into phpmyadmin.

Base code:

$pID = $_GET['pID'];

$con = mysqli_connect("...","...","...","...");

The following works:

$query = 'INSERT INTO `photos` (`id`, `photo1`, `photo2`, `demographic_id`)
SELECT `id`, `photo1`, `photo2`, `demographic_id`
FROM `photos_queue`
WHERE `photos_queue`.`demographic_id` = '.$pID;

mysqli_query($con, $query);

This does not:

$query = 'SELECT `photo1` FROM `photos_queue` WHERE `demographic_id` = '.$pID;
$result = mysqli_query($con, $query);
print($result);
unlink($result);

I've printed $query and the value of it is valid; I can copy it directly into phpmyadmin and it will work fine.

4
  • 4
    have you echoed out mysqli_error() to see if it is erroring out? Commented Oct 5, 2013 at 0:47
  • also, what is the value of $pID? Commented Oct 5, 2013 at 0:48
  • 2
    You seem to have forgotten to call mysqli_fetch_XXX() to get the row from the query. Commented Oct 5, 2013 at 0:50
  • user2722375: A good idiom that is seen a lot when creating these connections is $con = mysqli_connect("...","...","...","...") or die(mysqli_error());. Obviously, the call to die() kills the script, so I usually use it for debugging... but I think it'd help you here. Commented Oct 5, 2013 at 0:50

2 Answers 2

1

mysqli_query() doesn't return the table data, it just returns a resource that can be used to fetch it. You need to do:

$result = mysqli_query($con, $query) or die (mysqli_error($con));
$row = mysqli_fetch_assoc($result);
$filename = $row['photo1'];
print($filename);
unlink($filename);
Sign up to request clarification or add additional context in comments.

8 Comments

Aha, that's it! I looked at another file that was giving me data just fine, but didn't notice I had skipped the mysqli_fetch_assoc/mysqli_fetch_row part. Thank you!
Unfortunately it still doesn't delete the file. The value of photo1 is "foldername/filename.jpg". I tried prepending "mysite.com" so the value of $filename would be "mysite.com/foldername/filename.jpg" but it still doesn't delete.
That's a totally unrelated problem. I don't know why you would prepend the domain name, filenames are not URLs. If the file is outside the document root, you need to prepend that path. Find the script that creates the file, and see where it puts them.
"foldername/filename.jpg" is the value I use as the src of an <img> and it works fine. It's inside of a folder called "foldername" which is in the root folder.
Maybe prepend $_SERVER['DOCUMENT_ROOT']? I came here to help you with SQL, I can't really guess the layout of your webserver.
|
0
($row = mysqli_fetch_array($result)

This should be placed after,

$result = mysqli_query($con, $query);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.