i want fetch all users data but am getting only one user details, please help me to solve
$conn = mysqli_connect("localhost", "root", "", "bitmining");
$sql6="SELECT username FROM users";
if($result = mysqli_query($conn, $sql6)){
while ($row=mysqli_fetch_array($result)){
//Hashrate Data Fetch
$investedusername = $row['username'];
$sql3="SELECT sum(hashrate_amount) as total FROM buyhashrate WHERE invested_username='$investedusername'";
$result = mysqli_query($conn, $sql3);
$row = mysqli_fetch_assoc($result);
//Total Value of Hashrate
echo $row['total'] . " GH/s";
echo "<br />";
}
$result->close();
}
Your re-using the $result field, change your second reference to something like ...
$result1 = mysqli_query($conn, $sql3);
$row = mysqli_fetch_assoc($result1);
This will stop it reseting the value your using for your main loop in
while ($row=mysqli_fetch_array($result)){
You use the $result variable for both mysqli_query
$result = mysqli_query($conn, $sql3);
While other answers are right about your mistake, I want to give you a better solution.
Try to use a join like this:
$conn = mysqli_connect("localhost", "root", "", "bitmining");
$sql="SELECT SUM(hashrate_amount) AS total FROM users AS t1 LEFT JOIN buyhashrate AS t2 ON (t1.username=t2.invested_username) GROUP BY t1.username";
if($result = mysqli_query($conn, $sql)){
while ($row=mysqli_fetch_array($result)){
//Total Value of Hashrate
echo $row['total'] . " GH/s";
echo "<br />";
}
$result->close();
}
This way you do just one query from database. But using your method you have n+1 queries which n is the number of users. So for one hundred users there is 101 queries.
Related
I have a simple mysql table with 'ratee' being the user and rating being the rating given to that user. I want to display how many times that user has been rated and what the average of those ratings are. The former works as shown in the code below but the latter does not. Where am i going wrong please? I am using PHP.
//working
$sql = "SELECT * FROM ratings WHERE ratee='" . $user1 . "'";
$result = mysqli_query($conn, $sql);
$ratingsqty = mysqli_num_rows($result);
echo $ratingsqty;
//not working 1
$sql = "SELECT * FROM ratings WHERE ratee='" . $user1 . "'";
$result = mysqli_query($conn, $sql);
$rating = mysqli_avg($result);
echo $rating;
//not working 2
$sql = "SELECT avg(rating) FROM ratings WHERE ratee='" . $user1 . "'";
$rating = mysqli_query($conn, $sql);
echo $rating;
Your problem is that you do not even bother to go through a basic mysqli tutorial or read mysqli documentation.
Not working 1: There is no such function as mysqli_avg(). Period.
Not working 2: mysqli_query() returns a mysqli_result object, not the average. You can use one of the fetch_*() methods of mysqli_result object to get the actual average calculated by MySQL:
See the code below:
$sql = "SELECT avg(rating) FROM ratings WHERE ratee='" . $user1 . "'"; //in real life code pls take some measures to prevent sql injection attacks
$result = mysqli_query($conn, $sql); //in real life code pls check for execution errors
$rating = $result->fetch_array(MYSQLI_NUM)
echo $rating[0];
mysqli_query() returns a Mysql results object. As such, you can't just echo it. You'll still need mysqli_fetch_row() or similar.
Try this
$sql = "SELECT * FROM ratings WHERE ratee='" . $user1 . "'";
$result = mysqli_query($conn, $sql);
$rating = mysqli_fetch_array($result,MYSQLI_ASSOC);
now $rating is an array by this you can print data of your table
using: echo $rating['ratee']; or echo $rating['rating'];
For average try this:
$sql = "SELECT avg(rating) as av FROM ratings WHERE ratee='" . $user1 . "'";
$result = mysqli_query($conn, $sql);
$rating = mysqli_fetch_array($result,MYSQLI_ASSOC);
echo $rating['av'];
Perhaps too late for you but maybe others will benefit:
Get one line result from MySQL:
$val = fetch_one_record("SELECT some_field FROM some_table LIMIT 1);
function fetch_one_record($sql) {
$conn = db_connect();
$result = array_shift($conn->query($sql)->fetch_assoc());
$conn->close();
return $result;
}
Very basic but as a rookie I am struggling. The echo doesnt show any value, just the text. What am I doing wrong?
Connect.php:
<?php
$connection = mysqli_connect('test.com.mysql', 'test_com_systems', 'systems');
if (!$connection){
die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, 'swaut_com_systems');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($connection));
}
?>
Get.php:
<?php
require('connect.php');
$query2 = "SELECT systemid FROM user WHERE username=test";
$result2 = mysqli_query($connection, $query2);
echo ( 'SystemID: '.$result2);
?>
Assuming you have connected to the database successfully then the query is incorrect. You must wrap all text values in quotes like this
<?php
require('connect.php');
$query2 = "SELECT systemid FROM user WHERE username='test'";
$result2 = mysqli_query($connection, $query2);
Now the mysqli_query submits the query to the database where it is run and a result set built. To see the result set you need to read the result set back from the database using one of the fetch functions for example
$row = mysqli_fetch_assoc($result2);
echo 'SystemID: ' . $row['systemid'];
If there are more than one rows in the result set you must do that in a loop like this
while ($row = mysqli_fetch_assoc($result2)){
echo 'SystemID: ' . $row['systemid'];
}
You are printing the mysqli result object. In order to printthe result you have to use:
$row = mysqli_fetch_assoc($result2);
print_r($row);
You need to collect the results of the mysqli_query using the following:
require('connect.php');
$query2 = "SELECT systemid FROM user WHERE username=test";
$result2 = mysqli_query($connection, $query2);
while ($row = mysqli_fetch_assoc($result2))
{
echo "System ID is: " . $row['systemid'];
}
Hi I have had a long search for this but have found no fix.
My code is as follows:
$link = mysqli_connect("localhost",".........","...........",".........") or die("Error " . mysqli_error($link));
$ctime = time();
$check = "SELECT * FROM thread WHERE forumid='48' AND visible='1' ORDER BY lastpost DESC LIMIT 1" or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$rc = mysqli_query($link, $check);
while($rows = $rc->fetch_assoc()){
$pid = $rows['firstpostid'];
$query = "SELECT * FROM dropouts WHERE date <= $ctime" or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$result = mysqli_query($link, $query);
$row_cnt = $result->num_rows;
while($row = $result->fetch_array())
{
$date = $row['date'];
$user = $row['username'];
$sql = "DELETE FROM dropouts WHERE date = $date" or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$done = mysqli_query($link, $sql);
//////////////////////////////////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
$check1 = "SELECT * FROM post WHERE postid='$pid'" or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$rc1 = mysqli_query($link, $check1);
while($row1 = $rc1->fetch_array())
{
$text = $row1['pagetext'];
echo str_replace($user, "", $text);
}
}
}
The problem is that when I run it like that I get no out put.
If I run it so that the while loops are not in eachother I get output but the script only does it for one row post/row.
Does anyone know how to fix this?
I read that it should work but this just isn't working...
Thanks
Have you tried echoing it. It can be that the query returns 0 or empty!
As a beginner in php, you may stumble into these kinds of issues. The best way to understand the problem would be to ECHO & EXIT. Check each step by doing this and you can better understand the problem.
In the problem above, instead of writing the entire code, first of all try checking the small portion of codes and remember:
The best compiler/interpreter lies between you two ears!!
Link to Mysql Table
I'm trying to have a basic website display info from a table to use as content. I run this
$result = mysqli_query($conn,"SELECT cont_id, cont_text FROM content") or die("Error in the consult.." . mysqli_error($conn));
$row = mysqli_fetch_array($result);
Each page_location is a page on the website. I would like to do something like
<?php echo $row['cont_text'] WHERE cont_id = 15; ?>
I know that's not right but then down the page I would echo cont_text from a new cont_id. How would I go about this?
Do I need to reorganize my table? Or multiple mysqli_query's?
You're over-thinking it. Just run one query using cont_id in the where clause of the query:
$result = mysqli_query($conn,"SELECT cont_text FROM content WHERE cont_id = 15") or die("Error in the consult.." . mysqli_error($conn));
$row = mysqli_fetch_array($result);
echo $row['cont_text'];
edit
Based on your comments try this:
$result = mysqli_query($conn,"SELECT cont_id , cont_text FROM content") or die("Error in the consult.." . mysqli_error($conn));
$content = array();
while($row = mysqli_fetch_array($result)) {
$content[$row['cont_id']] = $row['cont_text'];
}
echo $content[15];
I am fetching rows from a mysql table (jobs). Inside of that fetch, I am also fetching from another table (accounts) [to receive account api keys all depending on what ID_ASSOC is attacted to the job]: below is the code
$sql = "SELECT * FROM jobs";
$query = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_assoc($query)){
echo $row['action'];
echo "<br/>";
$job_poster_id = $row['id_assoc'];
$sql = "SELECT * FROM accounts WHERE id_assoc='$job_poster_id'";
$query = mysqli_query($db_conx, $sql);
while($rows = mysqli_fetch_assoc($query)){
$username = $rows['twitter_username'];
$consumer_key = $rows['consumer_key'];
$consumer_secret = $rows['consumer_secret'];
$access_token = $rows['access_token'];
$access_token_secret = $rows['access_token_secret'];
}
echo $job_poster_id ;
echo "<br/>";
echo $twitter_username;
echo "<br/>";
echo "----------------------------------";
echo "<br/>";
}
OUTPUT:
specific-message
4
admin
----------------------------------
When I do this, I only get one row output..and I can't seem to find out why. I want the above out put to repeat as many times as it has rows, and it's only doing one row (with the account fetch in the code). However when I do it without the internal fetch (accounts fetch), it returns multiple rows just as desired. Why is this? (below is sample code WITHOUT the accounts fetch):
$sql = "SELECT * FROM jobs";
$query = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_assoc($query)){
echo $row['action'];
echo "<br/>";
$job_poster_id = $row['id_assoc'];
echo $job_poster_id ;
echo "<br/>";
echo "----------------------------------";
echo "<br/>";
}
OUTPUT:
specific-message
4
----------------------------------
specific-message
1
----------------------------------
specific-message
2
----------------------------------
$query = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_assoc($query)){
echo $row['action'];
echo "<br/>";
$job_poster_id = $row['id_assoc'];
$sql = "SELECT * FROM accounts WHERE id_assoc='$job_poster_id'";
$query = mysqli_query($db_conx, $sql);
The problem is that you're using $query for the inner and the outer query.
When the inner query runs, and it steps through the loop, it's iterating to the end of the result set; when the outer while loop runs, mysqli_fetch_assoc($query) is returning false, because you're already at the end of the result set - just not the result set you were expecting.
You can fix this by renaming one of the $query variables.