<h1>Matches</h1>
<?php
$con=mysqli_connect("localhost","root","","esports");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM NA");
while($row = mysqli_fetch_array($result))
{
echo $row['team1'] . " vs. " . $row['team2'];
echo "<br>";
}
mysqli_close($con);
?>
I'm trying to use this to get information from my MySQL Database and then display it onto a webpage using an HTML Table. I'm kind of new to this, so how would I go about doing this? Any help is appreciated.
To connect to MySQL database you need to use mysql_connect and select the database with mysql_select_db and finally do a mysql_fetch_array
<?php
$con =mysql_connect("localhost","root","","esports");
mysql_select_db('your data base');
$result = mysql_query($con,"SELECT * FROM NA");
while($row = mysql_fetch_array($result))
{
echo '<pre>';
print_r( $row);
echo '</pre>';
}
mysql_close($con);
?>
There doesn't seem to be any problem with your query, so if you want to just format it as a table, you could do
echo "<table border=1>";
while ($row = mysql_fetch_array($result)
{
echo '<tr><td>';
echo $row['team1'];
echo '</td><td>';
echo $row['team2'];
echo '</td></tr>';
}
mysql_close($con);
Related
I am trying to display my query results on page. However, whenever I run the code although the query is correct it does not display anything.
Can anyone help? Would be muchly appreciated
<?php
session_start();
require_once("../config1.php");
if(isset($_POST["DailySales"])) {
$linkid = mysqli_connect(DB_DATA_SOURCE, DB_USERNAME, DB_PASSWORD, DB_DATABASE) or die("Could not connect:" . connect_error());
$sql = "SELECT Retdatetime AS date , sum(rentalrate + overduecharge) AS mny
FROM frs_FilmRental
WHERE shopid='2'
Order BY retdatetime DESC ";
$result = mysqli_query($linkid, $sql);
if (!$result)) {
printf("Errormessage: %s\n", mysqli_error($linkid));
}
echo "<table border = '1' align='center'>";
echo "<th> Shop ID 2</th></tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<h2><center>Shop ID 2 daily sales : </center></h2>";
echo "<tr><td>";
echo $row['mny'];
echo "</td><td>";
echo $row ['date'];
echo "</td></tr>";
}
}
?>
replace
echo $row ['date'];
by
echo $row ['Retdatetime'];
To understand query errors you should use mysqli_error() in your code. If there are no errors for executed query, then you can run while loop for it.
<?php
session_start();
require_once("../config1.php");
if(isset($_POST["DailySales"])) {
$linkid = mysqli_connect(DB_DATA_SOURCE, DB_USERNAME, DB_PASSWORD, DB_DATABASE) or die("Could not connect:" . connect_error());
$sql = "SELECT Retdatetime , sum(rentalrate + overduecharge) AS mny
FROM frs_FilmRental
WHERE shopid='2'
Order BY retdatetime DESC ";
$result = mysqli_query($linkid, $sql);
if (!$result)) {
printf("Errormessage: %s\n", mysqli_error($linkid));
} else {
echo "<table border = '1' align='center'>";
echo "<tr><th> Shop ID 2</th></tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<h2><center>Shop ID 2 daily sales : </center></h2>";
echo "<tr><td>";
echo $row['mny'];
echo "</td><td>";
echo $row ['date'];
echo "</td></tr>";
}
echo "</table>";
}
}
?>
fixes given in comments also applied
Please copy/paste the error, given by mysqli_error()
I am trying to figure out how to use this in a loop. Any help will be appreciated.
$conn = mysql_connect("localhost", "some_user", "password");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
if (!mysql_select_db("some_db")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$sql = "SELECT favid FROM ajaxfavourites";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo $row["favid"];
}
mysql_free_result($result);
Currently it displays results as:
116677889922
I need them to show them as (the way they are displayed in DB):
1166
7788
9922
PS I am aware that this function is deprecated, I am just trying to fix one of my older sites.
choose One of these ways:
echo $row["favid"]."<br>";
echo $row["favid"]."\n";
echo $row["favid"].PHP_EOL;
while ($row = mysql_fetch_assoc($result)) {
echo $row["favid"];
echo "\r\n";
}
You can simply echo the value with '<br/>' or '<p>' like following:
while ($row = mysql_fetch_assoc($result)) {
echo $row["favid"] . '<br/>';
}
OR
while ($row = mysql_fetch_assoc($result)) {
echo '<p>' . $row["favid"] . '</p>';
}
Also you can just put all favid into array and then in another loop, can customize how to show them, like following:
while ($row = mysql_fetch_assoc($result)) {
$ids[] = $row["favid"];
}
foreach($ids AS $idv) {
echo '<p>' . $idv . '</p>';
}
I have used the below code in my website,
<?php
$con= mysqli_connect("*******","******","*****", "catalejo_articles");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result= mysqli_query($con, "SELECT * FROM baul");
while($row = mysqli_fetch_array($result))
{
echo $row['title'] . " " . $row['date'];
}
mysqli_close($con);
?>
and it is not working. What am i doing wrong??
I am new to php and mysql, any help will be appreciated.
UPDATE:
I want to thank and apologize to all of you who spent precious time trying to help me. I just solved the problem, the original code was OK. The problem was I didn't change the file extension to PHP.
Make sure display error is enabled. add this line at the top of your page and see if it display any error :
error_reporting(-1);
And try inside while loop :
var_dump($row)
Does table contains data?
Try this way,
if ($result = mysqli_query($con, "SELECT * FROM baul", MYSQLI_USE_RESULT)) {
while($row = mysqli_fetch_array($result))
{
echo $row['title'] . " " . $row['date'];
}
mysqli_free_result($result);
}
Check if u have any records in your table using mysqli_num_rows
$con= mysqli_connect("*******","******","*****", "catalejo_articles");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result= mysqli_query($con, "SELECT * FROM baul");
$count = mysqli_num_rows($result);
if($count > 0)
{
while($row = mysqli_fetch_array($result))
{
echo $row['title'] . " " . $row['date'];
}
}else{
echo "No Records found in the table";
}
mysqli_close($con);
From what I can make out of your code, since you were attempting to reference by association instead of by numerical index, you weren't seeing anything because you were missing MYSQLI_ASSOC in your while loop:
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo $row['title']." ".$row['date'];
}
Otherwise you need to know where "title" and "date" columns are located and reference them by numerical value:
while($row = mysqli_fetch_array($result)) {
echo $row[0]." ".$row[1];
}
Alternatively, if mysqli_fetch_array is not working (due to PHP version), try using:
while($row = mysqli_fetch_assoc($result)) {
echo $row['title'].' '.$row['date'];
}
Below is my code, and it's working fine. However, how can I change from selection to say TEXT Field so as one can type, while the prioritized are listed down as in combo box?
<?php
$con=mysqli_connect("localhost","root","","databasse");
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
echo "Select Region: ";
echo "<select name='RegName' id='RegName'>";
$query = "SELECT * FROM rtable";
$result = mysqli_query($con,$query);
//echo "<option value='userid'>All</option>";
while($row = mysqli_fetch_array($result)) {
echo "<option value='RegName'>".$row['RegName']." </option>";
}
echo "</select>";
?>
Updated script with proper field names. Why isnt this working?
<?php
$con = mysql_connect("localhost","root","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("bookorama", $con);
$sql="SELECT * FROM customers";
$result = mysql_query($sql); // You actually have to execute the $sql with mysql_query();
echo "<table>"; //start the table
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) //Loop through the results
{
//echo each row of the table
echo "<tr>
<td>$row['customerID']</td>
<td>$row['name']</td>
<td>$row['Aaddress']</td>
<td>$row['city']</td>
</tr>";
}
echo '</table>'; //close out the table
?>
<?php
$con = mysql_connect("localhost","root","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("bookorama", $con);
$sql="SELECT * FROM customers";
$result = mysql_query($sql); // You actually have to execute the $sql with mysql_query();
echo "<table>"; //start the table
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) //Loop through the results
{
//echo each row of the table
echo "<tr>";
echo "<td>$row['CustomerID']</td>";
echo "<td>$row['address']</td>";
echo "<td>$row['city']</td>";
echo "</tr>";
}
echo '</table>'; //close out the table
?>
You can mysql_fetch_array or mysql_fetch_assoc to retriever the rows from you query.
For example using mysql_fetch_array:
$result = mysql_query($sql);
echo "<table><tbody>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td>".$row[0] . "</td><td>" . $row[1] . "</td></tr>";
}
echo "</tbody></table>"
You need to run the query and loop through the results.
You are better off learning from day one about PDO.
And also don't interpolate directly from any user submitted variables (that includes $_POST).
Pretty sure you need to do this when embedding anything more complex than scalars inside double quotes
echo "<tr>
<td>{$row['CustomerID']}</td>
<td>{$row['address']}</td>
<td>{$row['city']}</td>
</tr>";
So whenever you have a more complex var name than "test $var" in quotes, wrap with {} - and even then, it's good practice to wrap scalars too like "test {$var}"