Selecting a value by row number - php

I have the following MySQL table:
table_1
id | value_1 | value_2 | value_3
1 hehe haha stack
2 over flow me
3 123 abc hello
4 hi random php
5 html js css
How can I select value_2 from 3rd row, which is "abc"?
Something like $rows[3]['value_2']
Here's the code I had tried with:
$sql=mysql_query("SELECT * FROM table_1 ORDER BY `table_1`.`id` ASC");
$rows=mysql_fetch_array($sql);
//And then I tried accessing it by "$rows[3]['value_2']" and $rows['value_2'][3]
I can't use SELECT * FROM table_1 WHERE id='3', because I need to access multiple lines (all of them). I can't use a WHILE loop also, because I need the values in very different places in the code.

mysql_fetch_array only returns a row corresponding to the query. The proper use of mysql_fetch_array is as such:
$sql=mysql_query("SELECT * FROM table_1 ORDER BY `table_1`.`id` ASC");
while ($row = mysql_fetch_array($sql)) {
printf("ID: %s value_2: %s", $row[0], $row[2]);
}
http://www.php.net/manual/en/function.mysql-fetch-array.php

Found the answer. You can now access them how you wanted.
$sql=mysql_query("SELECT * FROM settings ORDER BY `settings`.`id` ASC");
$id=0;
while ($rows = mysql_fetch_array($sql)) {
$settings[$id]['value_1']=$rows['value_1'];
$settings[$id]['value_2']=$rows['value_2'];
$settings[$id]['value_3']=$rows['value_3'];
$id++;
}
echo $settings[5]['value_3'];
echo $settings[5]['value_1'];
echo $settings[4]['value_2'];
echo $settings[0]['value_3'];

Related

PHP MYSQL, two tables

I have two tables and i want to echo the total call count once each user logins:
Login
Firstname | Lastname | Login ID
------------------------------
Tyler | Durden | 3
Call Count
Name | Call Count | Open | GrandTotal
------------------------------
Tyler Durden| 100 | 33 | 133
i tried:
<?php
$result = mysqli_query($mysqli, "SELECT * FROM csvdata WHERE Name=".$_SESSION['firstname']. ' ' .$_SESSION['lastname']." ");
while($res = mysqli_fetch_array( $result )) {
echo $res['Open'].' Open Calls';
echo $res['GrandTotal'].' Grand Total Calls';
}
mysqli_close($mysqli);
?>
But its not working, i think i have to join the the two tables to get it to work. What do you think?
Assuming your Call Count table is actually called csvdata, you'll want to format your SQL request string a bit by adding single quotes around the WHERE name = part.
<?php
$result = mysqli_query($mysqli, "SELECT * FROM csvdata WHERE Name='".$_SESSION['firstname']. ' ' .$_SESSION['lastname']."' ");
while($res = mysqli_fetch_array( $result )) {
echo $res['Call Count'].' Call Count';
echo $res['Open'].' Open Calls';
echo $res['GrandTotal'].' Grand Total Calls';
}
mysqli_close($mysqli);
?>
Good practice would require that you use primary keys to facilitate joins between tables and make sure two users with the same name can be differenciated.
In that case you may want to consider replacing the Name column in your Call Count table for your loginID. This way you could get your name from the Login table (as shown below). Also, as bad as it is to have duplicated data like your user's name in both tables, you do not need your GrandTotal column since you can easily get the sum of CallCount and Open to get the exact same number. In the end, your query should look more like this (assuming your tables are called Login and CallCount).
<?php
$result = mysqli_query($mysqli, "SELECT l.FirstName, l.LastName, cc.CallCount, cc.Open, (cc.CallCount + cc.Open) AS GrandTotal FROM Login AS l JOIN CallCount AS cc ON l.LoginID = cc.LoginID WHERE l.FirstName LIKE \"".$_SESSION['firstname']."\" AND l.LastName LIKE \"".$_SESSION['lastname']."\"");
// ...
?>

Most Efficient way to MySQL Query inside query

i'm just learning PHP, and i have a question
Let just say, i have MySQL table "A"
Name | Job
--------|---------
Jynx | 1
Micah | 4
Nancy | 3
Turah | 1
And another table "B"
JobId | JobName
-------|-----------
1 | Lawyer
2 | Architec
3 | Farmer
4 | Mage
5 | Warrior
So supposedly in php i want to draw table that showed the content of table "A", but instead of displaying number at the "Job" colomn, they each display Job names from Table "B".
What is the most efficient way to do that?
For now, i just thinking of using
$conn = My database connect setting
$sql = "SELECT * FROM tableA ORDER BY Name";
$result = $conn->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>". $row['Name'] ."</td><td>";
$sql2 = "SELECT * FROM tableB WHERE JobId=$row['Job']";
$result2 = $conn->query($sql2);
while($row2 = mysqli_fetch_assoc($result2)) {
echo "<td>". $row2['JobName'] ."</td></tr>;
}
}
But wouldn't it take a lot of calculating proccess if there is multiple similliar colomn with hundreed of rows?
Is there any more efficient way to do this?
Sorry for my bad english
Thank you for your attention.
A join is definitely the way to go here.
SELECT a.Name, b.JobName
FROM tableA a
JOIN tableB b on (a.Job = b.JobId)
ORDER BY a.Name
Well, it is not easy to learn about JOIN, no time for now, but i will get to learn it latter.
As for now, i just get the idea to just use ARRAY instead
So before i draw the main table, i assign the supportive table (Table B) into associative array
$sql = "SELECT * FROM tableB ORDER BY Id";
$result = $conn->query($sql);
while($row = mysqli_fetch_assoc($result)) {
$job[$row['JobId']] = $row['JobName'];
}
And at the main table
$sql = "SELECT * FROM tableA ORDER BY Name";
$result = $conn->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>". $row['Name'] ."</td><td>". $job[$row['Job']];
}

