Welcome Guest, Not a member yet? Register   Sign In
How do I access mySQL database records with spaces in them?
#1

[eluser]\/ertigo[/eluser]
I am new to PHP and CodeIgniter: I am following along with the NETTUTS video tutorials, but am stumped as how to access database records with spaces in them. The error I am getting is "PHP Parse error: syntax error, unexpected T_VARIABLE".

I am looking to access the "First Name" and "Last Name" records.

Here is the code from the controller:
Code:
<? class Personnel extends Controller {

    function __constructer() {
        parent::Controller();
    }

    function index() {
        $this->load->model('personnel_model');

        $data['title']="Personnel Records";
        $this->load->view('header',$data);

        $this->load->view('toc');

        $data['records']=$this->personnel_model->GetList();
        $this->load->view('personnel',$data);

        $data['url']=current_url();
        $this->load->view('footer',$data);
    }
}

Here is the model code:
Code:
<? class Personnel_model extends Model {
    function getList() {
        $q = $this->db->query("SELECT Department,`First Name`,`Last Name`,Extension from Users" ) ;
        if($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
    }
}

Here is the view:
Code:
<table><tr><th>Name</th><th>Department</th><th>Extension</th></tr>
  &lt;? foreach($records as $row) :?&gt;
      <tr><td>&lt;!--?=$row[Last Name] ?&gt;, &lt;!--?=$row->"First Name"--&gt;</td>
      <td>&lt;?=$row->Department?&gt;</td><td>&lt;?=$row->Extension?&gt;</td>
  &lt;? endforeach; ?&gt;
</table>

&lt;? print_r ($records); ?&gt;<br><br>
&lt;? var_dump ($records); ?&gt;

And finally, here is the output I am getting from print_r() and var_dump():
Code:
Array ( [0] => stdClass Object ( [Department] => 1 [First Name] => Me [Last Name] => Test [Extension] => 283 ) )

array(1) { [0]=> object(stdClass)#14 (4) { ["Department"]=> string(1) "1" ["First Name"]=> string(3) "Me" ["Last Name"]=> string(4) "Test" ["Extension"]=> string(3) "283" } }
#2

[eluser]Developer13[/eluser]
Sorry - I'm actually not going to answer the question asked... instead, I'm going to recommend that you use underscores in place of spaces in your column names. Makes it easier for everybody in the end.
#3

[eluser]\/ertigo[/eluser]
Yeah, I figured that I would probably have to do something like that. I was hoping I could avoid having to write an import routine that did that, but it works now.




Theme © iAndrew 2016 - Forum software by © MyBB