mysql_query

Discussion in 'PHP' started by Doc757, Dec 12, 2012.

  1. #1
    Since moving to updated php 5.3.x I've been unable to continue my happy trail of using my old format of

    $queryb2="SELECT whatever FROM whereever WHERE username='$user' ";
    $resultb2=mysql_query($queryb2);
    $bank2=mysql_result($resultb2,"bank2");
    Code (markup):
    without throwing some BS error about Boolean...

    Sooo now that we are into the new age with the "i" - can someone explain to me, in example code, how to achieve the same result with updated php code for "mysql I" ?
     
    Doc757, Dec 12, 2012 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    $bank2 = mysql_fetch_array($resultb2);

    This is assuming you are selecting only a single row. Otherwise you need to loop through the result set.
     
    jestep, Dec 12, 2012 IP
  3. Doc757

    Doc757 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    yeah, just looking for a single row (single result) for a variable

    thank you
     
    Doc757, Dec 12, 2012 IP
  4. Deluxious

    Deluxious Greenhorn

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #4
    The first thing I'd ask is why not use mysql_fetch_object or mysql_fetch_array if you're returning a single row from the database?

    Also, mysqli supports the same type of procedural style as the legacy mysql_* queries, though object-oriented just makes more sense to use.

    Quick examples:

    
    //procedural mysqli select
    $db = mysqli_connect("localhost", "user", "pass", "database_name");
    
    $query = "select bankCode, bankName from banks where bankId = " . $whateverId;
    $result = mysqli_query($db, $query);
    
    $thisBank = mysqli_fetch_array($result);
    
    echo "Bank Code: " . $thisBank["bankCode"] . "<br/>";
    echo "Bank Name: " . $thisBank["bankName"];
    
    
    //object oriented mysqli select (cleaner code)
    $db = new mysqli("localhost", "user", "pass", "database_name");
    
    $result = $db->query("select bankCode, bankName from banks where bankId = " . $whateverId);
    
    $thisBank = $result->fetch_object();
    
    echo "Bank Code: " . $thisBank->bankCode . "<br/>";
    echo "Bank Name: " . $thisBank->bankName;
    
    
    //could also be written as
    $db = new mysqli("localhost", "user", "pass", "database_name");
    $thisBank = $db->query("select bankCode, bankName from banks where bankId = " . $whateverId)->fetch_object();
    
    echo "Bank Code: " . $thisBank->bankCode . "<br/>";
    echo "Bank Name: " . $thisBank->bankName;
    
    
    Code (markup):
    Check the manual pages for myqli on php.net for examples of pretty much any type of query you'd want to run.

    Hope this helped you.
     
    Deluxious, Dec 12, 2012 IP