WebSite X5Help Center

 
Errol W.
Errol W.
User

Php coding issue while using join  en

Autor: Errol W.
Visitado 1157, Seguidores 1, Compartilhado 0  

When I run the code using the INNER JOIN, I get http 500 error.

Here is the code:

$servername = "******";
$username = "******";
$password = "******";
$dbname = "******";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//$sql = "SELECT order_id,field_id, label, value FROM cartinvoice_addresses";
// Here is where I am having and issue
$sql = "SELECT order_id,field_id, label, value,price,name
FROM cartinvoice_addresses
INNER JOIN ON cartinvoice_addresses.order_id = cartproducts.order_id
//
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if( $row["field_id"] == "Address1"){ echo "

"; }
echo " " . $row["value"].$row["name"];
//Added code

}
} else {
echo "0 results";
}

Publicado em
19 RESPOSTAS - 1 CORRIGIR
Paul M.
Paul M.
Moderator

Hello Errol,

I've edited your original post to obscure your database connection credentials...  it's best not to post these on a public forum for security reasons, for your own sake.

There were some errors in your PHP syntax.  The revised code is as below:

<?php


$servername = "******";
$username = "******";
$password = "******";
$dbname = "******";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}


//$sql = "SELECT order_id,field_id, label, value FROM cartinvoice_addresses";
// Here is where I am having and issue$sql = "SELECT order_id,field_id, label, value,price,name FROM cartinvoice_addresses INNER JOIN ON cartinvoice_addresses.order_id = cartproducts.order_id";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {if ($row["field_id"] == "Address1"){echo "<br><br>";}

echo " " . $row["value"].$row["name"];}

}


//Added code

else {echo "0 results";}


?>

However, as I'm unsure what you are attempting to do with the database data, I can't comment (for the time being anyway) on your SELECT-JOIN clause.  I would expect a table name immediately after 'INNER JOIN' but preceding 'ON'

Kind regards,

Paul

Search the WebSite X5 Help Center

Ler mais
Publicado em de Paul M.
Errol W.
Errol W.
User
Autor

In regards to the credentials, they were incorrect. But Thanks. My plan is to get data from two tables, Then print one row with the combined data. When I work with one table all is well.

Ler mais
Publicado em de Errol W.
Errol W.
Errol W.
User
Autor

Hello Paul,

I added the table as you suggested, I got the same result http 500.

<?php
$servername = "localhost";
$username = "Delta";
$password = "Dst1913";
$dbname = "BuyTicket";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//$sql = "SELECT order_id,field_id, label, value FROM cartinvoice_addresses";
//$sql = "SELECT order_id,field_id, label, value FROM cartinvoice_addresses";
// Here is where I am having and issue
$sql = "SELECT order_id field_id, label, value, price, name
FROM cartinvoice_addresses
INNER JOIN cartinvoice_addresses ON cartinvoice_addresses.order_id = cartproducts.order_id;
//
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if( $row["field_id"] == "Address1"){ echo "<br><br>"; }
echo " " . $row["value"].$row["label"].$row["price"].$row["name"];
//Added code

}
} else {
echo "0 results";
}
$conn->close();
?>

Ler mais
Publicado em de Errol W.
Paul M.
Paul M.
Moderator

Can you be as specific as possible about exactly which data you want to extract from the database?  You'll need very specific syntax in your SELECT statement.  The SELECT statement in the code above is not properly formed.

For example, do you want to take the entire data from both tables and display it in one row?

Or do you want to take only one or two columns from one table in the database, and amalgamate these with one or two columns from another table in the database?  If so, which columns and which tables?

Or do you want to join data in a single row only if certain data in columns in both tables are identical?

Or something else?

Ler mais
Publicado em de Paul M.
John S.
John S.
User

Hello Errol

If you are interested in direct help, you can contact me here : http://calendarforum.dk/contact.html

As this is not really a X5 issue and as the forum is not suitable for assistance like this, I suggest you contact me or another person that will assist, directly.

As said; I have made this before :

here : http://eksempelsite.dk/udstillinger/Emner.php  two tables are joined and showed in a html-table.

Here : https://mogenshansen49.dk/det-gamle-testamente.html where the output is integrated in a X5 page. 

Ler mais
Publicado em de John S.
John S.
John S.
User

And - if your database can be accessed from outside your domain, it is as Paul earlier told, not very smart to expose connection settings.

Ler mais
Publicado em de John S.
Errol W.
Errol W.
User
Autor

Hello Paul,

This is the option I want.

Or do you want to take only one or two columns from one table in the database, and amalgamate these with one or two columns from another table in the database?  If so, which columns and which tables?

Here is what I am using now with no http 500 error.  But my results are 0. So something is logically incorrect.