Extracting several elements using php

I have these two tables in my database :
table_A: table_B:
id user id user
1 Mike 1 Mike
2 Dan 2 Dan
3 Tom 3 Tom
4 Lina 4 Lina
5 Cynthia
6 Sam
And i'm using this SQL query, to detect which users in table_B do not exist in table_A, and select those users (not existing in table A) ids:
SELECT table_B.id FROM table_B WHERE table_B.id NOT IN (SELECT table_A.id FROM table_A);
And i'm using this php script to put the ids in a variable $not:
$sql=" SELECT table_B.id FROM table_B WHERE table_B.id NOT IN (SELECT table_A.id FROM table_A)";
$result = mysqli_query($con,$sql);
if (!$result) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
while($row = mysqli_fetch_array($result)){
$not=$row[0];
}
Now if i want to extract 2 elements with my sql query:
SELECT table_B.id, table_B.name FROM table_B WHERE table_B.id NOT IN (SELECT table_A.id FROM table_A);
How can i place both, each in a variable using the above script, so i'll be able to insert each element in a different table in the database?
You can put the values in a multidimensional array.
$not = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$not[] = array(
"id" => $row['id'],
"name" => $row['name']
);
}
echo $not[0]['id']; //Returns 5
echo $not[0]['name']; //Returns Cynthia
echo $not[1]['id']; //Returns 6
echo $not[1]['name']; //Returns Sam
in your query define table_B.id as user_id and table_B.name as user
then in your php use mysqli_fetch_object like this
while($row = mysqli_fetch_object($result)){
$not= $row;
//or if you want to store all the results use $not[] = $row; and define $not as array
}

SELECT duplicate columns in MYSQL database PHP

I have a database table titled products.
I have 2 columns in the table titled name and image
I have 4 entries in my database
item 1 | image1.jpg
item 2 | image1.jpg
item 3 | image1.jpg
item 4 | image2.jpg
I need to display the results on a page but I only need to display image.1jpg once, my while() loop is obviously displaying all 4 images.
I've been at this for hours, please help!
My code:
$query = mysql_query("SELECT * FROM products WHERE page='Birthdays' ORDER BY id ASC");
while($fetch = mysql_fetch_assoc($query)){
$image = $fetch['image'];
$name = $fetch['name'];
echo ''.$name.'';
echo '<img src="'.$thumbnail_image.'"/>';
}
What you need is using GROUP BY in your query. So change the query as
SELECT * FROM products WHERE page='Birthdays' GROUP BY image ORDER BY id ASC
Try the code below;
$query = mysql_query("SELECT * FROM products WHERE page='Birthdays' GROUP BY image ORDER BY id ASC");
while($fetch = mysql_fetch_assoc($query)){
$image = $fetch['image'];
$name = $fetch['name'];
echo ''.$name.'';
echo '<img src="'.$thumbnail_image.'"/>';
}
You can also use a trick like not using while statement at all. If you just write the
$fetch = mysql_fetch_assoc($query) without while you would just get the first result in the query. I hope this helps.

PHP MySQL query outputs only a two-element array

I just made a quick little script with SQL query.
Now when I go to phpmyadmin and execute
SELECT name FROM players WHERE online='1' ORDER BY name ASC
It outputs the desired players ( 0TheMonk, Player, Veeve )
But with PHP:
$query=mysql_query("SELECT name FROM players WHERE online='1' ORDER BY name ASC");
$query_array=mysql_fetch_array($query);
echo implode(',',$query_array);
It echoes: 0TheMonk,0TheMonk
Instead of: 0TheMonk,Player,Veeve
It always outputs the first player in the array, twice. What am I doing wrong?
Thanks in advance.
Use while loop
$query=mysql_query("SELECT name FROM players WHERE online='1' ORDER BY name ASC");
while($query_array=mysql_fetch_array($query))
{
echo $query_array['name'].",";
}
Try this : Almost same as Sumits answer.
$res = array();
$query=mysql_query("SELECT name FROM players WHERE online='1' ORDER BY name ASC");
while($query_array=mysql_fetch_array($query))
{
$res[] = $query_array['name'];
}
echo implode(",",$res);
In this case there will not a extra , at the ens

Categories