Can't fetch rows using mysqli

Discussion in 'PHP' started by qwikad.com, Sep 22, 2015.

  1. #1
    Thought it would be a breeze... What am I doing wrong? This one doesn't fetch any results:

    
    <?php
    $mysqli = new mysqli("localhost", "my_user", "my_password", "my_db");
    
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s\n", $mysqli->connect_error);
        exit();
    }
    
    $query = "SELECT user, favid FROM ajaxfavourites";
    
    if ($result = $mysqli->query($query)) {
    
        while ($row = $result->fetch_assoc()) {
    
      echo $row["user"];
      echo $row["favid"];
    
        }
    
    
        $result->free();
    }
    
    $mysqli->close();
    ?>
    
    Code (markup):
    This one does:

    
    <?php
    
    $conn = mysql_connect("localhost", "my_user", "my_password");
    
    if (!mysql_select_db("my_db")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
    }
    
    $sql = "SELECT favid,user FROM  ajaxfavourites";
    
    $result = mysql_query($sql);
    
    
    
    while ($row = mysql_fetch_assoc($result)) {
    
    echo $row["user"];
    echo $row["favid"];
    
    }
    mysql_free_result($result);
    
    
    ?>
    
    Code (markup):

     
    qwikad.com, Sep 22, 2015 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    On a quick visual check I don't see anything wrong with the first one other than it checking errno instead of error. Never seen it done that way before. If you're still on a version of PHP before 5.2.9 there can be some quirks in how mysqli handles error reporting, another reason I prefer PDO -- but that's mostly in the past since one can usually at least expect PHP 5.3 to be supported. (I have stopped worrying about anything prior to 5.4 at this point).

    I would probably lose the variable for nothing, and use the object version instead of the garbage legacy functional wrappers -- but apart from that this (unless I'm not seeing something REALLY obvious):

    <?php
    
    $mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
    
    if ($mysqli->connect_error) die(
    	'Connect Error (' . $mysqli->connect_errno . ' ) ' .
    	$mysqli->connect_error
    );
    
    if ($result = $mysqli->query('
    	SELECT user, favid FROM ajaxfavourites
    ')) {
    	while ($row = $result->fetch_assoc()) echo $row['user'], $row['favid'];
    	$result->free();
    }
    
    $mysqli->close();
    
    ?>
    Code (markup):
    Shouldn't be outputting anything significantly different from your older mysql_ version. Again though, there's a LOT of reasons I prefer PDO -- not just that it can target more than just mysql, but also that you can use try/catch for error handling meaning proper errors are logged, and that you can directly foreach iterate the query set result:

    <?php
    
    try {
    	$db = new PDO(
    		'mysql:host=localhost;dbname=my_db',
    		'my_user',
    		'my_password'
    	);
    } catch (PDOException $e) {
    	die('Connection failed: ' . $e->getMessage);
    }
    
    foreach ($db->query('
    	SELECT user, favid FROM ajaxfavourites
    ') as $row) echo $row['user'], $row['favid'];
    
    ?>
    Code (markup):
     
    deathshadow, Sep 22, 2015 IP
  3. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,279
    Likes Received:
    1,696
    Best Answers:
    31
    Trophy Points:
    475
    #3
    I tried both. The PDO worked and the other one didn't, which got me thinking I need to tweak with my Apache settings to enable mysqli extension handling. Dang it. There's always something that's NOT working. C'est la vie.

    **The php editor shows pdo.so, pdo_sqlite.so, pdo_mysql.so as supported extensions. I added mysqli.so and saved it. The php.ini shows extension = "mysqli.so" but it still doesn't execute mysqli_ scripts. Does it mean I need to rebuild the whole thing through EasyApache?
     
    Last edited: Sep 23, 2015
    qwikad.com, Sep 23, 2015 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    No clue, I've NEVER encountered a build of PHP that didn't have those enabled, and I've never used easyApache... but I'm a Debian guy when it comes to servers; for me it's apt-get or GTFO. Typically those go in with the php-common package.

    Well, that or local testing where I just use XAMPP.

    What/where are you hosting that it's not included?
     
    deathshadow, Sep 23, 2015 IP
  5. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,279
    Likes Received:
    1,696
    Best Answers:
    31
    Trophy Points:
    475
    #5
    I am with hostgator. I don't think it's hostgator's issue per say. A while back I upgraded my cpanel php version and as I found out today I should've selected mysqli support or something like that in one of the options while upgrading. I guess I didn't. The only beef I have with them is why the hell did they not make THAT FACT obvious to anyone who upgrades their cpanel to a new php version? Anyway, I kinda starting liking PDO, although I am mostly clueless and have no idea how to convert some of the old stuff to it, that's why I asked it here: https://forums.digitalpoint.com/threads/need-to-convert-a-small-mysql-script-to-pdo.2764500/
     
    qwikad.com, Sep 23, 2015 IP