-2

I have custom table name "backend-member". then i have code how to insert the data into that table using $wpdb.

global $wpdb;

if ( isset( $_POST['save'] ) ){
        $wpdb->insert( 'backend-member', array(
            'navn' => $_POST['field1'], 
            'mobil' => $_POST['field2'],
            'email' => $_POST['field4'],
            'facebook' => $_POST['field3'],
            'date_created' => date("M d,Y H:i:s")
           ),
            array( '%s', '%s', '%s', '%s', '%s') 
        );
    header('location:/');
  }

and its work to insert the data. and i am trying to query all data using this code, but it doesnt work, no result, even error no appeared, somebody help me how to make this work? thank you. this is my code.

global $wpdb;

$result = $wpdb->get_results ( "
    SELECT * 
    FROM  backend-member" );

foreach ( $result as $page )
{
   echo $page->id.'<br/>';
   echo $page->navn.'<br/>';
}

Thanks in advance

  • What do you mean by "it doesn't work" - Are you getting an error? Is the SQL failing? Are there no results being returned? We can't help is we don't know what problem we're trying to help with! – FluffyKitten Sep 04 '17 at 03:49
  • sorry for that, I mean is, the insert data is working, but when i query the data its doesnt work, means, no result or error appeared. – Jovel Mark Diaz Sep 04 '17 at 03:56
  • No errors, well this `$backend-member` should be throwing you one. Edit: You removed the `$` from it and that still should be throwing an error where my comment was based on the original post https://stackoverflow.com/revisions/46030011/1 – Funk Forty Niner Sep 04 '17 at 04:01
  • backend-member is the name of my table sir, what is the correct query for this sir? – Jovel Mark Diaz Sep 04 '17 at 04:03
  • i already do that sir, – Jovel Mark Diaz Sep 04 '17 at 04:04
  • in eirther case, that would have interpreted in the same logical error, but the clarity of the now said table name is also interpreting that as doing math. – Funk Forty Niner Sep 04 '17 at 04:05
  • @Fred-ii- i update my codes above sir, can you help me about this issue sir? please – Jovel Mark Diaz Sep 04 '17 at 04:06
  • @Fred-ii- i use post snippets plugin to insert my php code – Jovel Mark Diaz Sep 04 '17 at 04:07
  • @Fred-ii- did you downvote my answer? If so, can you let me know whats wrong with it please for future reference? – FluffyKitten Sep 04 '17 at 04:12
  • @FluffyKitten your code it doesnt work sir, still no output, even error.. :( – Jovel Mark Diaz Sep 04 '17 at 04:15
  • @FluffyKitten why are you asking me that? Coming from "outside the box" here and being unbiased, I'd say that your answer is wrong for a reasons. One being you didn't check the OP's edit and second, you're also wanting to do math here, to which [I told the OP earlier](https://stackoverflow.com/questions/46030011/query-all-data-from-database-on-wordpress/46030134#comment79020168_46030011) also. What is "word minus word" equal to? ;-) – Funk Forty Niner Sep 04 '17 at 04:16
  • @Fred-ii- i dont understand, it my first time to do this in wordpress :( – Jovel Mark Diaz Sep 04 '17 at 04:21
  • @Fred-ii- I asked because I wanted to know what the problem was! One - the OP made the edit at the same time I posted... not much I can do about that! And yes, it was only after I checked the "duplicate" link that I figure out what "*the clarity of the now said table name is also interpreting that as doing math*" meant... even as an English speaker, that sentence made no sense to me. And the OP doesn't appear to be an English speaker.... for the first time in my 6 years here I think I'm actually going to respond to a closed question to explain :) – FluffyKitten Sep 04 '17 at 04:26
  • 1
    @JovelMarkDiaz what @Fred-ii- was hinting at is that you need to surround the table name in backticks i.e. ` backend-member ` (with no spaces - thats the only way they would appear!) because otherwise it thinks the - is a minus sign and you are trying to do math. – FluffyKitten Sep 04 '17 at 04:30
  • @FluffyKitten i see, thank you . i understand now, i will update that one. – Jovel Mark Diaz Sep 04 '17 at 04:31
  • @FluffyKitten wow, thank you, it works now, after i rename my table to "backend_member" – Jovel Mark Diaz Sep 04 '17 at 04:34
  • @Fred-ii- thank you also :) – Jovel Mark Diaz Sep 04 '17 at 04:35

1 Answers1

1

You have an error in your SQL select statement - the table name should be surrounded in backticks (i.e. ``) . Otherwise it thinks the - is a minus sign for maths.

Try this:

global $wpdb;

$sql = "SELECT *  FROM  `backend-member`" 
$result = $wpdb->get_results ( $sql );

// just in case there is still an error, show it
if (is_null($results) &&  !empty($wpdb->last_error)) {

    $wpdb->show_errors();
    $wpdb->get_results ($wpdb->prepare($sql));
    $wpdb->print_error();

} else {

    foreach ( $result as $page ) {
       echo $page->id.'<br/>';
       echo $page->navn.'<br/>';
    }
}
FluffyKitten
  • 13,824
  • 10
  • 39
  • 52