am new to PHP. my question is i have a table with values and i want to sum the values of one of the column but its not working pls i would appreciate if you can help me with this
mysql_select_db($database_ebsConn, $ebsConn);
$transact4 = "SELECT sum(credit) FROM account WHERE integral_no ='$intergra'";
$transact5 = mysql_query($transact4, $ebsConn) or die(mysql_error());
$transact6 = mysql_fetch_assoc($transact5);
pls i am waiting for your reply
try to go with prepared Statements.... at least use separate database file
$sqlSEND=mysql_query("SELECT sum(credit) FROM account WHERE integral_no ='$intergra'");
while($result = mysql_fetch_assoc($sqlSEND)){
echo $result['sum(credit)'];
}
Related
In the Exam1 table, ID, examname, and exam_id values are added fine. But when I try to use the UPDATE query to update the points to another table (Question), it does not work.
for($i = 1; $i<$arraysize; $i++){ //For every question
$questionid = $array[$i]['questionid'];
$points = $array[$i]['points'];
$ID = $ID+1;
$queue ="INSERT INTO Exam1 (ID, examname, exam_id) VALUES
('$questionid','$examname', '$exam_id')";
$result = mysqli_query($connection,$queue);
$queue1 ="INSERT INTO points (ID, points) VALUES
( '$ID' , '$points')";
$result1 = mysqli_query($connection,$queue1);
$sql = "UPDATE Question SET points='$points' where ID ='$questionid'";
$result1 = mysqli_query($connection,$sql);
}
$myquery = "UPDATE Question SET points='$points' WHERE ID ='" . $questionid . "'";
$resultset = mysqli_query($connection, $myquery);
Try this please
First of all be careful with int types and putting values for it in UPDATEs and INSERTs into a quote: There is type cast happening, and it may come to results, which you may not expect.
Second, I still have big trouble to understand your data model of your database: You are joining the Exam1.ID over with the Question.ID. Both appear to be the surrogates for the entity to me. Otherwise I would not understand what the meaning of the column Exam_ID (most likely coming from table Question in your case) should be. It therefore looks to me that the WHERE clause of your UPDATE statement is incomplete (did you also mean to restrict on EXAM_ID? Otherwise you might be setting all points to 20 for all questions irrespectively of the exam...?)
If I got you wrong, please provide a detailed overview about your database schema setup including the primary keys of at least the following tables:
Exam1
points
Question
to allow us further help you (and me adjusting the answer here). Also giving us some insight how you think the foreign key relationship is between the tables might help us to help you.
I have a MySQL database that holds details on various video games and user accounts. A user is able to review a game and the score is put into a review table.
I am trying to retrieve the average score for each game and have it displayed on a webpage. My SQL statement within PHP looks like this
$sqlOverall = "SELECT CAST(AVG('scoreOverall') AS DECIMAL('10,1'))
FROM 'ratings'
WHERE gameID = '$id'";
$scoreOverall = mysqli_query($conn, $sqlOverall);
But when I try to echo $scoreOverall it returns blank.
SELECT CAST(AVG(scoreOverall) AS DECIMAL(10,1))
FROM ratings
WHERE gameID = '$id';
Try above code.
Hope this will helps.
try this below query
$selquery="SELECT CAST(AVG(scoreOverall) AS DECIMAL(10,1)) FROM ratings WHERE gameID='$id'";
$result= mysqli_query($conn, $selquery);
It works fine. Hope it is your helpful
$sqlOverall="SELECT CAST(AVG(scoreOverall) AS DECIMAL(10,1)) FROM ratings WHERE gameID=$id";
$scoreOverall = mysqli_query($conn, $sqlOverall);
foreach ($scoreOverall as $value) {
$scoreOverall = $value['CAST(AVG(queue_status) AS DECIMAL(10,1))'];
echo $scoreOverall;
}
I have looked all over and at tons of code and examples.. This is such a small bit of code but I just can't seem to get it to work.
I have dbo.accounts which contains the id, username, password, createtime..
I have a simple form, you type in the username, and I need the select query to return the ID based on the username.
$result = mssql_query('SELECT id FROM dbo.account WHERE name = $username');
The dbo.gamemoney table will just insert some hardcoded info such as an amount of coins for the game..
My problem is that if I use a query as ID = 123, it works, but when I try to grab the id of dbo.accounts by using the username, I get nothing back.
I know it has to be something small, But I have tried to figure it out for so many hours now that I'm honestly lost..
Thanks for your time,
Chris
Since, $username is string type, you have to enclose it in quotes.
$result = mssql_query("SELECT id FROM dbo.account WHERE name = '$username'");
As a better practice would suggest use a try-catch scenario so that you get the exact error log. Try -
$result = mssql_query('SELECT id FROM dbo.account WHERE name = "'.$username.'"') or die('MSSQL error: ' . mssql_get_last_message());
Thanks everyone for the help!
I was able to get it working. Now I'll make sure it's the right way. I had forgot to add,
while($row = mssql_fetch_array($result)) {
$id = $row['id'];
$ip = $row['ip'];
}
Thats why the id was blank. I was missing some code.
Chris
I need someone to help me with this problem. My head doesn't want to think straight today.
So, i have a table named "recensions", and another named "comments". In each table, i have a column named "amne_id". This would make so i could connect the comments to the correct recension.
Now, on my first page, i simply get all the recensions with the code:
$rec = $this->db->get('recensions');
What i want is to count how many comments each recension has. But i have no idea how. I guess i maybe should use JOIN and num_rows()?
Please ask if you dont understand, so i can explain better.
Have a nice day!
$this->db->select('COUNT(*) as count, recensions.anme_id as recension_id')
->from('recensions')
->join('comments','recensions.anme_id = comments.anme_id','left')
->group_by('anme_id');
$result = $this->db->get();
Should give you the recensions id and the comment count for that id.
then loop:
foreach($result->result() as $row){
echo "Recension id $row->recension_id has $row->count comments<br />";
}
Like this??
$sql = mysql_query("SELECT * FROM recensions");
while($row = mysql_fetch_array($sql)){
$amne_id = $row{"amne_id"};
$sql2 = mysql_query("SELECT * FROM comments WHERE amne_id='$amne_id'");
$total_comments = mysql_num_rows($sql2);
echo $total_comments;
}
I don't know about the database connector you are using, but in pure SQL (assuming the connection recensions.id=comments.anme_id connection), try this:
SELECT COUNT(comments.id), anme_id
FROM recensions
LEFT JOIN comments on comments.anme_id=recensions.id
GROUP BY anme_id
This is the SQL statement I have written.
$result_array
= mysql_query("SELECT * FROM pl_fixtures WHERE id = pl_divisions.id", $con1);
It is written to compare values of dbfix.id with dbdiv.id. Can someone please tell me what is my error in comparing these two "id"s? dbfix and dbdiv are two different tables in a same database. Both have id as primary keys. I am using PHP 5.
Any help will be appreciated. Thank you :)
Regards,
BG
You aren't including the pl_divisions table in your select. Try the following:
$result_array = mysql_query("SELECT * FROM pl_fixtures, pl_divisions WHERE pl_fixtures.id = pl_divisions.id", $con1);