I have got the query below and i am trying to loop through the result but the echo only returns the second letter of the array instead of the whole 2nd array.
$tsql = "SELECT UserId, Email FROM Membership";
$stmt = sqlsrv_query($conn, $tsql);
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC);
while ($row) {
$body .= $row[0];
foreach($row as $email)
{
echo $email[1]. "<BR> ";
}
}
;
your code has too many issues. you should use something like
$tsql = "SELECT UserId, Email FROM Membership";
$stmt = sqlsrv_query($conn, $tsql);
while ($row = sqlsrv_fetch_assoc($stmt, SQLSRV_FETCH_ASSOC) ) {
echo $row [UserId]. "<BR> ";
echo $row [Email ]. "<BR> ";
};
Related
Usually when you do the basic php excercises you just use the following to print data on a blank page:
<?php
include('../includes/dbh.php');
$titulo = mysqli_real_escape_string($conn,$_GET['titulo']);
$sql = "SELECT * FROM proyectos WHERE proyect_name='$titulo'";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
if($queryResults > 0){
while ($row = mysqli_fetch_assoc($result)){
echo "<div>".$row['proyect_name']. "
</div>";
}
}
?>
My question is, if I try to separate and echo out $row proyect_name in another part of the webpage nothing happens. I used both echo and var_dump and yes, the query works but it just doesn't print anything. I am new to this so yes, it may be obvious to you but not to me. I think it may be the while loop, but I do not know how to "blend it in".
<?php
include('../includes/dbh.php');
$titulo = mysqli_real_escape_string($conn,$_GET['titulo']);
$sql = "SELECT * FROM proyectos WHERE proyect_name='$titulo'";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
$data=array();
if($queryResults > 0){
while ($row = mysqli_fetch_assoc($result)){
echo "<div>".$row['proyect_name']. "
</div>";
$data[] = $row;
}
}
foreach($data as value) {
echo $value;
}
// OR
foreach($data as $key => $value) {
echo $key . " - " . $value;
}
?>
Set variable before loop then add $row data to it then loop it out with a foreach loop Does this work??
Use mysqli_fetch_array() - Example
So in your case it would be
<?php
include('../includes/dbh.php');
$titulo = mysqli_real_escape_string($conn,$_GET['titulo']);
$sql = "SELECT * FROM proyectos WHERE proyect_name='$titulo'";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
if($queryResults > 0){
while ($row = $result->fetch_array()){
$rows[] = $row;
}
foreach ($rows as $row) {
echo "<div>".$row['proyect_name']."</div>";
}
}
?>
i want to Display the Result ('Positio: 2' or 'Position: 1') via a Echo
but $statement is a Object of class PDOStatement and not a String, how do i get it to see only the result of $statement? Thank you
<?php
$pdo = new PDO('mysql:host=localhost;dbname=testdb1', 'root', '');
$idV = $_GET['id'];
$statement = $pdo->prepare("SELECT position FROM idtabelle WHERE idnumber = ?");
$statement->execute(array($idV));
echo "Position: //$result_Of_Statement\\ ";
?>
Here's how I would do it:
$statement = $pdo->prepare("SELECT position FROM idtabelle WHERE idnumber = :idV");
$statement->bindParam(':idV', $idV);
$statement->execute();
while ($row = $statement->fetch(PDO::FETCH_ASSOC))
{
echo $row['position'];
}
You could also do away with the while loop if you wanted to.
I would fetch the result and use print_r or var_dump to see the result quickly.
$statement = $pdo->prepare("SELECT position FROM idtabelle WHERE idnumber = ?");
$statement->execute(array($idV));
$result = $statement->fetch(PDO::FETCH_ASSOC); //Or fetchAll()
echo "Position: ". print_r($result);
That's a quick and dirty way to inspect the result. If you want to add some formatting:
$result = $statement->fetch(PDO::FETCH_ASSOC);
for( $result as $key=>$value ){
echo ucfirst($key) .": $value \n";
}
If you want to get multiple rows wrap that in a loop where you fetch a new row each iteration
while( $result = $statement->fetch(PDO::FETCH_ASSOC) ){
echo "===New Row===";
for( $result as $key=>$value ){
echo ucfirst($key) .": $value \n";
}
}
In this code below I need to echo the result of the subquery.
When I echo $row['email'].'<br>'; the echo works okay and I get all the emails.
Problem is that this:
$resultsub.echo $rowb["email"];
is empty
Question: what do I do wrong in the subquery: $resultsub?
$result = mysqli_query($conn, "SELECT * FROM wp_mm_email_bounces");
if ($result) {
// output data of each row
while( $row = mysqli_fetch_assoc( $result ) )
{
echo $row['email'].'<br>'; // echo works okay
$resultsub = mysqli_query($conn, "SELECT * FROM `wp_mm_external_emails` WHERE `email` = '". $row['email'] ."' ");
// when I echo this subquery it runs ok. but somehow I get an empty
while( $rowb = mysqli_fetch_assoc( $resultsub) )
{
echo $rowb["email"];
} else {
echo "0 $resultsub";
}
} else {
echo "0 results";
}
I think you somehow messed your if-else and while parts up. If I "pretty-print" your code snippet above it looks like this (see my comments):
<?php
$result = mysqli_query($conn, "SELECT * FROM wp_mm_email_bounces");
if ($result)
{
while( $row = mysqli_fetch_assoc( $result ) )
{
echo $row['email'].'<br>';
$resultsub = mysqli_query($conn, "SELECT * FROM `wp_mm_external_emails` WHERE `email` = '". $row['email'] ."' ");
while( $rowb = mysqli_fetch_assoc( $resultsub) )
{
echo $rowb["email"];
}
else //Where is the corresponding if?
{
echo "0 $resultsub";
}
}
} //I added this enclosing bracket to the if-statement
else //If this is the corresponding else of your first if at the beginning, then you never closed it
{
echo "0 results";
}
?>
Why you don't use INNER JOIN query?
See the example below:
$sqlString = "SELECT ebounces.email as bouces_email, ext.email as ext_email FROM wp_mm_email_bounces ebouces INNER JOIN wp_mm_external_emails ext ON ebounces.email = ext.email";
$result = mysqli_query($conn, $sqlString);
if ($result) {
// output data of each row
while( $row = mysqli_fetch_assoc( $result ) )
{
echo $row['bouces_email'].'<br>';
echo $row["ext_email"];
}
}
else {
echo "0 results";
}
Ps. Put all columns that you need in the SQL Statement.
My bests
I need to print out all the usernames of my members in my site as a list, but I don't know how to do it. I tried to use 'mysqli_fetch_array' function to fetch names from username column, but I've been failing. So, please assist me on how I can do this in a most efficient way.
This is my code
$sql = "SELECT username FROM users";
$query = mysqli_query($db_con, $sql);
$array = Array;
while($row = mysqli_fetch_array($query)) {
$array = row[];
}
foreach ($names as $array) {
echo $names;
}
Also I want to format the output, like separating each username by " | "
You are quite close..
$sql = "SELECT username FROM users";
$query = mysqli_query($db_con, $sql);
while($row = mysqli_fetch_assoc($query)) {
echo $row['username'].'|';
}
Couple of things, "Array" should be "array()" and you're doubling up, by assigning array values into an array, results are returned in an array already. Code below should work...
$sql = "SELECT username FROM users";
$query = mysqli_query($db_con, $sql);
$array = mysqli_fetch_array($query));
foreach ($array as $name) {
echo $name["username"] . "|";
}
Looks like you also had your foreach loop round the wrong way
no need to use extra foreach, just do this way-
$sql = "SELECT username FROM users";
$query = mysqli_query($db_con, $sql);
$array = array();
while($row = mysqli_fetch_assoc($query)) {
echo $row["username"] . "|";
}
Don't worry it is really very easy
<?php
//All your preceding code
$sql = "SELECT username FROM users";
$query = mysqli_query($db_con, $sql);
?>
<ul>
<?php
while($row = mysqli_fetch_array($query)) {
echo "<li>" . $row['username'] . "</li>";
}
?>
</ul>
The query I have below will only show me one result even if there are multiple matching entries (completely or partially matching). How do I fix it so it will return all matching entries:
//$allowed is a variable from database.
$sql = "SELECT `users`.`full_name`, `taglines`.`name`, `users`.`user_id` FROM
`users` LEFT JOIN `taglines` ON `users`.`user_id` = `taglines`.`person_id`
WHERE ( `users`.`user_settings` = '$allowed' ) and ( `users`.`full_name`
LIKE '%$q%' ) LIMIT $startrow, 15";
$result = mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
$num_rows1 = mysql_num_rows($result);
if ($result == "")
{
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0)
{
}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{
$person = htmlspecialchars($row['full_name']);
}
}
}
print $person;
Because your overwriting $person on each iteration.
Hold it in a $person[] array if your expecting more then one. Then loop through it with a foreach loop when you intend to output.
Not related but your also querying twice, you only need 1 $result = mysql_query($sql);
Update (Simple Outputting Example):
<?php
$person=array();
while($row = mysql_fetch_array($query)){
$person[] = array('full_name'=>$row['full_name'],
'email'=>$row['email'],
'somthing_else1'=>$row['some_other_column']);
}
//Then when you want to output:
foreach($person as $value){
echo '<p>Name:'.htmlentities($value['full_name']).'</p>';
echo '<p>Eamil:'.htmlentities($value['email']).'</p>';
echo '<p>FooBar:'.htmlentities($value['somthing_else1']).'</p>';
}
?>
Or an alternative way to is to build your output within the loop using concatenation.
<?php
$person='';
while($row = mysql_fetch_array($query)){
$person .= '<p>Name:'.$row['full_name'].'</p>';
$person .= '<p>Email:'.$row['email'].'</p>';
}
echo $person;
?>
Or just echo it.
<?php
while($row = mysql_fetch_array($query)){
echo '<p>Name:'.$row['full_name'].'</p>';
echo '<p>Email:'.$row['email'].'</p>';
}
?>