-3

How can I get the error to show from my MYSQL Statement below or is there a better way to write the below with PDO or similar:

Code:

$mysqlFiles = "SELECT campaign_image, optout_image FROM campaigns WHERE id = '$campaign_id'";

while($data = mysql_fetch_assoc($mysqlFiles));
{
    $imageNames[] = $data;
}
Jess McKenzie
  • 8,345
  • 27
  • 100
  • 170
  • 2
    http://php.net/manual/en/function.mysql-error.php ? am not quite grasping here. Are you trying to deliberately cause an error? – Funk Forty Niner Oct 26 '15 at 19:42
  • You'll need to execute your query before fetching data. See examples for [`mysql_query()`](http://php.net/manual/en/function.mysql-query.php). Also, yes, I highly recommend using MySQLi or PDO. – showdev Oct 26 '15 at 19:43
  • What exactly do you want. It is not very clear to me. Errors are probably written to the place defined by your php.ini – Rik Oct 26 '15 at 19:44
  • 1
    You did not even execute the query! – Sajib Acharya Oct 26 '15 at 20:28

3 Answers3

2

There's a good bit incorrect with your code.

  1. For starters, you're using mysql_fetch_assoc on a string, not a resource.
  2. Your SQL is vulnerable to SQL injection. You should be using prepared statements.
  3. The mysql_* functions are deprecated. You should be using the mysqli_* functions.

Once you've corrected those issues, you can view error messages use mysqli_error.

Community
  • 1
  • 1
Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
0

Use this (source: http://php.net/manual/en/function.mysql-error.php):

mysql_error ($mysqlconnection);
0

Firstly... in this line of code:

while($data = mysql_fetch_assoc($mysqlFiles));
                                             ^ end of statement, stops the process.
  • Notice the semi-colon at the end there?

Believe it or not, that is considered as valid syntax (and won't throw an error because of it), BUT... is also ending your statement and will not process { $imageNames[] = $data; }.

Whether the query is successful or not, it will still STOP right there.

Remove it

while($data = mysql_fetch_assoc($mysqlFiles))

Reference on semi-colon:

"As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block. The closing tag for the block will include the immediately trailing newline if one is present."

If you want to catch potential errors, use mysql_error() against your query

Something else I noticed is that you're not "querying" with mysql_query().

so your code will never result in a successful query along with what's already been said.

Example from the manual:

$query = sprintf("SELECT firstname, lastname, address, age FROM friends 
    WHERE firstname='%s' AND lastname='%s'",
    mysql_real_escape_string($firstname),
    mysql_real_escape_string($lastname));

// Perform Query
$result = mysql_query($query);

Example from http://php.net/manual/en/function.mysql-fetch-assoc.php

while ($row = mysql_fetch_assoc($result)) {
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}

If the following were coded as such and with the semi-colon, it would not process anything inside {...}:

while ($row = mysql_fetch_assoc($result)); {
                                    //   ^ semi-colon terminator similar to yours
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141