Jump to content

Problem displaying data from MySQL


Go to solution Solved by rocky48,

Recommended Posts

I am querying my database to show the visit statistics for a particular week and it shows the number of visits for the countries, but does not display the country name.

I have proved that the MySQL works by going into phpMyAdmin and pasting the query into SQL query tab, replacing the POST with 1, for week 1.

 

I can't see why it is not displying the country.

 

Here is the code:

<?php
include('connect_visits.php');
doDB7();

$WVisit_data="SELECT WeekNo15.WNo, WeekNo15.WCom, Countries.Country, Countries.CID, ctryvisits15.CVisits
        FROM ctryvisits15 
        LEFT JOIN Countries
            ON ctryvisits15.country = Countries.CID
        LEFT JOIN WeekNo15
            ON ctryvisits15.WNo = WeekNo15.WNo
        WHERE ctryvisits15.WNo = '{$_POST['WeekNo']}'
        ORDER BY ctryvisits15.CVisits DESC";

$WVisit_data_res = mysqli_query($mysqli, $WVisit_data) or die(mysqli_error($mysqli));
$display_block ="
<table width=\"20%\" cellpadding=\"3\" cellspacing=\"1\" border=\"1\" BGCOLOR=\"white\" >
<tr>
<th>Country</th>
<th>Visits</>
</tr>";
while ($WV_info = mysqli_fetch_array($WVisit_data_res)){
    $Ctry = $WV_info['country'];
    $Visits = $WV_info['CVisits'];
//add to display
$display_block .="
<tr>
<td width=\"10%\" valign=\"top\">".$Ctry."<br/></td>
<td width=\"5%\" valign=\"top\">".$Visits."<br/></td>
";
}
mysqli_free_result($WVisit_data_res);
mysqli_close($mysqli);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Design by Free CSS Templates
http://www.freecsstemplates.org
Released for free under a Creative Commons Attribution 2.5 License

Name       : Yosemite  
Description: A two-column, fixed-width design with dark color scheme.
Version    : 1.0
Released   : 20091106

-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>1066 Cards 4U - Stats for country</title>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="wrapper">
<div id="menu">
		<ul>
			<li class="current_page_item"><a href="index.php">Home</a></li>
			<li><a href="Links.html">Links</a></li>
			<li><a href="Verse_Menu.html">Verses</a></li>
			<li><a href="Techniques.html">Techniques</a></li>
			<li><a href="blog.php">Blog</a></li>
			<li><a href="Gallery.html">Gallery</a></li>
			<li><a href="contact.html">Contact</a></li>
			<li><a href="AboutUs.html">About Us</a></li>
			<li><a href="stats1.html">Stats</a></li>
		</ul>
</div><!-- end #menu -->
<div id="header">
		<div id="logo">
			<h1><a href="http://www.1066cards4u.co.uk">1066 Cards 4U</a></h1>
</div><!-- end #wrapper -->
</div><!-- end #header -->
<div id="page">
<div id="page-bgtop">
<div id="page-bgbtm">
<div id="content">
<h3>Statistics for Week Commencing <? echo $WkCom; ?> in 2015</h3>

<div id="table">
<?php echo $display_block; ?></div>
</div><!-- end #content -->
</body>
</html>

Can you help please?

Edited by rocky48
Link to comment
https://forums.phpfreaks.com/topic/293724-problem-displaying-data-from-mysql/
Share on other sites

  • Solution

Your observation alone did not solve the problem.

I noticed that the field ctryvisits.country was not in the SELECT query.

After adding that it worked.

Thanks very much!

when you are working on the same code sometimes you miss the most obvious.

You don't have to select a field that you are joining on, unless you also want to output it. Naturally, if you want to output a field it needs to be selected.

 

Other cases vary with flavour of SQL. With MySQL you can GROUP BY or ORDER BY a field that isn't selected but others, like MS SQL SERVER, are less lenient and adhere to the standard and the fields must be selected.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.