Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Mysql_result in Mysqli

    Hello all,

    I got an assignment from school. I created this script in Mysql. Now as a second assignment my teacher wants it in Mysqli. I could convert all the functions to Mysqli except for the Mysql_result. I cannot find anything how to change it. I really need help, otherwise I fail school :x
    I tried it

    Like
    $code = $result3->field_seek($i);
    $row = $bestellingen->fetch_field();
    $result3->close();

    or
    $code = $result3->data_seek($i);
    $row = $bestellingen->fetch_row();


    tried it with mysqli_fetch_object, but nothing seems to work, all I get is an error.

    This is my whole code:

    view sourceprint?
    $mysqli = mysqli_connect("localhost" , "et" , "et" , "et");
    $query2 "SELECT * FROM bestellingen WHERE klantnummer=$klantnummer";
    $bestellingen = mysqli_query($mysqli,$query2) or die("$query2 has failed!");
    $num = mysqli_num_rows($bestellingen);
    for ($i=0;$i<$num;$i++)
    {
    $query = "SELECT * FROM bestelregels WHERE bestelnummer=".mysql_result($bestellingen, $i, "nummer");
    $result = mysqli_query($query) or die("Erro executing query: fetch bestelregels");
    $num2 = mysqli_num_rows($result);
    $price = 0;
    for ($j=0;$j<$num2;$j++)
    {
    $productnummer = mysql_result($result, $j, "productnummer");
    $aantal = mysql_result($result, $j, "aantal");
    $query = "SELECT prijs FROM producten WHERE nummer=$productnummer";
    $result2 = mysqli_query($query) or die("Erro executing query: fetch order price");
    $ppu = mysql_result($result2, 0, "prijs");
    $price += $ppu*$aantal;
    }
    $price = number_format((float)$price, 2, '.', '');
    echo "<tr>";
    echo " <td><a href='bestelling_detail.php?ordernummer=".($i+1)."'>".($i+1)."</a></td>";
    echo " <td>".mysql_result($bestellingen, $i, "geplaatst_op")."</td>";
    echo " <td>$price</td>";
    echo "</tr>";


    The Mysql_result have to be replaced. What I also did was replacing bestelnummer=".mysql_result with bestelnummer=$code
    and putting the

    $code = $result3->field_seek($i);
    $row = $bestellingen->fetch_field();
    $result3->close();

    somewhere else.

    I tried a lot, but nothing seems to work. I don't know it anymore, can anyone help?

  • #2
    Junsee
    Guest
    http://php.net/manual/en/class.mysqli-result.php

    but to be honest there are some mistakes, you need to check it over. you can't half do code and expect it to work so change all the mysql to mysqli, and then we can start

    $query2 "SELECT * FROM bestellingen WHERE klantnummer=$klantnummer";
    like so
    $query2 = "SELECT * FROM bestellingen WHERE klantnummer=$klantnummer";

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,036
    Thanks
    2
    Thanked 316 Times in 308 Posts
    There is no way in mysqli of directly accessing one specific field in a row, probably because mysql_result() was the slowest possible way of accessing values. You need to fetch the whole row and then access the field you want in that row.

    Your assignment "wants it in Mysqli", is more than just to convert mysql to mysqli statements, but to actually write the code the way you would do so using the methods available in the mysqli library.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,436
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    The entire *CONCEPT* of this part of the code is HORRIBLE HORRIBLE.
    Code:
    $query2 = "SELECT * FROM bestellingen WHERE klantnummer=$klantnummer";
    $bestellingen = mysqli_query($mysqli,$query2) or die("$query2 has failed!");
    $num = mysqli_num_rows($bestellingen);
    for ($i=0;$i<$num;$i++)
    {
        $query = "SELECT * FROM bestelregels WHERE bestelnummer=".mysql_result($bestellingen, $i, "nummer");
    UGH UGH UGH!

    Any time you find yourself making a query inside a loop on the results of another query, you should almost automatically assume you screwed up big time.

    LEARN TO USE JOINS!!!

    *ONE* query, NOT nested queries.

    Code:
    $sql = 
        "SELECT S.nummer, S.geplaatst_op, E.aantal * P.prijs AS price
         FROM bestellingen AS S
         INNER JOIN bestelregels AS E ON S.nummer = E.bestelnummer
         INNER JOIN producten AS P ON P.nummer=E.productnummer
         WHERE klantnummer=$klantnummer";
    Note also that you should NOT use SELECT *. You should instead carefully SELECT only the fields you will actually use in your PHP code.
    Last edited by Old Pedant; 06-14-2013 at 12:48 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •