3

Right now im using Mysqli to retrieve data from a Mysql Database, The code im using is difficult to understand and from my understanding has depreciated functions or itself in entire is depreciated. If i could get some insight and maybe some updated techniques on my Qoal of retrieving data from a mysql DB.

mysqli prepared statements

What i have so far is this:

$param = 1;
$mysqli = new mysqli("127.0.0.1", "user", "password", "databaseName");
$mysqli->query('SELECT reply_id FROM replies WHERE reply_topic = ? ORDER BY reply_date ASC');
$mysqli->bind_param('i',$param)
$mysqli->execute();
$row = bind_result_array($mysqli);

while($mysqli->fetch()){
    $set[$row['']] = getCopy($row);
}
$mysqli->free_result();
return $set;




function bind_result_array($stmt)
{
    $meta = $stmt->result_metadata();
    $result = array();
    while ($field = $meta->fetch_field())
    {
        $result[$field->name] = NULL;
        $params[] = &$result[$field->name];
    }

    call_user_func_array(array($stmt, 'bind_result'), $params);
    return $result;
}

function getCopy($row)
{
    return array_map(create_function('$a', 'return $a;'), $row);
}
developerwjk
  • 8,619
  • 2
  • 17
  • 33
DirtyRedz
  • 566
  • 5
  • 14
  • Start with this [link](http://www.w3schools.com/php/php_mysql_connect.asp) to gain a clear understanding of mysqli. The only difference between mysql and mysqli is better use of $connection . we use mysqli_query($connection,$query) instead of mysql_query($query) and similary mysqli_fetch_array($connection,$result) . – Praveen Kavuri Sep 17 '14 at 21:38
  • Don't trust w3schools. The real question is why are you populating the variable `$set` in such a contrived way? What do you do with it later on? Reading from the db should be a lot simpler than this. – developerwjk Sep 17 '14 at 22:06
  • @developerwjk $set returns a multi dimensional array, one array for each row returned. I've seen this done in many tutorials and really have no idea how else to get what I want – DirtyRedz Sep 17 '14 at 22:08
  • Well, I posted an answer but I found it to be wrong on a few points so I deleted it. I'd suggest switching to PDO. Figuring out how to do this with mysqli does seem overcomplicated. – developerwjk Sep 17 '14 at 22:29
  • Your query result is a table-type data structure which is sort of similar to a 2D array. What you might be looking for is a "pivot table" - a table which has both row headings and column headings. – kermit Sep 17 '14 at 23:08
  • That sounds promising, unfortunately I've never used pivot tables and references to get me started – DirtyRedz Sep 17 '14 at 23:10
  • @DirtyRedz it's a little tricky to generate a pivot table. Here's some information on it: http://stackoverflow.com/questions/7674786/mysql-pivot-table ; FYI, MS Excel is fantastic at generating pivot tables from normal tables. If you dump the query results to an Excel sheet, it's just a few clicks to generate the pivot table. You drag the row header you want to the header section, drag column header you want to the column section, and it does the magic. Very powerful. – kermit Sep 17 '14 at 23:12
  • @kermit thanks for your assistance I will look into it and see if im aple to incorperate it into my code. – DirtyRedz Sep 17 '14 at 23:17

1 Answers1

2

You need to clean up the call on the database first. $mysqli->query has send the query before you bound parameters and so on.

Replace with the following;

$stmt = $mysqli->prepare('SELECT reply_id FROM replies WHERE reply_topic = ? ORDER BY reply_date ASC');
$stmt->bind_param('i',$param)
$stmt->execute();

The $mysqli->prepare returns a new object that you should bind values to and then execute. It avoids SQL injection!

None of this is depreciated. mysqli_ is exactly what you should use.

As all you want is an array of the results, you can use the following code to do so - no user functions required.

$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
        $set[] = $row['reply_id'];
}

For each row in the while, each field is saved into an array with the column name as the key. So $row['reply_id'] contains the value of the reply_id column in the database. Above I have saved this value to an array called $set, and when it loops over the next result row it saves the next value to that array too.

You can manipulate the result before or after saving it to the new array, as you see fit.

Hope this helps.

Community
  • 1
  • 1
worldofjr
  • 3,868
  • 8
  • 37
  • 49
  • I was told that create_function is depreciated and should not be used. But the getrow() method is the only approach ive seen in tutorials. – DirtyRedz Sep 17 '14 at 23:12
  • Ah, I missed that. You can just use `function($a) { return $a; }` as that will work in PHP5.2+ – worldofjr Sep 17 '14 at 23:21
  • Awsome thxs ill incorperate this into my code. Appreciate it. – DirtyRedz Sep 17 '14 at 23:35
  • Could you explain what the While loop is exactly doing, im fuzzing on hows its accomplishing its goal. – DirtyRedz Sep 17 '14 at 23:40
  • Simply, for each row of the result do this with the bound variables (or array in your case). – worldofjr Sep 17 '14 at 23:46
  • Im sry but im just not grasping this particular part, i mean bind_result_array() seems to return an array of nulls with field names as the keys. So how does fetch() in the while loop fill in the data. – DirtyRedz Sep 18 '14 at 00:07
  • I'm not altogether sure what the function is trying to achieve, but I assume it works with a desired result. Perhaps ask another question asking exactly that! – worldofjr Sep 18 '14 at 00:11
  • Actually ... what are *you* trying to achieve? Perhaps we can help with a better solution than what you have above. – worldofjr Sep 18 '14 at 00:16
  • Well im right, i would like to just get an array back for each row requested from the DB. It does do that now, however i dont know how so if i want to manipulate the data in any way before returning i don't know how. For example what if the DB returns a single item? Or i guess my biggest problem what if i retrieve data back but its unexpected data. How can i debug if their are no errors returned from the mysqli_ object. for example if i request 3 rows from a 5 row DB, but only get 2 or 1 back, and the statement works and is correct. How do i debug if i dont understand the code correctly. – DirtyRedz Sep 18 '14 at 00:26
  • I've updated my answer. You can create an array with the result data in a much simpler way, and you can manipulate it how you see fit. As far as debugging is concerned, that's something you should work out once you understand how the data is retrieved from the database. – worldofjr Sep 18 '14 at 00:57