<?php
$servername = "xxxxxx";
$username = "xxxxxxx";
$password = "xxxxxxx";
$dbname = "xxxxxxxxx";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT order_id,field_id, label, value, price, name FROM cartinvoice_addresses as table1
INNER JOIN cartproducts as table2 ON table1.order_id=table2.order_id
";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
// if( $row["field_id"] == "Address1"){ echo "<br><br>"; }
echo " " . $row["lable"].$row["value"].$row[price].$row[price];
//Added code

}
} else {
echo "0 results";
}
$conn->close();
?>

Ler mais
Publicado em de Errol W.
Errol W.
Errol W.
User
Autor

In addition here is what in the error log.

[08-Nov-2020 18:51:21 UTC] PHP Notice: Trying to get property 'num_rows' of non-object in /home/j8ge6f324p1c/public_html/php/test.php on line 20 [08-Nov-2020 18:56:04 UTC] PHP Notice: Trying to get property 'num_rows' of non-object in /home/j8ge6f324p1c/public_html/php/test.php on line 20 [08-Nov-2020 19:01:21 UTC] PHP Notice: Trying to get property 'num_rows' of non-object in /home/j8ge6f324p1c/public_html/php/test.php on line 20 [08-Nov-2020 19:07:17 UTC] PHP Notice: Trying to get property 'num_rows' of non-object in /home/j8ge6f324p1c/public_html/php/test.php on line 20 [08-Nov-2020 19:10:53 UTC] PHP Notice: Trying to get property 'num_rows' of non-object in /home/j8ge6f324p1c/public_html/php/test.php on line 20 [08-Nov-2020 19:20:16 UTC] PHP Notice: Trying to get property 'num_rows' of non-object in /home/j8ge6f324p1c/public_html/php/test.php on line 20

Ler mais
Publicado em de Errol W.
Paul M.
Paul M.
Moderator

Is this what you are looking for, Errol?

<?php
$servername = "xxxxxx";
$username = "xxxxxx";
$password = "xxxxxx";
$dbname = "xxxxxx";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT order_id, field_id, label, value FROM cartinvoice_addresses INNER JOIN cartproducts USING (order_id);";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
// if( $row["field_id"] == "Address1"){ echo "<br><br>"; }
echo " " . $row["lable"].$row["value"].$row[price].$row[price];
//Added code

}
} else {
echo "0 results";
}
$conn->close();
?>

Ler mais
Publicado em de Paul M.
Errol W.
Errol W.
User
Autor

Paul,

I tried that, it's showing me the two fields from cartproducts price and name

echo " " . $row["lable"].$row["value"].$row[price].$row[name];

Ler mais
Publicado em de Errol W.
Errol W.
Errol W.
User
Autor

I tried that, it's not showing me the two fields from cartproducts price and name

echo " " . $row["lable"].$row["value"].$row[price].$row[name];

Ler mais
Publicado em de Errol W.
Errol W.
Errol W.
User
Autor

One other thing. It's not giving me the line break if field_id==Address1 is true.

Ler mais
Publicado em de Errol W.
Paul M.
Paul M.
Moderator
Errol W.
I tried that, it's showing me the two fields from cartproducts price and name

What do you want it to show?  Can you be as specific as possible, please?  Can you give the names of the columns that you want from the first table (which I assume is cartinvoice_addresses) along with the names of the columns that you want from the second table (assumed to be cartproducts).

Ler mais
Publicado em de Paul M.
Errol W.
Errol W.
User
Autor

Update I fix the price and name issue. So now it's just the line break thing.

Ler mais
Publicado em de Errol W.
Paul M.
Paul M.
Moderator

The line in question seems to be commented out:

// if( $row["field_id"] == "Address1"){ echo "<br><br>"; }

If you remove the two forward slashes from the start of the line that should sort it.

Ler mais
Publicado em de Paul M.
Errol W.
Errol W.
User
Autor

So... from cartinvoice_addresses I want to show label and value. From cartproducts I want to show price and name. And every instance of field_id=Address1, I need line break

Ler mais
Publicado em de Errol W.
Errol W.
Errol W.
User
Autor

I found the problem with the line break. I had the code commented out //.

Ler mais
Publicado em de Errol W.
Paul M.
Paul M.
Moderator

Thanks for the specific indication of the data you required, Errol...  that was the information I needed.

<?php

$servername = "xxxxxx";
$username = "xxxxxx";
$password = "xxxxxx";
$dbname = "xxxxxx";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {die("Connection failed: " . mysqli_connect_error());}

$sql = "SELECT cartinvoice_addresses.field_id, cartinvoice_addresses.label, cartinvoice_addresses.value, cartproducts.name, cartproducts.price FROM cartinvoice_addresses INNER JOIN cartproducts ON cartinvoice_addresses.order_id = cartproducts.order_id;";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if ($row["field_id"] == "Address1") {echo "<br><br>"; echo "Product name: ".$row["name"]." Price: ".$row["price"];}
echo " ".$row["label"].":"." ".$row["value"];}
}
else {echo "0 results";}

$conn->close();

?>

Ler mais
Publicado em de Paul M.
Errol W.
Errol W.
User
Autor

Hello Paul, Thanks a bunch. It works.

Ler mais
Publicado em de Errol W.