my code is this
$sql = "SELECT * FROM faculty";
foreach ($pdo->query($sql) as $row) {
foreach ($row as $key => $value) {
echo $key."-".$value."<br/>";
}
}
it's fairly simple what i'm doing. My question is why am i getting the same value
two times, one with a key the name of the row in mysql (e.g "surname") and one the position of the array.
Thank you for your time.
That's because your default fetch mode is set to
PDO::FETCH_BOTH (integer)
Specifies that the fetch method shall return each row as an array indexed by both column name and number as returned in the corresponding result set, starting at column 0. Try e.g.
$pdo->query($sql, PDO::FETCH_ASSOC)
instead.
see also: http://docs.php.net/pdo.constants
Related
I have a simple query which will fetch all from a users tables
$query = $this->pdo->prepare('SELECT * FROM `users`');
$query->execute();
return $query->fetchAll();
Then i would have a foreach loop like
$results = $User->getUser();
foreach($results as $result){
echo $result['fname'];
echo $result['lname'];
}
But is there a way to display each of the fields without writing each field name?
First, fix your return fetchAll to specify the return style like so:
return $query->fetchAll(PDO::FETCH_ASSOC);
Then you can use an inner loop with your outter loop like so:
//get the results
$results = $User->getUser();
//loop over the results, setting $result to an array representing each row
foreach($results as $result){
//loop over each $result (row), setting $key to the column name and $value to the value in the column.
foreach($result as $key=>$value){
//echo the key and value.
echo "{$key} = {$value}<br>";
}
}
This will output all columns and their value regardless of what columns there are in the array. Following the comments, you can see what I do is, using your code, loop over the outer array that is an array of each row from the query. Then loop over the array from each row getting the column name and the value in that column. For now I am just echo'ing out the column and value. You would likely want to do more like echo this out to a table or whatever your end goal is.
So I am trying what I feel is a simply loop through a record result. My query returns one row of data. I am attempting to simply pull the value of each row from the returning record into a variable called $questions. However, my var $questions when printed out has duplicates in every space. It should read something like Bob|Ted|Joe|Sally and instead it is reading Bob|Bob|Ted|Ted|Joe|Joe|Sally|Sally. Why is the code below running twice in the foreach loop?
while ($row = mssql_fetch_array($result)){
foreach ($row as $col => $value) {
$questions.=$value."|";
}
}
echo "Questions: ".$questions."<br/>";
Here is all you need to do:
$questions = "";
while ($row = mssql_fetch_array($result)){
$questions .= $row['fieldName']."|";
}
echo "Questions: ".$questions."<br/>";
The foreach() in addition to the while() is unnecessary.
The mssql_fetch_array according to PHP doc:
In addition to storing the data in the numeric indices of the result
array, it also stores the data in associative indices, using the field
names as keys.
The result includes a normal array [0...n] with one element for each column, but also an associative array where each value is represented by a key named after the column name.
So if your first column is Id, you could get id from a row in two ways:
$id = $row[0];
# or you could do this
$id = $row['Id'];
This is why you get each value twice when looping through row.
I have an SQL query which returns a single row as a result. This row contains only one column, an integer. I want to put this integer into a variable.
I execute my query and return the results of the query in an array as such:
$row = mysqli_fetch_all($result, MYSQLI_ASSOC);
I would like to save the value of the single column of this single row into a variable, for example $age. How can this be done?
(This query will always return one row with one column, always an integer)
mysqli_fetch_all doesn't return a row. But set of rows.
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
$value = reset($rows[0]);
but it would be more logical to use a more suitable function:
$row = mysqli_fetch_row($result);
$value = $row[0]; // here you go.
Since you have only one column and your query returns only one row, that means you'll get only one value, and since you're fetching an associative array, you can do the following and store it in a variable.
$row = mysqli_fetch_all($result, MYSQLI_ASSOC);
foreach ($row as $r) {
$test = $r['age'];
}
However, since you're getting only one value out of that, you can just leave the result type out, and get $test = $r[0].
i hope to iterate the elements fetched from datebase
But the result looks very unexpected .
I found the code below print the $value and echo "<td id=".$key.$tag.">".$value."</td>";twice. Is there anything i misunderstood?
function selectTable($table){
$sql= "SELECT * FROM ".$table ;
$result=mysql_query($sql)
or die(mysql_error());
return $result;
}
$table = 'battery_con';
$result = selectTable($table);
unset($table);
while($row = mysql_fetch_array($result)){
......
foreach ($row as $key => $value) {
print $value;
echo "<td id=".$key.$tag.">".$value."</td>";
}
.....
}
You are using mysql_fetch_array which by default returns an array with two elements per column (this is what the second (optional) paramter means: result_type = MYSQL_BOTH).
One is indexed with an integer representing the column index, one is indexed with the column name.
That's why you get two entries in your list. You would set the second parameter to MYSQL_ASSOC to get just one value per column.
Please use mysql_fetch_assoc() place of mysql_fetch_array()
I hope it will help
In addition to #Andreas's answer by default mysql_fetch_array gives both associative and numeric indexes, if you don't want this you can limit it with the second parameter in your while loop:
$row = mysql_fetch_array($result, MYSQL_NUM); // numeric keys only
$row = mysql_fetch_array($result, MYSQL_ASSOC); // associative keys only
As previously mentioned by #sonusindhu you can also use mysql_fetch_row to only get numeric keys, or mysql_fetch_assoc to only get associative keys.
Update
The mysql_xxx() functions being deprecated you should consider using the mysqli_xxx() functions instead.
See the example 1 of the php manual for more details:
http://php.net/manual/en/mysqli-result.fetch-array.php
When I try to query some content from certain table under certain column, The echo result shows two values, one with the key 0 and other with the key name of the column.
My code is like this :
$query = "select id from nepal_posts";
$queryExe = mysql_query($query, $connection);
while ($fetched = mysql_fetch_array($queryExe)) {
foreach ($fetched as $key => $value) {
echo $key."----->".$value." ";
}
}
and result was like this :
0----->9 id----->9 0----->10 id----->10
Why there is two times repetition ?
How should I code, to get proper result ?
my db table is like :
id -> 9, 10 title -> About Us / Om Oss, Our Services / VÃ¥r Verksamhet
post -> bla bla, bla bla
mysql_fetch_array
Returns an array of strings that corresponds to the fetched row, or
FALSE if there are no more rows. The type of returned array depends on
how result_type is defined. By using MYSQL_BOTH (default), you'll get
an array with both associative and number indices. Using MYSQL_ASSOC,
you only get associative indices (as mysql_fetch_assoc() works), using
MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).
http://php.net/mysql_fetch_array
Use mysql_fetch_assoc() instead of mysql_fetch_array()
while ($fetched = mysql_fetch_assoc($queryExe)) {
foreach ($fetched as $key => $value) {
echo $key."----->".$value." ";
}
}