Can't loop more than 1 row Mysql - php

I'm trying to get all rows from database , but when my database only have 1 row, it will show on website, if i'm add more 1 row, nothing show in website.
My code:
require_once('data/class.dbsetting.php');
Mysql sql = new Mysql();
sql->connect();
$query = 'select * from accounts';
$result = $sql->query($query);
While($row = $sql->fetch_assoc($result))
{
echo $row['Username'];
}

Try with :
mysql_fetch_all($result,MYSQL_ASSOC);

Related

SELECT statement returns first row instead of record that is looked up

I am trying to select a record from my database, and I am return instead the first one in the table. No matter what I try, the first one gets returned.
Here's the query:
$query_task_owner = "select user_id from users where full_name = '$c_task_owner_name'";
$response = #mysqli_query($dbc, $query_task_owner);
Then I try a test to see the value that is returned as such:
echo $response or die(mysql_error());
This is where I see the user_id of the first row.
Even if I try to put a specific value in the query, as follow, I am getting the same result:
$query_task_owner = "select user_id from users where full_name = 'LeBron James'";
I do not understand because when I trying this query directly in PHPMyAdmin, I am getting the right result. So the query itself is correct.
Any idea?
Fetch $response using mysqli_fetch_array().
<?php
$query_task_owner = "select user_id from users where full_name = '$c_task_owner_name'";
$response = #mysqli_query($dbc, $query_task_owner);
$row = mysqli_fetch_array($response,MYSQLI_ASSOC);
echo $row['user_id'];
?>
If, users are more related to that full name. Then, use while loop to fetch all record.
<?php
$query_task_owner = "select user_id from users where full_name = '$c_task_owner_name'";
$response = #mysqli_query($dbc, $query_task_owner);
while($row = mysqli_fetch_array($response,MYSQLI_ASSOC))
{
echo $row['user_id']."<br>";
}
?>

Want to give values in sql command dynamically by the dropdown box?

I have a Attendance table containing attendance of all the employee.
Currently i am working on report_generation page.
So, i want to pass emp_mail id through drop-down box to fetch records of that particular employee.
here is the database table containing records
Here is the code from the report generation page:
$dbh = getDBH();
$output = "";
$sql = "SELECT ATT.AttDate FROM attandance ATT";
$query = $dbh->prepare($sql);
$query->execute();
$results = $query->fetchAll(\PDO::FETCH_OBJ);
$arrDates = array();
foreach($results as $intKey=>$objAttDate)
{
$arrDates[] = $objAttDate->AttDate;
}
var_dump($arrDates);

incorrect result display from database

I have a database table that has 4 records with a column _id that auto increments. When I run a query to get all records, it works but it doesn't echo out all the ids, it only points to the first rows and echos it four times. I am using PHP and MySQLi. Here is my code
Code for querying
$sql = "SELECT * FROM att_table";
$query = $conn->query($sql);
$result = $query->fetch_assoc();
Code for display
do{
echo result['_id'];
}while($query->fetch_assoc());
It outputs 1111 instead of 1234. Please what is wrong?
You're fetching each of the 4 results, so it loops the appropriate number of times; but you're only assigning the fetched result to $result once, so that's the only _id value that gets echoed
do{
echo $result['_id'];
}while($result = $query->fetch_assoc())
You also can use a foreach loop :
$sql = "SELECT * FROM att_table";
$query = $conn->query($sql);
$result = $query->fetch_assoc();
foreach($result as $data){
echo $data['_id'];
}

Mysql select and get data from a row

$mysqli = new mysqli("localhost", "root", "", "secure_login");
if ($result = $mysqli->query("SELECT members FROM hannes")) {
echo $result->row['firstlogin'];
$result->close();
}
I'm quite new to Mysql and I have a problem where I want to get the value of the row "firstlogin" from the user "hannes"
My table looks like this (table name is "members")
Username firstlogin
hannes 0
rachel 2
adam 1
What am I doing wrong?
Try this,
$mysqli->query("SELECT `firstlogin` FROM `members` where `Username`='hannes'")
Full code: You need to use fetch_row to get the rows from table as like below, if you have multiple rows in your table then you need to use while loop. In your case you have only one record so there is no need of while loop
if ($result = $mysqli->query("SELECT `firstlogin` FROM `members` where `Username`='hannes'")) {
$row = $result->fetch_array();
echo $row['firstlogin'];
$result->close();
}

Multiple SELECT Statements and INSERTS in 1 file

I'm working with a file and I'm attempting to do multiple select statements one after another and insert some values. So far the insert and the select I've got working together but when attempting to get the last SELECT to work I get no value. Checking the SQL query in workbench and everything works fine. Here's the code:
$id = "SELECT idaccount FROM `animator`.`account` WHERE email = '$Email'";
$result = mysqli_query($dbc, $id) or die("Error: ".mysqli_error($dbc));
while($row = mysqli_fetch_array($result))
{
echo $row[0];
$insert_into_user = "INSERT INTO `animator`.`user` (idaccount) VALUES ('$row[0]')";
}
$select_userid = "SELECT iduser FROM `animator`.`user` WHERE iduser = '$row[0]'";
$results = mysqli_query($dbc, $select_userid) or die("Error: ".mysqli_error($dbc));
while($rows = mysqli_fetch_array($results))
{
echo $rows[0];
}
I do not want to use $mysqli->multi_query because of previous problems I ran into. Any suggestions? And yes I know the naming conventions are close naming... They will be changed shortly.
Your code makes no sense. You repeatedly build/re-build the $insert_int-User query, and then NEVER actually execute the query. The $select_userid query will use only the LAST retrieved $row[0] value from the first query. Since that last "row" will be a boolean FALSE to signify that no more data is available $row[0] will actually be trying to de-reference that boolean FALSE as an array.
Since you're effectively only doing 2 select queries (or at least trying to), why not re-write as a single two-value joined query?
SELECT iduser, idaccount
FROM account
LEFT JOIN user ON user.iduser=account.idaccount
WHERE email='$Email';
I'm not sure what you're trying to do in your code exactly but that a look at this...
// create select statement to get all accounts where email=$Email from animator.account
$id_query = "SELECT idaccount FROM animator.account WHERE email = '$Email'";
echo $id_query."\n";
// run select statement for email=$mail
$select_results = mysqli_query($dbc, $id_query) or die("Error: ".mysqli_error($dbc));
// if we got some rows back from the database...
if ($select_results!==false)
{
$row_count = 0;
// loop through all results
while($row = mysqli_fetch_array($result))
{
$idaccount = $row[0];
echo "\n\n-- Row #$row_count --------------------------------------------\n";
echo $idaccount."\n";
// create insert statement for this idaccount
$insert_into_user = "INSERT INTO animator.user (idaccount) VALUES ('$idaccount')";
echo $insert_into_user."\n";
// run insert statement for this idaccount
$insert_results = mysqli_query($dbc, $insert_into_user) or die("Error: ".mysqli_error($dbc));
// if our insert statement worked...
if ($insert_results!==false)
{
// Returns the auto generated id used in the last query
$last_inisert_id = mysqli_insert_id($dbc);
echo $last_inisert_id."\n";
}
else
{
echo "insert statement did not work.\n";
}
$row_count++;
}
}
// we didn't get any rows back from the DB for email=$Email
else
{
echo "select query returned no results...? \n";
}

Categories