I am currently building a leaderboard (https://mgo.io/mgo3/leaderboard.php) and I'm having issues making it manually accept data. I want it to grab data from my MySQL database, and allow me to manually go in and enter data which it will in turn display as well. It currently grabs id, and clan names from the clans db.
How do I get it to read data I input from two new 'wins' and 'losses' columns I add in the db it already grabs data from?
Heres my code:
<?php
include_once "Log.class.php";
include_once "dbcon.php";
function getLeaders() {
global $dbh;
date_default_timezone_set('UTC');
$res = array();
$stmt = $dbh->prepare("SELECT id, name FROM clans ORDER BY id ASC");
$stmt->execute();
while ($row = $stmt->fetch()) {
$clan_id = (int) $row['id'];
$clan_name = $row['name'];
$res[$clan_id] = array();
$res[$clan_id]['name'] = $clan_name;
$res[$clan_id]['rank'] = 0;
$res[$clan_id]['cp'] = 0;
$res[$clan_id]['win'] = 0;
$res[$clan_id]['loss'] = 0;
?>
Just add the fields to your SELECT statement and assign the retrieved values to the appropriate variables:
// Add wins & losses here:
$stmt = $dbh->prepare("SELECT id, name, wins, losses
FROM clans ORDER BY id ASC");
$stmt->execute();
while ($row = $stmt->fetch()) {
$clan_id = (int) $row['id'];
$clan_name = $row['name'];
$res[$clan_id] = array();
$res[$clan_id]['name'] = $clan_name;
$res[$clan_id]['rank'] = 0;
$res[$clan_id]['cp'] = 0;
$res[$clan_id]['win'] = $row['wins']; // <<<< and here...
$res[$clan_id]['loss'] = $row['losses']; // <<<<
Related
My output is only id column, reference to "combustibili" table. How to get value reference from combustibili?
$combustibili = "SELECT * FROM tabelint";
$rezcom = $conectare->query($combustibili);
$p = new GNUPlot();
while($row = $rezcom->fetch_assoc()) {
if($row["ID"] == 1){
$data = new PGData($row["ID_BENZINA_STANDARD"]);
$data2 = new PGData($row["ID_MOTORINA_STANDARD"]);
$data3 = new PGData($row["ID_BENZINA_SUPERIOARA"]);
$data4 = new PGData($row["ID_MOTORINA_SUPERIOARA"]);
}
}
From information provided assume COMBUSTIBILItable structure should be something like
COMBUSTIBILI(id pk and fk_in_tabelint number, name varchar, price float)
TABELINT(id pk number, ID_BENZINA_STANDARD fk->COMBUSTIBILI number ... etc)
so
SELECT * FROM tabelint where id=1 will retun something like
(id #1, ID_BENZINA_STANDARD #2 ... etc)
and just need to query COMBUSTIBILI with fks
$combustibili = "SELECT * FROM tabelint";
$rezcom = $conectare->query($combustibili);
$p = new GNUPlot();
//assume also record for id=1 exists
while($row = $rezcom->fetch_assoc()) {
if($row["ID"] == 1){
$data = new PGData($row["ID_BENZINA_STANDARD"]);
$data2 = new PGData($row["ID_MOTORINA_STANDARD"]);
$data3 = new PGData($row["ID_BENZINA_SUPERIOARA"]);
$data4 = new PGData($row["ID_MOTORINA_SUPERIOARA"]);
}
}
//add for each id_s
//BENZINA_STANDARD :: should be 1 row if db is consistent
//not sure how PGData class is defined and how to retrive only id : maybe $pgd->id ...
//$data = $row["ID_BENZINA_STANDARD"] : this is what is needed
$sql = "SELECT * FROM COMBUSTIBILI where id=".$data;
$sqlout = $conectare->query($sql);
//just parse and retrive what ever wanted
while($row = $sqlout->fetch_assoc()) { ... etc ... }
//same with data2_3_4
I'm trying to make an ad system with PHP pdo. How can I get result id's of the following SQL query into my PHP variables?
SELECT id FROM ads, ( SELECT id AS sid FROM ads WHERE position="A" ORDER BY RAND( ) LIMIT 5 ) tmp WHERE ads.id = tmp.sid
I just don't know how to get those values in the variables like this:
$ad_1 = result[1]
$ad_2 = result[2]
$ad_3 = result[3]
$ad_4 = result[4]
$ad_5 = result[5]
With the help of those variables ($ad_1...5) I could display unique and random ads in different places, get correct data, and update ad clicks/views...
Best to get into the habit of using prepared statements like this:
<?php
$adIds = array();
$position = 'A'; // probably set dynamicaly
$stmt = $dbh->prepare("SELECT id FROM ads, ( SELECT id AS sid FROM ads WHERE position=? ORDER BY RAND( ) LIMIT 5 ) tmp WHERE ads.id = tmp.sid");
$stmt->bindParam(1, $position);
if ($stmt->execute()) {
while ($row = $stmt->fetch()) {
$adIds[] = $row[0];
}
}
?>
$adIds is now an array containing all the ids returned from the select statement.
try this :
$begin = "ad_";
for($i = 0; $i < 6 ; $i++) {
$($begin.$i) = $result[$i];
}
(assuming you array $result is previously defined)
Here's a simplified code similar to what I'm using. In this one, I'm pulling Names from ID's.
$counter = 0;
$select = "SELECT nID,nName WHERE nID = $counter";
$result = sqlsrv_query($connection, $select);
$maxusers = 10;
while($counter<$maxusers) {
while($row = sqlsrv_fetch_array($result)) {
echo $row['nName'];
}
$counter++
}
What I get is the same name, the counter in the select statement stays at 0.
I had to put the definition of the $select statement and the $result inside the loop, it redefines everything every time we enter the while loop, looks like the code below. That doesn't seem practical and optimal to me. What are the best work-around for situations like these? I'm not really familiar with variable scopes in PHP, I haven't found any good documentation on that matter when it comes to sql functions.
$counter = 0;
$maxusers = 10;
while($counter<$maxusers) {
$select = "SELECT nID,nName WHERE nID = $counter";
$result = sqlsrv_query($connection, $select);
while($row = sqlsrv_fetch_array($result)) {
echo $row['nName'];
}
$counter++
}
Here's the code that I've actually written.
$selectFirst = "SELECT TOP 1 nDateTime,nUserID FROM TB_EVENT_LOG WHERE nUserID = $usercounter AND nDateTime BETWEEN $today AND $tomorrow";
$selectLast = "SELECT TOP 1 nDateTime,nUserID FROM TB_EVENT_LOG WHERE nUserID = $usercounter DateTime BETWEEN $today AND $tomorrow DESC";
$resultFirst = sqlsrv_query($bscon, $selectFirst);
$resultLast = sqlsrv_query($bscon, $selectLast);
$selectnumberofUsers = "SELECT TOP 1 nUserIdn FROM TB_USER ORDER by nUserIdn DESC";
$usersmaxq = sqlsrv_query($bscon, $selectnumberofUsers);
$usersmax = sqlsrv_fetch_object($usersmaxq)->nUserIdn;
while($usercounter<$usersmax){
$usercounter = $usercounter + 1;
while($rowfirst = sqlsrv_fetch_array($resultFirst)) {
$intime = $rowfirst['nDateTime'];
}
echo $intime." ".$usercounter."<br />";
}
Your issue doesn't have to do with variable scope. The $select variable is set once as string with the current value of $counter. Your second example works because this value is reset every time.
In your second example however, you're creating a sql statement that gets 1 row (assuming nID is unique), then looping through your result retrieve that one row. You're doing 10 sql calls, but you only need one if you modify your query like so:
$minusers = 0;
$maxusers = 10;
$select = "SELECT nID,nName WHERE nID >= $minusers AND nID < $maxusers ORDER BY nID";
$result = sqlsrv_query($connection, $select);
while($row = sqlsrv_fetch_array($result)) {
echo $row['nName'];
}
For your actual code, you should be able to get one record per nUserId by using GROUP BY. Try this:
$selectFirst = "SELECT nDateTime,nUserID FROM TB_EVENT_LOG WHERE nUserID >= $usersmin AND nUserID <= $usersmax AND nDateTime BETWEEN $today AND $tomorrow GROUP BY nUserID";
I am using mysqli to fetch data from the database and put it into an array with a while loop. When i echo out the array i get an empty array however in a function i previously did this code worked but it had a different result from the database. I know that the database is giving out good data because when i echo out the result $idGroup it gives me 2 which is correct.
Ps i know it will keep replacing itself because i don't specify an index
private function Groups()
{
$functionRun = 0;
$i = 0;
$helperArray = array();
$this->grouplist = array();
$query = "SELECT GroupName, Groups.idGroup
FROM Groups
INNER JOIN Members
ON Groups.idGroup = Members.idGroup
WHERE Members.idMember = ? ";
//prepare query and execute
if($stmt = $this->connection->prepare($query))
{
$stmt->bind_param('s', $this->id);
$stmt->execute();
$stmt->bind_result($GroupName, $idGroup);
while ($stmt->fetch())
{
$helperArray[] = $idGroup;
}
echo $helperArray;
}
Use print_r when dealing with arrays. Use echo on strings.
Try this
$query = "SELECT GroupName, Groups.idGroup
FROM Groups
INNER JOIN Members
ON Groups.idGroup = Members.idGroup
WHERE Members.idMember = ? ";
//prepare query and execute
if($stmt = $this->connection->prepare($query))
{
$stmt->bind_param('s', $this->id);
$stmt->execute();
$stmt->bind_result($GroupName, $idGroup);
$helperArray =array();
while ($stmt->fetch())
{
$helperArray[] = $idGroup;
}
print_r($helperArray);
}
Assuming I have a uniqid key in my table and that same key is sent to my site in a get method, how do I pull that specific key out and assign all the data from the table to variables. This is what I have so far but cant seem to figure it out.
$query1 = "SELECT *
FROM todo_item2 as ti INNER JOIN todo_category2 as tc ON ti.todo_id = tc.todo_id'
WHERE todo_id = :todo_id";
$statement1 = $db->prepare($query1);
$statement1 -> execute(array(
'todo_id' =>$id
));
while ($row = $statement1->fetch())
{
$text = $row['todo'];
$cat = $row['category'];
$percent = $row['precent'];
$date = $row['due_date'];
}
You should ready about what execute actually does .. the parameters to execute (and I assume you're using PDO or something similar here) are the tokens of the query. What you want is something like:
$query = " ... WHERE todo_id = ?"
$stmt = $db->prepare($query);
$stmt->execute(array($id));
while ($row = $stmt->fetch()) {
//$row is now an associative array of row values.
}
// Start the Load
$query1 = "SELECT *
FROM todo_item2 as ti INNER JOIN todo_category2 as tc ON ti.todo_id = tc.todo_id
WHERE ti.todo_id = :todo_id";
$statement1 = $db->prepare($query1);
$statement1 -> execute(array(
'todo_id' =>$id
));
// Make Sure the Data Exists
if( $statement1->rowCount() == 0 )
{
die('Please Enter a Valid ID Tag - (id)');
}
while($row = $statement1->fetch())
{
$text = $row['todo'];
$cat = $row['category'];
$percent = $row['percent'];
$date = $row['due_date'];
}