tags:

views:

1621

answers:

3

Hello,

I have been trying to convert a php page to mysqli, and have encoutnered some problems. Given the code below, and the way which I have ordered things to work, I would like to know what the better way is using mysqli methods.

Is there an mysqli alternative to mysql_num_rows or is a different method of calculating the number of rows required?

How would I do the following using mysqli?:

$data = mysql_query($countQuery) or die(mysql_error());
$rowcount = mysql_num_rows($data);

What is an alternative for mysql_fetch_assoc? I feel that I should not be using the current rows method I am using, even if there is a replacement function, so what would be the correct approach?

I apologize for these questions, but I have not been able to determine the answers myself so far.

<?php
$con = mysqli_connect("localhost", "user", "", "ebay");
if (!$con) {
    echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error();
    exit;
}
$con->query("SET NAMES 'utf8'");
$cmd = "word";
//normally retrieved from GET
if($cmd=="deleterec") {
    $deleteQuery = "DELETE FROM AUCTIONS1 WHERE ARTICLE_NO = ?";
    if ($delRecord = $con->prepare($deleteQuery)) {
     $delRecord->bind_param("s", $pk);
     $delRecord->execute();
    }
}
$table = 'AUCTIONS';
$brand = "test";
$countQuery = "SELECT ARTICLE_NO FROM ? WHERE upper(ARTICLE_NAME) LIKE '% ? %'";
if ($numRecords = $con->prepare($countQuery)) {
    $numRecords->bind_param("ss", $table, $brand);
    $numRecords->execute();
    $data = $con->query($countQuery) or die(print_r($con->error));
    $rowcount = mysql_num_rows($data);
    $rows = getRowsByArticleSearch($query, $table, $max);
    $last = ceil($rowcount/$page_rows);
}
$self = htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'utf-8');
foreach ($rows as $row) // print table rows {
    echo '<tr>' . "\n";
    echo '<td><a href="#" onclick="doThings(\'Layer2\', \'' . $pk . '\')">'.$row['USERNAME'].'</a></td>' . "\n";
    // repeated for each column
}
function getRowsByArticleSearch($searchString, $table, $max) {
    global $con;
    $recordsQuery = "SELECT ARTICLE_NO, USERNAME, ACCESSSTARTS, ARTICLE_NAME, date_format(str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s'), '%d %m %Y' ) AS shortDate FROM ? WHERE upper(ARTICLE_NAME) LIKE '%?%' ORDER BY str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s')" . $max;
    if ($getRecords = $con->prepare($recordsQuery)) {
     $getRecords->bind_param("ss", $searchString, $table);
     $getRecords->execute();
     $getRecords->bind_result($ARTICLE_NO, $USERNAME, $ACCESSSTARTS, $ARTICLE_NAME, $shortDate);
     while ($getRecords->fetch()) {
      $result = $con->query($recordsQuery);
      $rows = array();
      while($row = mysql_fetch_assoc($result)) {
       $rows[] = $row;
      }
      return $rows;
     }
    }
}
+1  A: 

You can use:

$data->num_rows();
$data->fetch_assoc();

You can check out the docs for more info num_rows and fetch_assoc.

Brian Fisher
+1  A: 

I usually use this directly after my execute:

 $query->execute();
 // store the result first
 $query->store_result();
 $rows = $query->num_rows;
Ross
+1  A: 

I've rewritten your code sample with the correct property / function calls to fix the two issues that you mention in the question:

<?php
$con = mysqli::connect("localhost", "user", "", "ebay");

if (!$con) {
    echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error();
    exit;
}

$con->query("SET NAMES 'utf8'");
$cmd = "word";

//normally retrieved from GET
if($cmd=="deleterec") {
    $deleteQuery = "DELETE FROM AUCTIONS1 WHERE ARTICLE_NO = ?";
    if ($delRecord = $con->prepare($deleteQuery)) {
        $delRecord->bind_param("s", $pk);
        $delRecord->execute();
    }
}

$table = 'AUCTIONS';
$brand = "test";
$countQuery = "SELECT ARTICLE_NO FROM ? WHERE upper(ARTICLE_NAME) LIKE '% ? %'";

if ($numRecords = $con->prepare($countQuery)) {
    $numRecords->bind_param("ss", $table, $brand);
    $numRecords->execute();
    $data = $con->query($countQuery) or die(print_r($con->error));

    // Here is the property that you can reference to determine the affected rows from the previous query. -- gabriel
    $rowcount = $data->num_rows; 
    $rows = getRowsByArticleSearch($query, $table, $max);
    $last = ceil($rowcount/$page_rows);
}

$self = htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'utf-8');

foreach ($rows as $row) // print table rows {
    echo '<tr>' . "\n";
    echo '<td><a href="#" onclick="doThings(\'Layer2\', \'' . $pk . '\')">'.$row['USERNAME'].'</a></td>' . "\n";
    // repeated for each column
}

function getRowsByArticleSearch($searchString, $table, $max) {
    //global $con; Globals are BAD!! Please don't use.
    $con = mysqli::connet("localhost", "user", "", "ebay");

    $recordsQuery = "SELECT ARTICLE_NO, USERNAME, ACCESSSTARTS, ARTICLE_NAME, date_format(str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s'), '%d %m %Y' ) AS shortDate FROM ? WHERE upper(ARTICLE_NAME) LIKE '%?%' ORDER BY str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s')" . $max;
    if ($getRecords = $con->prepare($recordsQuery)) {
        $getRecords->bind_param("ss", $searchString, $table);
        $getRecords->execute();
        $getRecords->bind_result($ARTICLE_NO, $USERNAME, $ACCESSSTARTS, $ARTICLE_NAME, $shortDate);
        while ($getRecords->fetch()) {
                $result = $con->query($recordsQuery);
                $rows = array();

       // Notice the adjusted function call to retrieve the associate array for each record within the result set. -- gabriel
                while($row = $result->fetch_assoc()) {
                        $rows[] = $row;
                }
                return $rows;
        }
    }
}

However, I would like to add that dusoft was correct in the assessment that MySQLi is buggy and is known to create issues. It is for this reason that we had to switch from MySQLi to PDO (which is native to PHP as of at least 5.1) and we haven't had any problems since. I would strongly recommend that you look at PDO or perhaps one of the Pear libraries to actually provide your database connection interface.

Noah Goodrich
Ahh, many thanks. Just a quick question, but do you see any reason for $countquery to fail? I am getting an incorrect syntax error, but the query works fine when I put it in manually.
Joshxtothe4
also, why are globals necessarily bad, at least in my example?
Joshxtothe4