What is wrong about this code??
echo "<table>";
echo "<tr><td>Machine Nummer</td><td>Eigenaar</td><td>Status</td><td>Locatie</td></tr>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['machineid'] . "</td><td> ". $eigenaar=mysql_query("SELECT userName FROM users WHERE userId =" . $row['eigenaar'] ."") ."</td><td>" . $row['status'] . "</td><td>" . $row['locatie'] . "</td></tr>";
}
echo "</table>";
$res=mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
$query = "SELECT * FROM machines WHERE locatie='".$_SESSION['location']."'"; //You don't need a ; like you do in SQL
$result = mysql_query($query);
I want it to output the eigenaar name which equals to the userId but i only get the output:Resource id #9 Resource id #10 Resource id #11.
The answer is to use the INNER JOIN function.
Related
Im sorry for my bad english but this is the best i can.
I am trying to make a script that get's the item values of one line in my
for this example i'll be using the row with ID 2.
The first query is working correctly and getting the 2 values of 1items and 2items and deletes the ; that comes with it.
But the second part seems a little bit harder and i can't solve it.
The query is getting id, base_item from the table
The last 2 Outputs marked red are the one's that are matching 1items & 2items
I'm trying to let the if statement filter the $item1 as id in the item table and output the baseitem of the found row
same goes for item2
I'm using MySQL & MySQLi because this cms doesnt support newer versions of PHP yet.
<?php
$query = "SELECT * FROM logs_client_trade ORDER by id DESC";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$item1 = rtrim($row['1items'],"; ");
$item2 = rtrim($row['2items'],"; ");
echo("<tr>");
echo("<td>" . $row['id'] . "</td>");
echo("<td>" . $row['1id'] . "</td>");
echo("<td>" . $row['2id'] . "</td>");
$userinfo = "SELECT id, base_item FROM items LIMIT 1";
$result2 = mysql_query($userinfo) or die(mysql_error());
while($row2 = mysql_fetch_assoc($result2)){
echo("<td>");
if($row2['id'] == $item1){ echo $row2['baseitem']; } else echo "Not available";
echo ("</td>");
echo("<td>");
if($row2['id'] == $item1){ echo $row2['baseitem']; } else echo "Not available";
echo ("</td>");
}
$tradetime = $row['timestamp'];
$date = date("d M Y - H:i:s", $tradetime);
echo("<td>$date</td></tr>");
}
?>
You forgot the while loop for the second query:
$userinfo = mysql_query("SELECT id, base_item FROM items");
while($get2 = $userinfo->fetch_assoc()) {
$baseid = $get2['id'];
$baseitem = $get2['baseitem'];
echo("<tr>");
[...]
}
In addition to that, your code is a mess. In the second loop you are still listing the rows from the logs table... I suggest you to review carefully your code, being careful on how you are using the different $row and $get2 arrays.
You can use a JOIN to get all of the info with one SQL statement:
SELECT *
FROM `logs_client_trade` a
LEFT JOIN `items` b
ON REPLACE(a.`1items`,';','') = b.`id`
LEFT JOIN `items` c
ON REPLACE(a.`2items`,';','') = c.`id`
ORDER by a.`id` DESC
UPDATE:
Here's the PHP code that will run the query and output the table rows. I've specified the column names based on the code in the original question.
<?php
$query = "SELECT
a.`id`,
a.`1id`,
a.`2id`,
a.`1items`,
a.`1items`,
IFNULL(b.`baseitem`,'Not Available') as `baseitem1`,
IFNULL(c.`baseitem`,'Not Available') as `baseitem2`,
DATE_FORMAT(a.`timestamp`,'%d %m %Y - %H:%i:%s') as `tradetime`
FROM `logs_client_trade` a
LEFT JOIN `items` b
ON REPLACE(a.`1items`,';','') = b.`id`
LEFT JOIN `items` c
ON REPLACE(a.`2items`,';','') = c.`id`
ORDER by a.`id` DESC
";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$item1 = rtrim($row['1items'],"; ");
$item2 = rtrim($row['2items'],"; ");
echo "<tr>\r\n";
echo " <td>" . $row['id'] . "</td>\r\n";
echo " <td>" . $row['1id'] . "</td>\r\n";
echo " <td>" . $row['2id'] . "</td>\r\n";
echo " <td>" . $row['baseitem1'] . "</td>\r\n";
echo " <td>" . $row['baseitem2'] . "</td>\r\n";
echo " <td>" . $row['tradetime'] . "</td>\r\n";
echo("</tr>\r\n");
}
?>
$sql = mysqli_query($conn, "SELECT * FROM stock WHERE Date = '$date'");
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
$d="D-";
// output data of each row
echo "<table><tr><th>ID</th><th>Date</th><th>Product Code</th><th>Description</th><th>Delivery Order</th><th>Cartons</th><th>Items</th><th>Quantity</th><th>Sent_To</th></tr>";
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["Date"]. "</td><td>" . $row["Product_Code"]. "</td><td>" . $row["Description"]. "</td><td>".$d. $row["DEO"]. "</td><td>" . $row["Cartons"]. "</td><td>" . $row["Items"]. "</td><td>" . $row["Quantity"]. "</td><td>" . $row["Sent_To"]. "</td></tr>";
}
echo "</table>";
Hello Guys When i echo $date it prints the date correctly but when i run this query i get an error
Warning: mysqli::query() expects parameter 1 to be string, object
given in C:\xampp\htdocs\rmt\displaydate.php on line 9
Notice: Trying to get property of non-object in
C:\xampp\htdocs\rmt\displaydate.php on line 11
Please help me to fix this error. Thanks in advance.
You're calling mysqli_query followed by $conn->query on the result from the first query. You only need to call one of them, for example;
$sql = "SELECT * FROM stock WHERE Date = '$date'";
$result = $conn->query($sql);
I want to select faster than this my page load is very slow when executing the script.
$bmw= mysqli_query($con,"SELECT * FROM cars WHERE name='bwm'
ORDER BY date DESC LIMIT 1"); $mercedes= mysqli_query($con,"SELECT * FROM cars
WHERE name='mercedes' ORDER BY date DESC LIMIT 1");
$audi= mysqli_query($con,"SELECT * FROM cars WHERE name='audi'
ORDER BY date DESC LIMIT 1");
$skoda= mysqli_query($con,"SELECT * FROM cars WHERE name='skoda'
ORDER BY date DESC LIMIT 1");
And echo it out like here below, like that the page load is very slow.
while($row = mysqli_fetch_array($bmw)) { echo $row['name'] . " " .
$row['date'];
}
while($row = mysqli_fetch_array($mercedes)) { echo $row['name'] . " "
. $row['date'];
}
while($row = mysqli_fetch_array($audi)) { echo $row['name'] . " " .
$row['date'];
}
Thanks in advance
There is nothing wrong in the queries you wrote, your performance issue may be due to :
missing indexes on name and date : create indexes on name and date
cars database too big for your hardware : new hardware/partitionning/redesign
network link slow : solution provided by Lacy K is interesting as there is only one query sent to mysql
You could also improves performance a bit by selecting only name and date instead of *
select name, date from ...
Why not selecting all make at once like this:
$q= mysqli_query($con,"SELECT * FROM cars WHERE name IN ('bwm', 'mercedes', 'audi', 'skoda') ORDER BY date DESC LIMIT 1");
while($row = mysqli_fetch_array($q)) {
if($row['name'] == 'bmw'){
echo $row['name'] . " " . $row['date'];
}
elsif($row['name'] == 'mercedes'){
echo $row['name'] . " " . $row['date'];
}
elsif($row['name'] == 'audi'){
echo $row['name'] . " " . $row['date'];
}
elsif($row['name'] == 'skoda'){
echo $row['name'] . " " . $row['date'];
}
else{
echo $row['name'] . " " . $row['date'];
}
}
Total newbie trying to learn... thanks for all the help so far!
UPDATE - updated code below - Thanks Phill
I'm now trying to get the relevant horse information from a table horses which I can join to race_form with the horse_name field. I want to then display the information for each horse in the race below the while($racecard) info. Does this make it a bit clearer?
I thought I could just do another query and then a while loop with mysql_fetch_array below the one I have already and that would display it and then move onto the next race, but that apparently doesn't work...?!
I'm wondering if you can run 2 while loops after each other inside a while loop? I am working on a horse racing formguide - I can display each race list, but underneath I want to display individual horse details on each horse in the race. Anyway if you look at this page you can see what I'm trying to do:
http://tasmanianracing.com/superform/formguide.php?meetingID=21042011LAUN&Submit=View+Form
Problem is, any time I put a second while loop after the race list, it won't show up. I'm trying to run a query to get the horse details from my horse table and then run a while for each horse in the race, but nothing shows up.
I hope this is clear enough ... my snippet of code is below - please let me know if I've missed out something important:
echo "<table width='990' border='1' cellspacing='2' cellpadding='2'>";
$racedetails = mysql_query("SELECT *
FROM race_info
WHERE meetingID = ('" . $safemeetingID . "')");
while($row = mysql_fetch_array($racedetails))
{
echo "<tr><td>Race " . $row['race_number'] . " at " . $row['start_time'] . " " . $row['class'] . " over " . $row['distance'] . "m</td></tr>";
$racecard = mysql_query("SELECT *
FROM race_form
INNER JOIN race_info ON race_form.raceID = race_info.raceID
WHERE race_form.raceID = ('" . $row['raceID'] . "')");
while($row = mysql_fetch_array($racecard))
{
echo "<tr><td>" . $row['number'] . "</td><td>" . $row['past10'] . "</td><td>" . $row['horse_name'] . "</td></tr>";
}
echo "</table><br>";
echo "<br>I wish I could put in some horse form here...<br>";
echo "<table width='990' border='1' cellspacing='2' cellpadding='2'>";
}
echo "</table>";
You'll probably need to change the names of some of the $row variables, they may interfere with each other.
For example:
while($row_races = mysql_fetch_array($totalraces)){
while($row_details = mysql_fetch_array($racedetails)){
while($row_card = mysql_fetch_array($racecard)){
I think you can get rid of one of your queries:
The first query gets the number of races by selecting a COUNT, This returns one record with the count value.
// This returns one record with the count
$total_races = "SELECT COUNT(race_number) AS totalraces
FROM race_info WHERE meetingID = ('".$safemeetingID."') ";
Next you iterate over the the same records as the rows returned for the race details are the same as the count.
// This looks to return the record(s) with the race details
$race_details = "SELECT * FROM race_info
WHERE meetingID = ('" . $safemeetingID . "')";
I think you can just use this to get the desired results: (I agree to rename the $row variable(s) to something descriptive for each while loop)
$racedetails = mysql_query("SELECT *
FROM race_info
WHERE meetingID = ('" . $safemeetingID . "')");
while($details_row = mysql_fetch_array($racedetails))
{
echo "<tr><td>Race " . $details_row['race_number'] . " at " . $details_row['start_time'] . " " . $details_row['class'] . " over " . $details_row['distance'] . "m</td></tr>";
$racecard = mysql_query("SELECT *
FROM race_form
INNER JOIN race_info ON race_form.raceID = race_info.raceID
WHERE race_form.raceID = ('" . $details_row['raceID'] . "')");
while($rc_row = mysql_fetch_array($racecard))
{
echo "<tr><td>" . $rc_row['number'] . "</td><td>" . $rc_row['past10'] . "</td><td>" . $rc_row['horse_name'] . "</td></tr>";
}
echo "</table><br>";
echo "Testing<br>Testing<br>I wish I could put in some horse form here...<br>";
echo "<table width='990' border='1' cellspacing='2' cellpadding='2'>";
}
NOT TESTED/PSEUDO CODE
"SELECT *
FROM horses AS h,
INNER JOIN race_info AS ri ON race_form.raceID = race_info.raceID
WHERE horse_name IN (
SELECT horse_name
FROM race_form AS srf
WHERE h.horse_name = srf.horse_name
)
AND race_form.raceID = ('" . $details_row['raceID'] . "')"
The idea is to join the two queries into one, I know this is not the correct syntax but it might give you an idea on how to go about it.
Or you can do another query while loop for the horse names
$racedetails = mysql_query("SELECT *
FROM race_info
WHERE meetingID = ('" . $safemeetingID . "')");
while($details_row = mysql_fetch_array($racedetails))
{
echo "<tr><td>Race " . $details_row['race_number'] . " at " . $details_row['start_time'] . " " . $details_row['class'] . " over " . $details_row['distance'] . "m</td></tr>";
$racecard = mysql_query("SELECT *
FROM race_form
INNER JOIN race_info ON race_form.raceID = race_info.raceID
WHERE race_form.raceID = ('" . $details_row['raceID'] . "')");
while($rc_row = mysql_fetch_array($racecard))
{
echo "<tr><td>" . $rc_row['number'] . "</td><td>" . $rc_row['past10'] . "</td><td>" . $rc_row['horse_name'] . "</td></tr>";
$horses = mysql_query("SELECT *
FROM horses
WHERE horse_name = ('" . $rc_row['horse_name'] . "')");
while($horse_row = mysql_fetch_array($horses))
{
// echo horse details here
}
}
echo "</table><br>";
echo "Testing<br>Testing<br>I wish I could put in some horse form here...<br>";
echo "<table width='990' border='1' cellspacing='2' cellpadding='2'>";
}
I theory you could and in practice you can, but a while in a while in a for in a while seems a little bit over the top...
I'm sure we can help you make it more efficient if you explain what it is you're trying to do.
I've got two tables from which I need to extract information, but the data from the second table depends on the information I get from the first one. Is there an easy way to handle this?
<?php
mysql_connect('localhost', 'root', 'root') or die(mysql_error());
mysql_select_db('stadium') or die(mysql_error());
$result = mysql_query("SELECT * FROM events");
$result2 = mysql_query("SELECT name FROM competitions WHERE id='$row[competition_id]' ");
while($row = mysql_fetch_array($result)) {
echo "<tr id=\"" . $row['id'] . "\"> \n<td>" . $row['name'] . "</td>";
echo "<td>" . $row['competition_id'] . "</td>";
echo "<td>" . $row['date'] . "</td></tr>";
}
?>
Use a JOIN.
SELECT e.*, c.name as competition_name FROM events e LEFT JOIN competitions c on c.id = e.competition_id