This question already has answers here:
MySQL JOIN tables with duplicate column names
(1 answer)
Ambiguous field name between an alias and a field in the select list with name
(3 answers)
Closed 12 months ago.
I have two tables currencies and markets, I want to use id, last, volume, bid, trade_status from markets table and only Symbol from currencies table.
when I using the bellow code as I have volume and id in both tables, that is multiply times showing
<?php
$con = mysqli_connect("localhost","Dotland","passwd","Dotland");
$response = array();
if($con){
$sql = "select * from markets, currencies";
$result = mysqli_query($con,$sql);
if ($result){
header("content-Type: JSON");
$i=0;
while($row = mysqli_fetch_assoc($result)){
$response[$i]['id'] = $row ['id'];
$response[$i]['symbol'] = $row ['symbol'];
$response[$i]['last'] = $row ['last'];
$response[$i]['volume'] = $row ['volume'];
$response[$i]['bid'] = $row ['bid'];
$response[$i]['trade_status'] = $row ['trade_status'];
$i++;
}
echo json_encode($response,JSON_PRETTY_PRINT);
}
}
else{
echo "Database Connection Failed";
}
?>
Related
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:
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:
MySQL returns only one row
(5 answers)
Closed 6 years ago.
I want to list all username from database if the id exists. That code works fine in database select * from users where id IN (1,2,3,4,5).
But in php it doesn't work. My php code is
$sql = "select * from users where id IN (1,2,3,4,5)";
$result = mysql_query($sql);
if ( mysql_num_rows($result) > 0 ) {
$row = mysql_fetch_assoc( $result );
echo $row["username"];
}
This php code only list one user name. I want to list all usernames. Sorry for my bad English. Thanks.
<?php
$sql = "select * from users where id IN (1,2,3,4,5)";
$result = mysql_query($sql);
if ( mysql_num_rows($result) > 0 ) {
while($row = mysql_fetch_assoc( $result )) {
echo $row["username"]."\n";
}
}
if you only fetch once, you will only get one row - obviously. so if you want all rows, you have to fetch as long as there are rows to fetch.
This question already has answers here:
MySQL PHP - SELECT WHERE id = array()? [duplicate]
(6 answers)
Closed 8 years ago.
Which is the best way to Get multiple rows in one MySQL query.
I have an array of IDs:
$id_array = array('34','341','342','334','344','354','3234','33234','3234','3234');
I would like to get the title associated with those id's from my mysql database.
I have two approaches:
1) example:
foreach($id_array as $id){
$query = mysqli_query($con, "SELECT title FROM content WHERE id = '$id'");
$id_db = mysqli_fetch_row($query);
echo $id_db['title'];
}
2) example:
$query = mysqli_query($con, "SELECT title FROM content WHERE id = '$id_array[1]' AND id = '$id_array[2]' AND id = '$id_array[3]' AND 'id = $id_array[4]' AND id = '$id_array[5]'");
while($result = mysqli_fetch_assoc($query)){
echo $result['title'];
}
I am working on high load site and would like to use the best solution.
The above code isn't 100% complete, it is just a raw implementation of the idea.
The array elements can be from 1 to 1k in count.
What about this solution ?
$ids = implode(',',array_map('intval',$id_array));
$query = mysqli_query($con, "SELECT title FROM content WHERE id IN ($ids)");
// ..
Use mysql in array method man.
If you want get titles for ids that are in array:
$db = new mysqli('server', 'user', 'password', 'db');
$stmt = $db->prepare('select title form content where id = ?');
$stmt->bind_param('i', $i);
$stmt->bind_result($title);
$titles = array();
foreach($id_array as $a) {
$i = $a;
$stmt->execute();
array_push($titles, $title);
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MYSQL COUNT GROUP BY question
$query = "SELECT * FROM product_id_1";
$result = mysql_query($query);
while( $record = mysql_fetch_array($result) ){
$array[] = $record['username'];
$unique_array = array_unique($array);
}
foreach( $unique_array as $list ){
echo $list.'<br/>';
}
Greeting,
I'm stuck trying to figure out how to apply array_count_unique(). Reason is I need to find out the count of each individual user in the table, i.e how many times adam appears and how many times abcd appears.
UPDATE
This is what I've done according to you guys's suggestion:
$query = 'SELECT username, COUNT(*) AS username_count FROM product_id_1 GROUP BY username';
$result = mysql_query($query);
while ($record = mysql_fetch_object($result)) {
echo $record->username;
echo $record->username_count;
}
This is exactly why we have RDBMS and SQL:
SELECT COUNT(1) AS username_count, username
FROM
product_id_1
GROUP BY
username