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.
Related
I'm not too good with PHP, but I know some other similar languages.
Now what I am trying to do is get how many rows are in a table. Here is my code.
$result = mysql_query("SELECT COUNT(*) FROM list");
$num_rows = mysql_num_rows($result);
echo $num_rows;
Now this code returns with 1, when there is actually 3. Why is that?
Thanks for any help.
Check This
$result = mysql_fetch_array(mysql_query("SELECT COUNT(*) as count FROM list"));
echo $result['count'];
Try like this:
$result = mysql_query("SELECT COUNT(*) FROM list");
$num_rows = mysql_fetch_object($result);
print_r($num_rows);
Note: Mysql_* functions are deprecated. Hence not recommended.
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT COUNT(*) as total_row FROM list");
echo "<table border='1'>
<tr>
<th>total_row</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['total_row'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
$result = mysql_query("SELECT COUNT(*) as total FROM list");
$num_rows = mysql_num_rows($result);
echo $num_rows; // output 1;
$rows = mysql_fetch_object($result);
echo $rows->total; // output 3;
You should do like this:
$result = mysql_query("SELECT * FROM list");
$num_rows = mysql_num_rows($result);
echo $num_rows;
If you want to get total number of row through COUNT, you can do like this:
$result = mysql_query("select count(*) FROM list");
$row = mysql_fetch_array($result);
echo $row[0];
It return number of rows count in single column, then only it return 1
Refer:
http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html
Only change your query
$result = mysql_query("SELECT * FROM list");
$num_rows = mysql_num_rows($result);
echo $num_rows;
It returns one row because, the query below returns only one value that is the number of rows.
$result = mysql_query("SELECT COUNT(*) FROM list");
I think this should help:
$result = mysql_query("SELECT COUNT(*) FROM list");
$num_rows = $result->fetchColumn();
echo $num_rows;
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.
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) ) {
Is this Query and display info all correct? Syntax-wise.
<?php
mysql_connect("HOST", "USERNAME", "PASSWORD") or die (mysql_error ());
mysql_select_db("DATABASENAME?!?!") or die(mysql_error());
$strSQL = "SELECT * FROM TABLENAME";
$result = mysql_query($strSQL) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo $row['COLUMNNAME'] . "<br />";
}
mysql_close()
?>
Use statement as
<?php
$conn=mysqli_connect("HOST", "USERNAME", "PASSWORD") or die (mysqli_error ());
mysqli_select_db("DATABASENAME",$conn) or die(mysqli_error());
$strSQL = "SELECT * FROM TABLENAME";
$result = mysqli_query($strSQL) or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
echo $row['COLUMNNAME'] . "<br />";
}
mysqli_close()
?>
Also I advice you to use Mysqli or Pdo instead of Mysql driver as it is deprecated.
There is no issue in your question or code. Syntax wise it is fine. But it is advised not to use mysql_* functions because it is deprecated.
The general way of using the code for your condition would be:
<?php
mysqli_connect("HOST", "USERNAME", "PASSWORD") or die (mysqli_error());
mysqli_select_db("DATABASENAME") or die(mysqli_error());
$strSQL = "SELECT * FROM TABLENAME";
$result = mysqli_query($strSQL) or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
foreach ($row as $column => $value)
echo $column . " = " . $value . "<br />";
echo "<br/>";
}
mysqli_close();
?>
So, this iterates and suppose say you have four columns, and two rows, it gives an output like:
Column 1 Name = Value
Column 2 Name = Value
Column 3 Name = Value
Column 4 Name = Value
Column 1 Name = Value
Column 2 Name = Value
Column 3 Name = Value
Column 4 Name = Value
i am tryng to select 5 mysql rows from the database and display them like $row[1] ect.... in php i am not sure how to do it an someone lead me down the right way please
Ok i hav looked a bit more
i wanted it to come out 1 - 5 and i wanted it to display the names
$result = mysql_query("SELECT * FROM table ORDER BY id DESC") or die (mysql_error());
while ($row = mysql_fetch_array($result)) {
$name = $row['name'];
$arr = array("somearray" => array(1 => 5, 2 => 9, 3 => 42, 4 => 42, 5 => 42));
echo $arr["somearray"][1];
echo $arr["somearray"][2];
echo $arr["somearray"][3];
echo $arr["somearray"][4];
echo $arr["somearray"][5];
}
I think the OP wants five ROWS, not COLUMNS. Here is the correct code, assuming you already have a mysql connection open:
$sql = 'SELECT *
FROM table_name
ORDER BY col_name
LIMIT 5';
$result = mysql_query($sql);
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
$row[] = $line;
}
// print (access individual rows: $row[0] ... $row[4])
var_dump($row);
From php.net :
http://us3.php.net/manual/en/function.mysql-fetch-row.php
<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
echo $row[0]; // 42
echo $row[1]; // the email value
?>
Try using MySQL's LIMIT clause to limit your results to 5 rows. And use PHP's mysql_fetch_assoc() (returns an associative array) instead of mysql_fetch_row() (returns a numerically indexed array). Also, it's good practice to free the memory associated with the result using mysql_free_result().
$result = mysql_query("SELECT * FROM mytable ORDER BY id DESC LIMIT 5") or die (mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$name = $row['name'];
echo $name;
}
mysql_free_result($result);
If you only need to select 5 rows from MySQL you can do this:
SELECT *
FROM Table
ORDER BY column
LIMIT 5