I know that $wins should be 3. Because I have 3 rows with the integer "1" in the "win" column on table "rated_teams" But for some reason this code will not work. Can you find the problem please? ALSO, I'm aware that some of this is depracted. I'll update the whole page, once I get it at least in working condition.
<?php
$sql = "SELECT SUM(win) FROM rated_teams WHERE server='$server' AND name='$myteam'";
$query = mysql_query($sql, $con)
or die('A error occured: ' . mysql_error());
while ((mysql_fetch_array($query))) {
$wins = $row['SUM(win)'];
}
?>
<h3>Total Wins: <?php echo $wins?> </h3>
Try with
$sql = "SELECT SUM(win) as sum FROM rated_teams WHERE server='$server' AND name='$myteam'";
and while you are getting give like
while ($row = mysql_fetch_array($query)) {
$wins = $row['sum'];
}
And my advice is try to avoid mysql_* functions due to they are deprecated.Instead use mysqli_* functions or PDO statements.
You do not set the $row variable. Edit your while to this.
while ($row = mysql_fetch_array($query))
You need to give your calculated column an alias. Try this:
<?php
$sql = "SELECT SUM(win) as sumwin FROM rated_teams WHERE server='$server' AND name='$myteam'";
$query = mysql_query($sql, $con) or die('A error occured: ' . mysql_error());
while ($row = mysql_fetch_array($query)) {
$wins = $row['sumwin'];
}
?>
<h3>Total Wins: <?php echo $wins?> </h3>
Please write sql query right manner.Write like this.
$sql = "SELECT SUM(win) as sumwin FROM rated_teams WHERE server='".$server."' AND name='".$myteam."'";
while ((mysql_fetch_array($query))) {
should be
while ($row = mysql_fetch_array($query) ) {
Related
I'm trying to save an id number in my database and I can't seem to store an integer inside the $resultid variable.
Here is my code:
<?php
if (isset($_POST['tittel'])) {
$meny = $_POST['meny'];
$tittel = $_POST['tittel'];
$innhold = $_POST['innhold'];
$con = mysqli_connect('localhost', 'root', '', 'vikerfjell');
if ($con) {
echo "Connected!";
"<br>";
} else {
die("Connection failed!");
}
$menyid = ("SELECT idmeny FROM meny WHERE tekst = '$meny'");
$resultid = mysqli_query($con, $menyid);
$resultarr = mysqli_fetch_assoc($resultid);
$query = "INSERT INTO innhold (tittel, tekst, idmeny) ";
$query. = "VALUES('$tittel', '$innhold', $resultarr)";
$result = mysqli_query($con, $query);
"<br>";
if (!$result) {
die('Query FAILED!'.mysqli_error($con));
}
}
mysqli_fetch_assoc- fetch a result row as an associative array, u need to select the key of the id u are tying to store
$menyid = ("SELECT idmeny FROM meny WHERE tekst = '$meny'");
$resultid = mysqli_query($con, $menyid);
$resultarr = mysqli_fetch_assoc($resultid);
$column_id=$resultarr['id_stored_on_db'];
$query = "INSERT INTO innhold (tittel, tekst, idmeny) ";
$query .="VALUES('$tittel', '$innhold', $column_id)";
Guessing $resultarr holds multiple idmeny, so you really have to loop through each idmeny and create multiple insert statements, as such:
$menyid = "SELECT idmeny FROM meny WHERE tekst = '$meny'";
$result = mysqli_query($con, $menyid);
while ($row = $result->fetch_assoc()) {
$query = "INSERT INTO innhold (tittel, tekst, idmeny) ";
$query .= "VALUES('$tittel', '$innhold', {$row['idmeny']})";
$result = mysqli_query($con, $query);
}
Found the problem. I missed a value in my dropdown code (another php file). Thanks anyway : )
i am trying to echo the result of the SQL SELECT COUNT but it seems doesn't work for me, can anyone of you help me?
The error show that
mysql_fetch_array expects parameter 1 to be resources, boolean given
....
This is my sql
<?php
$query = ("SELECT COUNT(*) AS total FROM `errors` WHERE `sta` = 0");
$result = mysql_query($query);
$count = mysql_fetch_array($result);
echo $count['total'];
?>
Try to following code:
$query = ($con, "SELECT COUNT(*) AS total FROM errors WHERE sta = 0");
$count = mysqli_fetch_assoc($query);
echo $count['total'];
Hope will help you
You would use mysql_result() like shown below to retrieve the output as an int.
$query = mysql_query("SELECT COUNT(*) FROM `errors` WHERE `sta` = 0");
$result = mysql_result($query, 0, 0);
echo $result;
mysql_* extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0. Instead, either the mysqli or PDO_MySQL
extension should be used.
Instead of doing this, you can change your query and directly use mysql_num_rows as it also gives the count. And I guess you only need the count so it is better way, and here is the code :
<?php
$query = ("SELECT * FROM `errors` WHERE `sta` = 0");
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$count = mysql_num_rows($result);
echo $count; // this will give the total count
?>
// try this code...
$query = "SELECT * FROM `errors` WHERE `sta`='0'";
$result = mysql_query($query) or die (mysql_error());
$row2 = mysql_fetch_array($result);
$count= mysql_num_rows($result);
echo $count;
I want to display 24 unique id's but he is only giving me 1:
This is my php
<?php $sql = "SELECT id
FROM 15players
ORDER BY RAND()
LIMIT 24"; $result = mysql_query($sql) or die('Query failed: ' . mysql_error());
$row = mysql_fetch_array($result);
echo $row['id']; ?>
But if I execute this SQL in phpmyadmin,
It returns me 24 id's
So what am I doing wrong?
EDIT:
Got the right answer!
<?php $sql = "SELECT id
FROM 15players
ORDER BY RAND()
LIMIT 0,24";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
while($row = mysql_fetch_array($result)) {
echo $row['id'];
}
?>
mysql_fetch_array only returns 1 row you need to loop through the results.
while($row = mysql_fetch_array($result)) {
echo $row['id'];
}
If multiple results are fetched via a query, you need to iterate over it.
<?php
$sql = "SELECT id
FROM 15players
ORDER BY RAND()
LIMIT 24"; $result = mysql_query($sql) or die('Query failed: ' . mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['id'];
}
?>
Also mysql_* is deprecated, start using PDO.
mysql_fetch_array only get one row at a time. You need to loop through all of your results.
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo $row['id']. '<br>';
}
You should be using mysqli or PDO. The mysql_ is deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0.
I can't count my users in the database with my current configurations.
<?php
require_once 'scripts/app_config.php';
mysqli_connect(DATABASE_HOST,DATABASE_USER,DATABASE_PASS,DATABASE_NAME);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT COUNT(*) FROM users";
$result = mysql_query($query);
$num = mysql_num_rows($result);
echo "There are: ". $num . " users in the database";
mysqli_close($con);
?>
Your problem is this:
$num = mysql_num_rows($result);
Your query only returns 1 row, so I presume the problem you're having is that it always outputs: There are 1 users in the database.
You should instead use:
$num = mysqli_fetch_array($query)[0];
Either use COUNT(*) OR use mysql_num_rows
$query = "SELECT COUNT(*) FROM users";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$num = $row[0];
echo "There are: ". $num . " users in the database";
Note : mysql_* function is deprecated, move on mysqli_* function asap.
In my database I have a field called spaj_per as an auto increment primary key.
How can I display the lastest inserted value in the field spaj_per. I tried this.
<?php
$con = mysql_connect("localhost", "SuperAdmin", "***");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("sistem_pengurusan_fail",$con);
$q = "SELECT MAX(id) AS spaj_per FROM unit_pengambilan";
$result = mysql_query($q);
$row = mysql_fetch_array($result);
?>
<?php echo $row['spaj_per']; ?>
But it won't work.
You may use
SELECT id FROM mytable ORDER BY id DESC LIMIT 1;
or like
$last_id = mysql_insert_id();
Try this,
$q = "SELECT MAX(spaj_per) AS spaj_per FROM unit_pengambilan";
$result =mysql_query($q);
$row = mysql_fetch_row($result);
echo $row[0];
Use PHP's mysql_insert_id() function
Try the following
<?
$q = "SELECT MAX(spaj_per) AS last_spaj_per FROM unit_pengambilan";
$result =mysql_query($q);
$row = mysql_fetch_array($result);
echo $row['last_spaj_per'];
?>
$row = mysql_fetch_array($result); Change to $row = mysql_fetch_assoc($result); and try
Try with mysql_insert_id
For more reference use following link
http://php.net/manual/en/function.mysql-insert-id.php