What is the equivalent of mysql_result in mysqli?

Asked

Viewed 3,581 times

0

How can I make a FOR in php using mysqli?

'Cause I’ve always been able to do that:

for($i=0; $i < $qtde; $i++){
   $pet_id = **mysql_result**($dadosPets, $i, 'animal_id');
}

How do I do the same, however, with mysqli?

  • You can search PHP websites about the mysqli documentation, as far as you know, mysqli (my SQL improved) is an enhanced version of SQL, so your methods and functions will be the same, correct? Try to do this with normal mysqli that should work.

  • Sorry to inform you @Cypherpotato, but this does not proceed as I have tried and it is a mistake. "Warning: mysql_result() expects Parameter 1 to be Resource, Object Given in /home/petsm976/public_html/php/default.php on line 164 "

  • 1

    @Gustavosevero mysql_* or mysqli_? This Warning shows mysql_, And mysqli_result does not exist in the mysqli_* functions, see which one it was replaced with at this link

  • @Joker, I’m using mysql_, but I want to switch to mysqli_

  • @Joker, I’ve looked at this link you indicated, but there is no example with FOR, all are with WHILE.

  • 1

    Gustavosevero the link that the @joker indicated is yes correct, see that has an excerpt written mysqli_data_seek() in conjunction with mysqli_field_seek() and mysqli_fetch_field(), this is the correct example, the function data_seek. I created an answer.

Show 1 more comment

3 answers

4


The documentation link itself can help you http://php.net/manual/en/ (recommend in English, because sometimes the Portuguese version contains errors :/ )

Will have to use:

http://php.net/manual/en/mysqli-result.data-seek.php

A complete example:

<?php
//Conecta
$mysqli = new mysqli("localhost", "usuario", "senha", "banco");

if (mysqli_connect_errno()) {
    printf("Erro de conexão: %s\n", mysqli_connect_error());
    exit;
}

$query = 'SELECT ...'; //Sua select

if ($result = $mysqli->query($query)) {
    for($i = 0; $i < $qtde; $i++) {
        if ($result->data_seek($i)) {
            $row = $result->fetch_assoc();

            //ID do pet
            $pet_id = $row['pet_id'];

        }
    }
    $result->close();
}

$mysqli->close();

Procedural style:

<?php
//Conecta
$link = mysqli_connect("localhost", "usuario", "senha", "banco");

if (!$link) {
    printf("Erro de conexão: %s\n", mysqli_connect_error());
    exit;
}

$query = 'SELECT ...'; //Sua select

if ($result = mysqli_query($link, $query)) {
    for($i = 0; $i < $qtde; $i++) {
        if (mysqli_data_seek($result, $i)) {
            $row = mysqli_fetch_assoc($result);

            //ID do pet
            $pet_id = $row['pet_id'];
        }
    }

    mysqli_free_result($result);
}

mysqli_close($link);
  • I’ll try it @Guilhermenascimento, thank you.

  • I went to the link you passed, http://php.net/manualen/mysqli-result.data-seek.php, and tried to use the same code, but procedural... I just don’t understand how I put the for... Can you help me?

  • Thanks @Guilhermenascimento, I put this procedural code and appeared the following message: "Warning: mysqli_data_seek(): Couldn’t fetch mysqli_result in /home/petsm976/public_html/php/default.php on line 167"

  • @Gustavosevero updated the answer, see that I changed two lines of code, test again please.

  • Thanks @Guilhermenscimento, no error appears anymore. I just try to echo the variable and it does not appear hehehe

  • Yes, @Guilhermenascimento, put $pet_id = $Row['pet_id'];, but var_dump($data)? What $data? hehehehe

  • I gave a var_dump($result), but the result was: Object(mysqli_result)#4 (5) { ["current_field"]=> int(0) ["field_count"]=> int(14) ["lengths"]=> array(14) { [0]=> int(1) [1]=> int(1) [2]=> int(4) [3]=> int(1) [4]=> int(1) [5]=> int(10) [6]=> int(1) [7]=> int(0) [8]=> int(1) [9]=> int(1) [10]=> int(1) [11]=> int(2) [12]=> int(12) [13]=> int(9) } ["num_rows"]]]]=> int(4) ["type"]=> int(0) } Object(mysqli_result)#4 (5) { ["current_field"]=> int(0) ["field_count"]=> int(14) ["lengths"]=> array(14) { [0]=> int(1) [1]=> int(1)...

  • I gave a var_dump($Row) and it appeared this: array(14) { [0]=> string(1) "9" [1]=> string(1) "5" [2]=> string(4) "Tuti" [3]=> string(1) "2" [4]=> string(1) "1" [5]=> string(10) "2013-06-11" [6]=> string(1) "2" [7]=> string(0) "" [8]=> string(1) "2" [9]=> string(1) "1" [10]=> string(1) "1" [11]=> string(2) "RS" [12]=> string(12) "Porto Alegre" [13]=> string(9) "cross 50%" } array(14) { [0]=> string(1) "2" [1]=> string(1) "1" [2]=> ... .

  • @Gustavosevero I edited the answer code, the correct is mysqli_fetch_assoc and not mysqli_fetch_row, it was my mistake.

  • Thanks @Guilhermenascimento, now the data appears.

Show 5 more comments

1

I prefer this example

Object-Oriented rating

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
 
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";

if ($result = $mysqli->query($query)) {

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
    }

    /* free result set */
    $result->close();
}

/* close connection */
$mysqli->close();
?>

procedural notation

<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";

if ($result = mysqli_query($link, $query)) {

    /* fetch associative array */
    while ($row = mysqli_fetch_assoc($result)) {
        printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
    }

    /* free result set */
    mysqli_free_result($result);
}

/* close connection */
mysqli_close($link);
?>
  • 1

    Hello, Danley. Why do you prefer this medium? Although the example quoted in my reply show mysqli_data_seek, does not mean that it is useful to use it compared to "LIMIT", the mysqli_data_seek can be used with ranges, for example: 1, 3, 5, 7 or even random ranges, 2, 7, 15, 17, 18, 24, its control is to "navigate" based on the Indian in any way the person wishes. Anyway I agree that for simple, LIMIT is what solves ;)

  • Opa Guilherme, I agree that your example allows more logic and, consequently, meet more requirements; I prefer the example of this answer, because it makes it easier for cases where you want to take all the data more easily. But mainly because it is more readable for those who are starting with PHP. Only one more way available to help beginners.

-5

Mysqli is obsolete and should be used in its place the PDO class, with this you can assemble your querys based on POO, making them much simpler and safer.

See more in the PHP documentation

PDO

  • 4

    Mysqli is not obsolete and why use PDO? What advantage do you expect to have in doing so? It is no longer simple, it is no longer safe, and even if you want to do OOP (which is not true, OOP is something else), you can use object notation in Mysqli as well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.