the following mysql query is only returning a single row when it should be returning 4.
$query = "SELECT * FROM questions";
$result = mysql_query($query) or die("ERROR: $query.".mysql_error());
// if records are present
if (mysql_num_rows($result) > 0) {
while ( $row = mysql_fetch_object($result) ){
// get question ID and title
$qid = $row->qid;
echo '<div id=ques>';
echo '<h2>'.$row->qtitle .'</h2>';
echo '</div>';
print_r ($row);
the print_r function displays this:
stdClass Object ( [qtitle] => dummy text here [qid] => 1 )
mysql_fetch_*() only pulls a single row at a time. Without seeing the rest of the loop it's impossible to tell if something else is going on down there.
You don't have closing brackets for while-loop and if
Related
I am having some trouble trying to get this done. The issue is:
I have an array that looks like
wants_arr_flat ( [0] => booze [1] => nudes [2]
=> rooms
I want my foreach loop to go through these values making a different SQL statement and then saving the result of that SQL statement on to another array. The code I am trying to work with now is this.
foreach ( $wants_arr_flat as $value ) {
$sql = "SELECT * FROM offers WHERE user_id != $_SESSION[user_id] AND offer='$value'";
$result=$conn->query($sql);
$want_match_arr = mysqli_fetch_row($result);
echo '<pre>'; print_r($want_match_arr); echo '</pre>'; //testing echo
Obviously this is just overwriting the last array each iteration so I only end up with one result in the array.
instead of
$want_match_arr = mysqli_fetch_row($result);
use
$want_match_arr[] = mysqli_fetch_row($result);
If you get multiple rows from SQL then this is better.
$want_match_arr = [];
foreach ( $wants_arr_flat as $value ) {
$sql = "SELECT * FROM offers WHERE user_id != $_SESSION[user_id] AND offer='$value'";
$result=$conn->query($sql);
while ($row = mysql_fetch_assoc($result)) {
$want_match_arr[] = $row;
}
}
echo '<pre>'; print_r($want_match_arr); echo '</pre>'; //testing echo
I need to fetch data from my db. I've done it but when I say fetch array [1], the output is the all second letters in all rows. Here is the code:
include "baglan2.php";
$query = $db->query("SELECT * FROM diziler", PDO::FETCH_ASSOC);
if ( $query->rowCount() ){
foreach( $query as $row ){
echo ($row['link']. "<br />");
}
I tried it with msqli but saw the same result; here is the mysqli code:
$query = mysqli_query($baglanti, "SELECT * FROM diziler");
if ( mysqli_affected_rows($baglanti) ){
while ( $row = mysqli_fetch_assoc($query) ){
print $row['link'][1]."<br />";
}
}
For instance all my data are links. When I run print $row['link'][1], it gives "t" letter from "http:" in all rows. I need to fetch my data by row not column. I have tried every method possible. However I couldn't find any method that worked.
for instance I want to make this codes output "http://**.com" in each element.
I am giving solution based on second attempt:-
$query = mysqli_query($baglanti, "SELECT link FROM diziler");
$link_array = array();
if ( $query){
while ( $row = mysqli_fetch_assoc($query) ){
$link_array[] = $row['link'];
}
}
echo "<pre/>";print_r($link_array); // it have all the links
Reason:- https://eval.in/649070
It's specification is given here:- https://stackoverflow.com/a/17193651/4248328
That is:- $string[int] is syntactic sugar for substr($string, int, 1)
I am trying to fetch a column from a database and display the entire contents as an array. I have so far been able to fetch only one of the entries from the table. I understand that I am fetching only $row[0] which is why i am getting only 1 element. I have tried to fetch all elements but I have not succeeded yet. can someone please let me know how to do it correctly? I have attached my code below.
<?php
$con=mysqli_connect("localhost","root","raspberry","users");
if (mysqli_connect_errno())
{
echo "failled to connect:".mysqli_connect_error();
}
$sql = "SELECT `current` FROM `monitor` ORDER BY `Sno.`";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result,MYSQLI_NUM);
printf("%s\n",$row[0]);
mysqli_close($con)
?>
PDO has a dedicated feature for your problem PDOStatement::fetchColumn.
So, from your question what I understand is- you want to fetch a database column and store this in a array which will make you to display the results.
<?php
$con=mysqli_connect("localhost","root","raspberry","users");
if (mysqli_connect_errno())
{
echo "failled to connect:".mysqli_connect_error();
}
$sql = "SELECT current FROM monitor ORDER BY Sno";
$result = mysqli_query($con,$sql);
$arr = array();
while ($row = mysqli_fetch_array($result)) {
$arr2 = array();
foreach ($row as $val) $arr2[] = $val;
$arr[] = $arr2;
}
$first_col = array_column($arr, 0);
print_r($first_col);
mysqli_close($con)
?>
i.e. if you have five entry to the database whose first column contains
the values like 'current1', 'current2', 'current3', 'current3' and 'current4' the result of this code will be
Array
(
[0] => current1
[1] => current2
[2] => current3
[3] => current4
[4] => current5
)
I have a question about this ....
$query = 'SELECT *
FROM EXAMPLE';
$result = mysql_query($query);
$row = mysql_fetch_array ($result);
print_r($row);
This has the data of the first row [0] => 1 [id]=> 1... blah, blah but now I'm just wondering, what about the other table data? Is it a php safety to not display the "full array" of all the data inside EXAMPLE table? Just the first row of the data?
This is just out of curiosity.
I know if I want to see specific part of the entire data I can do a while loop.
while ($row = mysql_fetch_array ($result)) {
echo $row['text'].'<br>';
}
mysql_fetch_array only returns one row of data. In your second example you are calling mysql_fetch_array over and over for every row.
To output all your data, do the following:
$query = 'SELECT * FROM EXAMPLE';
$result = mysql_query( $query );
print( '<pre>' ); // Preserve Whitespace/Newlines
while ( $row = mysql_fetch_array( $result ) )
print_r($row);
print( '</pre>' );
If you want to see a specific part of the entire data, do the following:
while ( $row = mysql_fetch_assoc( $result ) )
echo $row['text'].'<br>';
$query = "SELECT * FROM table";
$result = mysql_query($query, $db);
$all = mysql_fetch_assoc($result);
echo mysql_num_rows($result) . ":" . count($all);
This returns
2063:7
I have not used count before, so I'm not 100% sure it's not counting the table columns. It's late and I might be going nuts.
Here's another example of what's happening:
$result = mysql_query($query, $db);
echo "Rows: " . mysql_num_rows($result) . " <BR />";
$player_array = mysql_fetch_assoc($result);
echo "<pre>";
print_r($player_array);
echo "</pre>";
Which outputs:
Rows: 9
Array
(
[playerID] => 10000030
)
TL;DR: I submit queries which return multiple rows, but fetch_array only gives me a small portion of those rows in the resulting array.
mysql_fetch_assoc returns only one row in once you have to use loop to retrieve all rows
while($row = mysql_fetch_assoc($result))
{
print_r($row);
}
Every call to mysql_fetch_assoc($result); gives you one row of the result set:
(from the documentation)
mysql_fetch_assoc — Fetch a result row as an associative array
Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array.
You have to use the function in a loop:
$all = array();
while(($row = mysql_fetch_assoc($result))) {
$all[] = $row;
}
The example in the document shows how it is done.
mysql_fetch_assoc doesn't work that way, you need to call it multiple times to get all rows. Like this:
while ($row = mysql_fetch_assoc($db_result))
{
print_r($row);
}