Accessing multiple sql files in one php document - php

I am trying to be able to access multiple dates and be able to map them to one player, and one country. The multiple years are in one table on the database, this table then uses foreign keys to attach the other two tables (player, country). How can access all of the years that the one individual won without adding it as a separate table?
below is my sql and php.
Many thanks in advance
$sql = "SELECT w.year, p.wikilink, c.countryname, c.countrylink, c.region, c.regionlink, p.playername, c.flag
FROM worldchampionperiod AS w
LEFT JOIN country AS c
ON w.Country_idcountry = c.idcountry
LEFT JOIN player AS p
ON w.player_idplayer = p.idplayer
GROUP BY p.idplayer
ORDER BY year";
$result = $conn->query($sql);
if ($result->rowCount() > 0) {
// output data of each row
while($row = $result->fetch()) {
echo "<tr>";
// echo "<td>" <a href=$row[""]>delete</a>. $row["playername"] . "</td>";
echo "<td>" . $row["playername"] . "</td>";
echo "<td>" . $row["year"] . "</td>";
//echo "<td>" . $row["countryname"] . <a href="'. $row["countrylink"] .'"> . "<br>" . $row["region"] . '<a href="'. $row["regionlink"] .'">' "</td>";
echo "<td>". ''. $row["countryname"].'' ."<br>" . ''. $row["region"].'' . "<br>" . '<img src="' .$row["flag"].'"width=30px>' . "</td>"; "</td>";
echo "</tr>";

Related

How to escape SELECT COUNT (*) and create a table

I am fairly new to working with PHP and WordPress. I tried exploring how to escape and sanitize, and I got a little confused along the way.
I'd like to echo out the contents of an entire table from the database. I am unsure whether there is a better way of both creating a table in a more structured way, and I can't figure out how to escape the data when I don't select specific data from the database. Maybe i'm just confused. Any help or pointers is highly appreciated.
I found the code somewhere on Stack Overflow, edited a little and tried to understand it. I understand it now, but I am still confused on where to go from here.
<?php
$results = $wpdb->get_results( "SELECT * FROM user"); // Query to fetch data from database table and storing in $results
if(!empty($results)) // Checking if $results have some values or not
{
echo "<table width='100%' border='0' id='userTable'>"; // Adding <table> and <tbody> tag outside foreach loop so that it wont create again and again
echo "<tbody>";
echo "<tr>"; // Adding rows of table inside foreach loop
echo "<th>E-mail</th>" . "<th>Fornavn</th>" . "<th>Efternavn</th>" . "<th>Registreret den</th>";
echo "</tr>";
foreach($results as $row){ //putting the user_ip field value in variable to use it later in update query
echo "<td colspan='3'><hr size='2'></td>";
echo "<tr>";
echo "<td>" . esc_attr($row->email) . "</td>" . "<td>" . $row->firstname . "</td>" . "<td>" . $row->lastname . "</td>" . "<td>" . $row->signuptime . "</td>"; //fetching data from user_ip field
}
echo "</tbody>";
echo "</table>";
}
?>
This part...
foreach($results as $row){
//putting the user_ip field value in variable to use it later in update query
echo "<td colspan='3'><hr size='2'></td>";
echo "<tr>";
echo "<td>" . esc_attr($row->email) . "</td>" . "<td>" . $row->firstname . "</td>"
. "<td>" . $row->lastname . "</td>" . "<td>" . $row->signuptime . "</td>";
//fetching data from user_ip field
}
...would product html like
{3 columns}{content}{3 columns end}{row start}
{column start}{content}{column end} * 4
{3 columns}{row start}
{column start}{content}{column end} * 4
{3 columns}{row start}
{column start}{content}{column end} * 4
etc
What you want in your loop is probably:
{row start}{4 columns}{content}{4 columns end}{row end}
{row start}{column start}{content}{column end} * 4{row end}
which would look like this:
foreach($results as $row){
//putting the user_ip field value in variable to use it later in update query
echo "<tr><td colspan='4'><hr size='2'></td></tr>";
echo "<tr><td>" . esc_attr($row->email) . "</td>" . "<td>" . $row->firstname . "
</td><td>" . $row->lastname . "</td>" . "<td>" . $row->signuptime . "</td></tr>";
//fetching data from user_ip field
}
In your SQL-statement on your first row: SELECT * FROM user , all fields are returned into the $results array of objects. If you want to specify which fields are returned you simply include them instead of the *, e.g. SELECT id, email, firstname, lastname FROM user

Why is only one row from this query displayed?

I am trying to print the results of this query, but it only prints out the first row. Why is that?
if (isset($_GET['consumiperstanza'])) {
$num_stanza=$_GET['num_stanza'];
$data1=$_GET['data1'];
$data2=$_GET['data2'];
$query="SELECT stanze.num_stanza,consumi.cod_consumo, servizi.nome,
consumi.quantita, servizi.prezzo, consumi.data_c
FROM consumi, servizi, stanze
WHERE stanze.num_stanza=consumi.num_stanza
AND servizi.cod_servizio=consumi.cod_servizio
AND stanze.num_stanza='$num_stanza'
AND consumi.data_c BETWEEN '$data1' AND '$data2'
GROUP BY stanze.num_stanza";
$risultato = $conn->query($query);
if(mysqli_num_rows($risultato) > 0){
echo "<table border=1 bgcolor= 'white' align='center'>";
echo "<tr>";
echo "<th>NUMERO STANZA</th>";
echo "<th>CODICE CONSUMO</th>";
echo "<th>NOME</th>";
echo "<th>QUANTITÀ</th>";
echo "<th>PREZZO</th>";
echo "<th>DATA</th>";
echo "</tr>";
while($riga = mysqli_fetch_array($risultato)){
echo "<tr>";
echo "<td>" . $riga[0] . "</td>";
echo "<td>" . $riga[1] . "</td>";
echo "<td>" . $riga[2] . "</td>";
echo "<td>" . $riga[3] . "</td>";
echo "<td>" . $riga[4] . "</td>";
echo "<td>" . $riga[5] . "</td>";
echo "</tr>";
}
echo "</table><br>";
}
}
This is part of your criteria
AND stanze.num_stanza='$num_stanza'
But you're also doing this.
GROUP BY stanze.num_stanza
So, you'll only get one group.
Additionally, all the other columns in your SELECT are not well-defined since they are not included in the GROUP BY and are not aggregate expressions. Newer versions of MySQL actually will not allow you to do this by default. It is possible in older versions, but may not give you the results you expect.
The MySQL 5.6 manual:
... this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are nondeterministic.

Reference child table with a main table

In my database I have a jobs table which you can say is the main table in my database. I have a column called category and this column points to a table called category which holds different categories.
Picking up the concept of foreign keys,I made turned the column categories into a foreign key, which looks at the categories table.
In my categories table I make sure it points to the ID.
When I run my webpage it prints out the value 1 in the category column, when in theory should it not print "Driving"?
function getJobDetails($job,$cat){
//this connects to the database
include "connectToDatabse.php";
//show me the results from job, where category is like cat vice versa
$results = $pdo->query("SELECT * FROM job WHERE category LIKE '$cat%' OR title LIKE '$job'");
$str = "<table>"; //prints out table
$str .= "<td>" ."Title" . "</td>"; //first row
$str .= "<td>" ."Reference" . "</td>"; //N row...
$str .= "<td>" ."Salary(£)" . "</td>";
$str .= "<td>" ."Description" . "</td>";
$str .= "<td>" ."Category" . "</td>";
foreach ($results as $row) {
$ref = $row['reference'];
$link = "<form method='get' action='apply.php' name='edit'>
<input type='hidden' name='referenceNumber' value='$ref'>
<input type='submit' value='$ref'>
</form>";
$str .= "<tr>";
$str .= "<td>" . $row['title'] . "</td>";
$str .= "<td>" . $row['reference'] . "</td>";
$str .= "<td>" . $row['salary'] . "</td>";
$str .= "<td>" . $row['description'] . "</td>";
$str .= "<td>" . $row['category'] . "</td>";
$str .= "<td> " .$link . "</td>";
$str .= "</tr>";
}
$str .= "</table>";
echo $str;
}
The above code is a function that returns the data in the job table.
Edit: referring to the question, since the categories column is pointing to the category table, should it not refer the data back to the job table?
No it shouldn't. You're missunderstanding foreign-keys. They do not change the data you get, they only tell the database system 'hey, this references somthing else, if this other entry gets updated or deleted, please do X (update, delete, ...) with this entry here'. You'd still need a join to get the expected result:
SELECT ..., category.title FROM job LEFT JOIN category ON category.id = job.category

Filtering MySQL Database by field result

I am pulling stock data off of a trading API through a loop based on an aray of stock symbols. This spits out xml data which I then parse and input into a MySQL database for analysis later. This is an example of the fields I'm inserting into the database (I can add more):
$sql = "INSERT into securities (symbol, ask, ask_time, asksz)
VALUES ('$symbol', '$ask', '$ask_time','$asksz')";
My current query pulls all of the data and lists it in rows, entry after entry into a table:
$sql = "SELECT id, symbol, ask, ask_time, asksz FROM securities";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>symbol</th>";
echo "<th>ask</th>";
echo "<th>ask_time</th>";
echo "<th>asksz</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['symbol'] . "</td>";
echo "<td>" . $row['ask'] . "</td>";
echo "<td>" . $row['ask_time'] . "</td>";
echo "<td>" . $row['asksz'] . "</td>";
echo "</tr>";
}
echo "</table>";
Is there a way to compare two separate stocks side by side? For instance, structure my query in order to pull those 4 fields only for 'AAPL', then in the same row pull the same data for 'GOOG'?

You can't specify target table for update in FROM clause in MySQL

I want to change the status of a single order by selecting its requisition_id where the status matches a specific keyword.
But it keeps showing me this error "You can't specify target table for update in FROM clause"
UPDATE order_info
SET status ='Approved'
WHERE requisition_no IN (SELECT requisition_no FROM order_info WHERE status = 'not approved')
What can I do to fix it?
EDIT- I've included the PHP code as well
while ($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['requisition_no'] . "</td>";
echo "<td>" . $row['desig'] . "</td>";
echo "<td>" . $row['deptt'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['make'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo '<form method="post" action="status.php">'; /*Redirects to status.php to change the status (using diff. page because i'm a hopeless kid, who doesnt want to learn new ways of simplifying things)*/
echo "<td>" . $row['status'] . "<br/>";
if($row['status']=='Approved' || $row['status']=='Under Procurement'){//does nothing
}
/*Here, using a submit button*/
else { echo '<input type="submit" name="status" value="Approve"></td>';
$_SESSION['rno'] = $row['requisition_no'];}
echo '</form>';
You have a few syntax errors probably causing this error. You are missing an equals sign = after status and the right parenthesis ) at the end:
UPDATE order_info o
SET o.status = 'Approved'
WHERE o.requisition_no IN (SELECT x.requisition_no FROM (select * from order_info) x WHERE x.status = 'not approved')
Also, I think your query can be rewritten like this:
UPDATE order_info
SET status = 'Approved'
WHERE status = 'not approved'

Categories