0

I am trying to use this code in order to echo some information, which is required to have a bind_param, in order to generate the data. What changes do I need to make to this code in order to get it to work?

function get_header_link($sql, $image)
{
    include 'connect.php';

    $id = $_GET['id'];
    $select = $conn->prepare($sql);
    $select->bind_param('s', $id);
    if ($result = $select->execute()) {
        foreach($select as $value => $row) {
            echo "<h4>" . $row['display'] . "</h4>\n
                    <div class='placeholder' id='large'>\n
                        <img src='" . $row[$image] . "'/>\n
                        <div class='name'>
                            <h1>" . $row['display'] . "</h1>\n
                        </div>
                  </div>";
        }
    }
}

On running the code above, using:

get_header_link("SELECT * FROM homelinks WHERE linkID=?", "large_image");

I no longer get an error, and none of the data from the database is printing.

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
Chri
  • 66
  • 1
  • 12
  • Currently you execute your query twice. Once in the prepare form once without. Drop the line with `$conn->query()`. – Sirko Jul 22 '15 at 15:45
  • Did that change but got an error stating that "Notice: Trying to get property of non-object in C:\xampp\htdocs\WebDevelopment\AfterGlowWebsite\functions.php on line 252->if ($result->num_rows > 0) {" – Chri Jul 22 '15 at 15:47
  • after just deleting the line, $result will not be defined. Then the proper way is `$result = $select->execute();` instead of simple `$select->execute();` – Random Jul 22 '15 at 15:51
  • @sirko - line has been dropped however nothing gets displayed now. – Chri Jul 22 '15 at 15:51
  • @random: did that but I get this error message: **Notice: Trying to get property of non-object in C:\xampp\htdocs\WebDevelopment\AfterGlowWebsite\functions.php on line 251->if ($result->num_rows > 0) {** – Chri Jul 22 '15 at 15:55
  • So the if must be : `if($result = $select->execute();) {` – Random Jul 22 '15 at 16:06
  • still doesn't work :( - and I'm getting an error message stating that the ; is extra. – Chri Jul 22 '15 at 16:09
  • remove the `;`... my fault... there is no `;` in a if... – Random Jul 22 '15 at 16:14
  • @Random : even if I remove the ; the code still give me an error, **Notice: Trying to get property of non-object in C:\xampp\htdocs\WebDevelopment\AfterGlowWebsite\functions.php on line 251 -> referring to if ($result->num_rows > 0) { ** – Chri Jul 22 '15 at 16:18
  • That's a weird behavior. It should work with mysqli... what is `connect.php` (and so, what is `$conn` ?) Isn't it some custom code.... ? – Random Jul 22 '15 at 16:26
  • @Random: Posted the code for connect.php in the question – Chri Jul 22 '15 at 16:29

2 Answers2

0

After reading this, you should use mysqli like this:

function get_header_link($sql, $image)
{
    include 'connect.php';

    $id = $_GET['id'];
    $select = $conn->prepare($sql);
    $select->bind_param('s', $id);
    $select->execute();
    $select->store_result();
    $meta = $select->result_metadata();
    while ($field = $meta->fetch_field()) {
        $params[] = & $row[$field->name];
    }

    // bind all columns in $row
    call_user_func_array(array(
        $select,
        'bind_result'
    ) , $params);

    while ($select->fetch()) {
        echo "<h4>" . $row['display'] . "</h4>\n
                <div class='placeholder' id='large'>\n
                    <img src='" . $row[$image] . "'/>\n
                    <div class='name'>
                        <h1>" . $row['display'] . "</h1>\n
                    </div>
                </div>";
    }

    /* close statement */
    $select->close();
}
Community
  • 1
  • 1
Random
  • 3,158
  • 1
  • 15
  • 25
  • No longer getting errors but I'm getting a blank div – Chri Jul 22 '15 at 17:24
  • what do you mean by that? as I am not the best with regards to terminology – Chri Jul 22 '15 at 18:06
  • Thanks alot for the help, the answer you posted was super helpful. However it needed a minor change in order to work. – Chri Jul 22 '15 at 22:10
  • I edited answer to contain the correct answer, nice it works – Random Jul 23 '15 at 12:02
  • Edited the answer to check for empty rows, in case it is needed. – Chri Jul 27 '15 at 11:16
  • @Chri Is there really an issue if the result is empty ? Everything is in a while. I think fetch accepts empty result, so it would only not run the loop. Don't you agree ? I added store_result which may be useful indeed. – Random Jul 27 '15 at 12:23
  • In this situation no, however if there is data which is echoed before the while, checking for empty rows will prevent it from echoing if there are no rows. – Chri Jul 27 '15 at 23:33
0
function get_header_link($sql)
{
    include 'connect.php';

    $id = $_GET['id'];
    $select = $conn->prepare($sql);
    $select->bind_param('s', $id);
    $select->execute();
    $meta = $select->result_metadata();
    while ($field = $meta->fetch_field()) {
        $params[] = & $row[$field->name];
    }

    call_user_func_array(array(
        $select,
        'bind_result'
    ) , $params);
    while ($select->fetch()) {
        echo "<h4>" . $row['display'] . "</h4>\n
            <div class='placeholder' id='large'>\n
                <img src='" . $row['large_image'] . "'/>\n
                <div class='name'>
                    <h1>" . $row['display'] . "</h1>\n
                </div>
          </div>";
    }

    $select->close();
}
Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
Chri
  • 66
  • 1
  • 12
  • It gave me the results I was going for :) I think the $row is indirectly affected by fetch somehow. – Chri Jul 23 '15 at 11:15
  • Indeed, it comes from `$params[] = &$row[$field->name]`, and then the call to `bind_result` which put the result in row variable... – Random Jul 23 '15 at 12:00