Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get Ruby MySQL returning PHP like DB SELECT result
    primarykey
    data
    text
    <p>So I use the PDO for a DB connection like this:</p> <pre><code>$this-&gt;dsn[$key] = array('mysql:host=' . $creds['SRVR'] . ';dbname=' . $db, $creds['USER'], $creds['PWD']); $this-&gt;db[$key] = new PDO($this-&gt;dsn[$key]); </code></pre> <p>Using PDO I can then execute a MySQL SELECT using something like this:</p> <pre><code>$sql = "SELECT * FROM table WHERE id = ?"; $st = $db-&gt;prepare($sql); $st-&gt;execute($id); $result = $st-&gt;fetchAll(); </code></pre> <p>The $result variable will then return an array of arrays where each row is given a incremental key - the first row having the array key 0. And then that data will have an array the DB data like this:</p> <pre><code>$result (array(2) [0]=&gt;[0=&gt;1, "id"=&gt;1, 1=&gt;"stuff", "field1"=&gt;"stuff", 2=&gt;"more stuff", "field2"=&gt;"more stuff" ...], [1]=&gt;[0=&gt;2, "id"=&gt;2, 1=&gt;"yet more stuff", "field1"=&gt;"yet more stuff", 2=&gt;"even more stuff", "field2"=&gt;"even more stuff"]); </code></pre> <p>In this example the DB table's field names would be id, field1 and field2. And the result allows you to spin through the array of data rows and then access the data using either a index (0, 1, 2) or the field name ("id", "field1", "field2"). Most of the time I prefer to access the data via the field names but access via both means is useful.</p> <p>So I'm learning the ruby-mysql gem right now and I can retrieve the data from the DB. However, I cannot get the field names. I could probably extract it from the SQL statement given but that requires a fair bit of coding for error trapping and only works so long as I'm not using SELECT * FROM ... as my SELECT statement.</p> <p>So I'm using a table full of State names and their abbreviations for my testing. When I use "SELECT State, Abbr FROM states" with the following code</p> <pre><code>st = @db.prepare(sql) if empty(where) st.execute() else st.execute(where) end rows = [] while row = st.fetch do rows &lt;&lt; row end st.close return rows </code></pre> <p>I get a result like this:</p> <pre><code>[["Alabama", "AL"], ["Alaska", "AK"], ...] </code></pre> <p>And I'm wanting a result like this:</p> <pre><code>[[0=&gt;"Alabama", "State"=&gt;"Alabama", 1=&gt;"AL", "Abbr"=&gt;"AL"], ...] </code></pre> <p>I'm guessing I don't have the way inspect would display it quite right but I'm hoping you get the idea by now.</p> <p>Anyway to do this? I've seen some reference to doing this type of thing but it appears to require the DBI module. I guess that isn't the end of the world but is that the only way? Or can I do it with ruby-mysql alone?</p> <p>I've been digging into all the methods I can find without success. Hopefully you guys can help.</p> <p>Thanks Gabe</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload