0

I had saved the required steps for a parameterized string match query that outputs the subsequent rows. I had lost my files when transferring to a faulty drive. So... I'm trying to mash things together and this isn't working.

$stmt = $link->prepare("SELECT id,entry,date from table WHERE string=? ORDER by Id DESC");
$stmt->bind_param('s',$string);
$stmt->execute();
$stmt->bind_result($id_db,$entry_db,$date_db);

if (($result = $link->query($stmt)) {

    while ($row = $result->fetch_row()){

    }

}

I can already tell that this is wrong, I don't use the parameterized results and I'm trying to use array indexes such as $row[0].

Going to get yelled at for this one I know.

The end result I want is for example:

string1 has rows: bob, mack, chris string2 has rows: alice, claire, lara

If $string=string1 then the output should be:

chris mack bob

I believe my problem is I am mixing statement types

jh314
  • 27,144
  • 16
  • 62
  • 82
janicehoplin
  • 397
  • 7
  • 15
  • 1
    Please, make a "DESCRIBE table" on your mysql database and update this post with results. – Emiliano Sangoi Jun 05 '15 at 21:17
  • hello Emiliano Sangoi, I have to figure out what that is, I haven't come across that before – janicehoplin Jun 05 '15 at 21:25
  • 1
    In mysql, DESCRIBE command shows table's structure – Emiliano Sangoi Jun 05 '15 at 21:29
  • I can't believe how I have lost the last 5 months of work. I did a lot of different things and I always referred to them and now even basic outputting of selected rows is a daunting task to me, this is sad. I'm frantically searching random websites trying to find prepared select statements outputting rows – janicehoplin Jun 05 '15 at 21:33
  • 1
    I think that you're using 'bind_result' function incorrectly. See this post http://stackoverflow.com/questions/18753262/example-of-how-to-use-bind-result-vs-get-result – Emiliano Sangoi Jun 05 '15 at 21:59
  • After that you use bind_result($var1, $var2), you have to call $stmt->fetch() and then you use $var1, $var2,... instead of $row[0], $row[1] – Emiliano Sangoi Jun 05 '15 at 22:02

1 Answers1

1

Assuming that "$link" is an instance of PHP's "mysqli" class, and that "id" and "Id" are two diffrent columns in your table (if it's not the case, please try replacing "Id" with "id" in the segment ".. ORDER BY Id.."), here is, based on your example, what I suggest that you try:

// Declare your "prepare" statement (make sure to change "Id" for "id" if both are used
// in reference to the same column in your table)
$stmt = $link->prepare('SELECT id, entry, date FROM table WHERE string = ? ORDER BY Id DESC');

// Bind the $string variable
$stmt->bind_param('s',$string);

// Execute the statement
$stmt->execute();

// Store the result (this is sometimes useful, depending on the data types in your table)
$stmt->store_result();

// Check whether at least one row in table matches the query, if not, stop here...
if ($stmt->num_rows === 0) exit('No matching rows'); // or do something else...

// Declare a container (i.e. storage) for each row (this is optional and depends on what
// you are trying to achieve)
$data = [];

// Loop through results (this is just an example; this could be achieved in a number
// of different ways)
for ($i = 0; $i < $stmt->num_rows; $i++)
{
    // Add a new array cell to $data, at index $i
    $data[$i] = [];

    // Bind result for row $i
    $stmt->bind_result($data[$i]['id'],$data[$i]['entry'],$data[$i]['date']);

    // Fetch $i^{th} row
    $stmt->fetch();
}

// Check if it worked (temporary)
var_dump($data);
focorner
  • 1,927
  • 1
  • 20
  • 24
  • Thank you for your response. The object-oriented example in the manual, worked for me but I will analyze your answer for potential future use. http://php.net/manual/en/mysqli-stmt.fetch.php – janicehoplin Jun 07 '15 at 05:01