display unique values from MYSQL PHP - php

I am trying to get the isbn number of a book from a database, but with my command it is giving me an associative array.
How do I get it to return just the isbn number in the array.
Code:
function view_all_names($db)
{
$query = "SELECT isbn from books";
$statement = $db->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$statement->closeCursor();
return $result;
}

If $db is a PDO object you can use
$result = $statement->fetchAll(PDO::FETCH_COLUMN);
PDOStatement::fetchAll

you should take only isbn numbers use mysql_fetch_assoc() with while loop.

Related

PDO::Fetch_classtype returns std class instead of first argument of sql query

$sql = "select classname,id,name,value,description from tablename";
$query = $this->conn->prepare($sql);
$query->execute();
$result = $query->fetch(PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE);
var_dump($result);
Result shows stdClassObject
Expected is the classname object (of the particular row)
Basically, am trying to loop through the result, figure out which class the row belongs to and access their getter functions. Same is achieved if I use the following, but not the solution that works for me.
$sql = "select classname,id,name,value,description from tablename";
$query = $this->conn->prepare($sql);
$query->execute();
$result = $query->fetch(PDO::FETCH_CLASS, 'MyClass');
var_dump($result);
Now, I can loop through the result and access $result->getId() or $result->getName() etc. How I can achieve this through PDO::FETCH_CLASSTYPE as the classname is dynamically decided by the row in the result set.

Iteration error when fetching mysqli data with php [duplicate]

I am trying to do an extremely simple query using mysqli. It is driving me mad!
I just want $data to be an array of the values from the sql query.
This is my code...
$req = $app->request();
$hashtag = $req->get('hashtag');
require_once 'Slim/lib/database.php';
$db = connect_db();
$statement = $db->prepare("SELECT `content` FROM `posts` WHERE `content` LIKE ?");
$newhashtag = '%#' . $hashtag . '%';
$statement -> bind_param("s", $newhashtag);
$statement -> execute();
$statement -> bind_result($result);
while ( $row = mysqli_fetch_array($statement) ) {
$data[] = $row;
}
print_r($data);
$statement -> close();
I just get an error mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given and it doesn't make a difference using $result or $statement on the fetch_array
You can try this:
$statement = $db->prepare("SELECT `content` FROM `posts` WHERE `content` LIKE ?");
$newhashtag = "%#$hashtag%";
$statement->bind_param("s", $newhashtag);
$statement->execute();
$result = $statement->get_result();
while ($row = $result->fetch_assoc())
{
$data[] = $row;
}
This uses the get_result() function, which is used to get the result from a prepared statement.
This is initialised outside the while loop and assigned to a new variable, in this instance $result.
Then $result->fetch_assoc() is assigned to $row and can be accessed within the loop. Accessing each column as a key from the array such that $row["content"] would return the content from each row under that column
To get the result object from the prepared statement you can use get_result(). This object can then be iterated with a foreach loop.
$statement->execute();
$result = $statement->get_result();
foreach ($result as $row) {
print_r($row);
}
If you need to fetch all rows into an array, you can use fetch_all().
$statement->execute();
$result = $statement->get_result();
$data = $result->fetch_all(MYSQLI_ASSOC);
print_r($data);
You can also fetch the data by binding each column to a variable. First, you specify a variable you want to be populated using bind_result() and then you call fetch() to populate that variable.
$statement->execute();
$statement->bind_result($content);
while ( $statement->fetch() ) {
// Every time fetch is called a value from the next row will be inserted into $content
$data[] = $content;
}
print_r($data);

PDO Query Showing No Results

