Error in SQL syntax

Discussion in 'PHP' started by Birthe_nielsen, Dec 9, 2010.

  1. #1
    I am trying to retrieve data from my database on localhost to make a mailinglist :)

    My code looks like this:

    <?php

    echo '<h1>Mailing list</h1>';

    require_once ('connect_to_mysql.php'); //Connect to the db.

    // Make the query:
    $q = "SELECT CONCAT (id, name, email, country, created, FROM customers WHERE material='1')";

    $r = @mysqli_query ($myConnection, $q); // Run the query.

    if ($r) { // If it ran OK, display the records.

    // Table header.
    echo '<table align="center"cellspacing="3" cellpadding="3"width="75%"><tr><td align="left"><b>Name</b></td><td align="left"><b>Date Registered</b></td></tr>';

    // Fetch and print all the records

    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

    echo '<tr><td align="left">' .$row['name'] . '</td><td align="left">'. $row['created'] . '</td></tr>';
    }

    echo '</table>'; // Close the table.

    mysqli_free_result ($r); // Free up the resources.

    }

    else { // If it did not run OK.

    // Error message:
    echo '<p class="error">The mailing list could not be retrieved/updated.</p>';

    // Debugging message:
    echo '<p>' . mysqli_error($myConnection) . '<br/><br />Query: ' . $q . '</p>';

    } // End of if ($r) IF.

    mysqli_close($myConnection); // Close the database connection.


    ?>

    My connect_to_mysql looks like this:

    <?php

    $db_host = "localhost";
    $db_username = "root";
    $db_pass = "root";
    $db_name = "subscribe";

    $myConnection = mysqli_connect($db_host,$db_username,$db_pass,$db_name) or die ("could not connect to mysql");

    mysqli_set_charset($myConnection, 'utf8');


    ?>

    But i get the following errors:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM customers WHERE material='1')' at line 1

    Query: SELECT CONCAT (id, name, email, country, created, FROM customers WHERE material='1')

    Does anyone know how to solve this problem?
     
    Birthe_nielsen, Dec 9, 2010 IP
  2. ZoomRumble

    ZoomRumble Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try this
    $q = "SELECT id, name, email, country, created FROM customers WHERE material='1'";
    PHP:
     
    ZoomRumble, Dec 9, 2010 IP
  3. Birthe_nielsen

    Birthe_nielsen Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you so much, it finally works! :)
     
    Birthe_nielsen, Dec 9, 2010 IP