This question already has answers here:
Get table names using SELECT statement in MySQL
(14 answers)
Closed 2 years ago.
Im trying to echo out all table names in a specific database, but I can't find a working solution! Here is the code I have to far:
$sql = "SHOW TABLES FROM test";
$result1 = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result1);
if ($resultCheck > 0){
while ($row = mysqli_fetch_assoc($result1)){
echo $row;
}
}
Any help would be great!
The $row is coming as an array, so you have to fetch it as
echo $row['Tables_in_test']
First of all, be sure not to ask for a database outside of what the user you are using is able to see (Therefore, make sure that the user can see other databases).
One that is figured out, this is the code
$sql = "SHOW TABLES FROM test";
$result1 = $conn->query($sql);
while ($row = mysqli_fetch_assoc($result1)){
echo $row["Tables_in_test"];
}
Related
This question already has answers here:
Get sum of MySQL column in PHP
(9 answers)
Closed 1 year ago.
I'm not sure if I'm overthinking this and being stupid but I'm trying to add all the values from my MySQL result.
Here is my code to fetch the values and store them in an array:
$query = "SELECT value FROM table";
$result = mysqli_query($dbcon, $query);
$array = array();
if(mysqli_num_rows($result) > 0 ){
while($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
}
print_r($array);
Now I just need to figure out how I can add these values together and use this array for other things like working out the average. If there is an easier way to do this than storing the results in an array then please let me know.
Any help is much appreciated.
Managed to figure this out myself, I used the below code:
$query = "SELECT SUM(column) AS value_sum FROM table";
$result = mysqli_query($dbcon, $query);
$row = mysqli_fetch_assoc($result);
$sum = $row['value_sum'];
I guess I was overthinking it.
This question already has answers here:
How to get count of rows in MySQL table using PHP?
(3 answers)
Closed 2 years ago.
How do I display this query result:
from MySQL onto a webpage, just like this:
?
I want to display the count only. I did run the same query through PHP but its returning '2'. The PHP code is below
<?php
//Connection for database
$conn = mysqli_connect("localhost", "root", "aaaaa", "db");
$query = "SELECT `gender`,COUNT(`gender`) FROM `reg` GROUP BY `gender`";
$result = mysqli_query($conn, $query);
if ($result)
{
// it return number of rows in the table.
$row = mysqli_num_rows($result);
if ($row)
{
printf("" . $row);
}
// close the result.
mysqli_free_result($result);
}
// Connection close
mysqli_close($conn);
?>
Please note that I have gone through other answers like this one but I can't really find my way through, I want the value count only not the total number of all rows in the table, .e.g. count for 'Male'.
You're using mysqli_num_rows(), which returns the number of rows in the result, not the actual data in the result. For that you need to use mysqli_fetch_assoc().
Your could would become:
$query = "SELECT `gender`,
COUNT(`gender`) AS `count`
FROM `reg`
GROUP BY `gender`";
$result = mysqli_query($conn, $query);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$gender = $row['gender'];
$count = $row['count'];
echo "$gender = $count<br>";
}
mysqli_free_result($result);
}
Note that I slightly changed your query to make the count accessible.
This question already has answers here:
Get sum of MySQL column in PHP
(9 answers)
Closed 1 year ago.
I'm trying to get the sum off all the prize money from a column in a MySql table, but I'm not getting a result.
$result = mysqli_query("SELECT SUM(prize_money) FROM cards");
while ($rows = mysqli_fetch_array($result)) {
echo $rows['SUM(prize_money)'];
}
I just want to add all of the numbers in the prize_money column then echo the results.
Thank You
You should apply an alias to the SUM so it is easier to access in the PHP.
You then need to pass the connection string to the mysqli_query function as the first parameter.
So for example if your database connection were:
$con=mysqli_connect("localhost","my_user","my_password","my_db");
then you'd use this code to execute the query and assign the alias:
$result = mysqli_query($con, 'SELECT SUM(prize_money) AS sum_prize_money FROM cards');
$row = mysqli_fetch_assoc($result);
$sum = $row['sum_prize_money'];
echo $sum;
Do you get a result if you do this:
$result = mysqli_query("SELECT SUM(prize_money) FROM cards");
$rows = mysqli_fetch_array($result);
echo $rows
As good practice, you should learn to aliase your sql variables e.g. SUM(prize_money) AS total etc
This question already has answers here:
MySQL skipping first row
(2 answers)
Closed 5 years ago.
Even though it works correctly when I query it in phpMyAdmin SQL tab. What I am trying to accomplish is to display all images uploaded by the user ( which are currently 4 ) but I only get 3 ( the one missing is always the first one ). Here's my code:
<?php
$currentUser = $_SESSION['id'];
$sql = "SELECT username FROM user WHERE id='$currentUser'";
$result = mysqli_query($conn, $sql);
$getResult = mysqli_fetch_assoc($result);
$author = $getResult['username'];
$sql2 = "SELECT * FROM image WHERE author='$author' ORDER BY id DESC";
$result2 = mysqli_query($conn, $sql2);
$getResult2 = mysqli_fetch_assoc($result2);
while ($row = $result2->fetch_assoc()){
echo '<img class="profilePageImages" src="uploads/'.$row['path'].'" alt="Random image" />';
}
?>
This line, before the loop is consuming the first row. Just delete it.
$getResult2 = mysqli_fetch_assoc($result2);
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 5 years ago.
I have this website that has an error handling when adding groups, which means if I have already 5 groups the website does not allow to add more. For that to happen I query the database, and with that query result I try to access the length from it.
If that length is <4 I will allow the group to be inserted else I will not allow and only show a message. This can be seen in the code above but for some reason it always allow the insertion into the query.
$con = "put the con here";
$query2 = "SELECT * from title where iduser= {$_SESSION['iduser']}";
$result = mysqli_query($con, $query2) or die (mysql_error());
$num_rows = mysql_num_rows($result);
echo $num_rows; //save the size in $num_rows
if($num_rows < 4){ //Did the condition here
$query="INSERT INTO grouptitle(title,iduser) VALUES ('$variavel',{$_SESSION['id_utilizador']})";
mysqli_query($con, $query) or die (mysql_error());
?>
<script>
alert("Grupo adedd");
self.location="Definitions.php";
<?php }else{ ?>
<script>
alert("Exceded the group limit");
self.location="Definitions.php";
<?php } ?>
The 4th line of your code there buddy... try this
$num_rows = mysqli_num_rows($result);