So previously my queries were working fine. I usually use mysql but have recently changed to a pre-configured vps through godaddy. Last night I was trying to connect to my server via PDO, which is showing the connection is being made.
Next I was trying to select the data from a table using:
global $conn;
$sql = "SELECT name FROM suppliers";
$stm = $conn->prepare($sql);
$stm->execute();
return $stm->fetch();
All this shows on my website is Connection Successful, Array
It doesn't show any array information or anything, just the word "Array". The table has information in it, so it should be displaying the results. Any idea of what I am doing wrong?
You get Array because you are echoing an array. Fetch returns an array,
PDO::FETCH_BOTH (default): returns an array indexed by both column name and 0-indexed column number as returned in your result set
Since you are querying a full table you probably want all the results so you should loop the fetch and return that array.
$sql = "SELECT name FROM suppliers";
$stm = $conn->prepare($sql);
$stm->execute();
while($row = $stm->fetch()) {
$returned_array[] = $row['name'];
}
return $returned_array;
Then iterate over this where ever you are using it for all the supplier names.
foreach(function_call_for_names() as $name) {
echo $name;
}
Alternately, you could use the PDO fetchAll function,
$sql = "SELECT name FROM suppliers";
$stm = $conn->prepare($sql);
$stm->execute();
return $stm->fetchAll();
then
foreach(function_call_for_names() as $row) {
echo $row['name'];
}

mysqli prepared statement with while loop

I am trying to do an extremely simple query using mysqli. It is driving me mad!
I just want $data to be an array of the values from the sql query.
This is my code...
$req = $app->request();
$hashtag = $req->get('hashtag');
require_once 'Slim/lib/database.php';
$db = connect_db();
$statement = $db->prepare("SELECT `content` FROM `posts` WHERE `content` LIKE ?");
$newhashtag = '%#' . $hashtag . '%';
$statement -> bind_param("s", $newhashtag);
$statement -> execute();
$statement -> bind_result($result);
while ( $row = mysqli_fetch_array($statement) ) {
$data[] = $row;
}
print_r($data);
$statement -> close();
I just get an error mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given and it doesn't make a difference using $result or $statement on the fetch_array
You can try this:
$statement = $db->prepare("SELECT `content` FROM `posts` WHERE `content` LIKE ?");
$newhashtag = "%#$hashtag%";
$statement->bind_param("s", $newhashtag);
$statement->execute();
$result = $statement->get_result();
while ($row = $result->fetch_assoc())
{
$data[] = $row;
}
This uses the get_result() function, which is used to get the result from a prepared statement.
This is initialised outside the while loop and assigned to a new variable, in this instance $result.
Then $result->fetch_assoc() is assigned to $row and can be accessed within the loop. Accessing each column as a key from the array such that $row["content"] would return the content from each row under that column
To get the result object from the prepared statement you can use get_result(). This object can then be iterated with a foreach loop.
$statement->execute();
$result = $statement->get_result();
foreach ($result as $row) {
print_r($row);
}
If you need to fetch all rows into an array, you can use fetch_all().
$statement->execute();
$result = $statement->get_result();
$data = $result->fetch_all(MYSQLI_ASSOC);
print_r($data);
You can also fetch the data by binding each column to a variable. First, you specify a variable you want to be populated using bind_result() and then you call fetch() to populate that variable.
$statement->execute();
$statement->bind_result($content);
while ( $statement->fetch() ) {
// Every time fetch is called a value from the next row will be inserted into $content
$data[] = $content;
}
print_r($data);

PDO Fetch Array like MySQLi

I know how to fetch a PDO array, but how do I collect data from it like you do with MySQLi's fetch_array?
For example,
MySQLi
$query = $mysqli->query("SELECT * FROM `foo` WHERE `ID`='1'");
$array = $query->fetch_array();
Getting a result
echo $array['bar'];
How would you do this with PDO? I understand you can do this:
PDO
$query = $pdo->prepare("SELECT * FROM `foo` WHERE `ID`='1'");
$query->execute();
$result = $query->fetchAll();
Getting the result
echo $result['bar'];
Does not return the same as MySQLi did
Am I doing something wrong, and is there a way of doing this?
fetchAll() is not the same as fetch_array().
You want fetch() to get one row, not fetchAll() which gets ALL rows.
$result = $query->fetch(PDO::FETCH_ASSOC);

Categories