I'm trying to make a query using procedural PHP. I've used OOP mostly. It's not giving correct results. I want to get all records from table offers and then make an array width all offer ids in it.
When print_r($sql->getNumRows())
- it gives me correct count. But I want to put ids in array and when I print_r(count($all))
- count is bigger.
What is proper way to do that?
My code is:
$sql->query("SELECT id FROM offers ORDER BY id ASC");
if ($row=$sql->getNumRows()) {
for ($i=0; $i< $sql->getNumRows(); $i++) {
$all[]= $sql->getRow($i);
}
print_r($sql->getNumRows()); //this prints correct number
print_r(count($all)); //this prints bigger count
Any reason you aren't using MySQL Improved Extension ( http://php.net/manual/en/book.mysqli.php )?
I believe the way you are trying to do it has been deprecated. You could try something like this:
<?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 id FROM offers ORDER BY id ASC";
$allRows = array();
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
//Store the row in an array for later use
$allRows[] = $row;
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
print_r(count($allRows));
print_r($allRows);
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.