im having problems with this code
<?php
include '01.php';
include 'header.php';
$sql = "SELECT cat_id, cat_name, cat_description FROM categories WHERE cat_id = " . mysql_real_escape_string($_GET['id']) . "";
$result = mysql_query($sql);
if(!$result)
{
echo 'The category could not be displayed, please try again later.' . mysql_error();
}
else
{
if(mysql_num_rows($result) == 0)
{
echo 'This category does not exist.';
}
else
{
//display category data
while($row = mysql_fetch_assoc($result))
{
echo '<h2>Topics in ′' . $row['cat_name'] . '′ category</h2>';
}
//do a query for the topics
$sql = "SELECT
topic_id,
topic_subject,
topic_date,
topic_cat
FROM
topics
WHERE
topic_cat = " . mysql_real_escape_string($_GET['id']);
$result = mysql_query($sql);
if(!$result)
{
echo 'The topics could not be displayed, please try again later.';
}
else
{
if(mysql_num_rows($result) == 0)
{
echo 'There are no topics in this category yet.';
}
else
{
//prepare the table
echo '<table border="1">
<tr>
<th>Topic</th>
<th>Created at</th>
</tr>';
while($row = mysql_fetch_assoc($result))
{
echo '<tr>';
echo '<td class="leftpart">';
echo '<h3>' . $row['topic_subject'] . '<h3>';
echo '</td>';
echo '<td class="rightpart">';
echo date('d-m-Y', strtotime($row['topic_date']));
echo '</td>';
echo '</tr>';
}
}
}
}
}
include 'footer.php';
?>
the error it gives me is this The category could not be displayed, please try again later.You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 strong text **
$sql = "SELECT cat_id, cat_name, cat_description FROM categories WHERE keeps not working for some reasonstrong text**
would really appreciate the help i dont know what to do anymore googled everywhere and tried different combinations but cant get the id from cat_id please help
seems you don't have proper quote. you should enclose the result of your mysql_real_escape_string($_GET['id']) inside '
like the sample below
"SELECT cat_id, cat_name, cat_description
FROM categories WHERE cat_id = '" . mysql_real_escape_string($_GET['id']) . "'";
if you want try to use a simplified query remember the quotes this way:
$sql= mysql_query("SELECT cat_id, cat_name, cat_description
FROM categories WHERE cat_id = '" .$id . "';");
but for debugging try
var_dump($id);
var_dump($sql);
and check if $id contain a proper value and $sql is correctly formed (try copy the resulting query and execute it in your console)
then if the query give you the right result in console this mean the resulting query is right..
PS the url should be
xxxxx.com/category.php?id=1
remember to assign a value to your id
Related
In my database I have a one-to-many table relationship where one parent can have many kids. The primary key is the parents email. I query to get the kids
$results1 = mysqli_query($con,"
SELECT directory.email
, dirKids.kname
, dirKids.kbirthday
FROM directory
JOIN dirKids
ON '$row[email]' = dirKids.parent
");
Then I loop through and echo the value to my html page
while($row1 = mysqli_fetch_array($results1)) {
if (!empty($row1["kname"])) {
echo "<tr><td>". $row1["kname"] ."</td><td>".
$row1["kbirthday"]."</td></tr>";
}
}
The problem I am having is that only one parent has kids in my database, but it will print the kids name and birthday 10 times because there are 10 people in my database. How can I get it to only print the child's name and birthday once?
My full code is listed below:
<?php
$con = mysqli_connect("localhost", "username", "password", "db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$results = mysqli_query($con,"SELECT directory.id, directory.fname, directory.lname, directory.address, directory.bdname, directory.birthday, directory.cell, directory.email, directory.sFName, directory.sBirthday, directory.sCell, directory.sEmail FROM directory ORDER BY lname") or die ("couldn't fetch query");
echo "<div class='accordion' id='accordion'>";
// output data of each row
while($row = mysqli_fetch_array($results)) {
$results1 = mysqli_query($con,"SELECT directory.email, dirKids.kname, dirKids.kbirthday FROM directory JOIN dirKids ON '$row[email]' = dirKids.parent");
echo "</table></div>";
if ($row['sFName'] == "" || $row['sFName'] == "undefined") {
echo "<div class='card'><div class='card-header'
id='headingOne'><h5 class='mb-0'><button class='btn btn-link'
type='button' data-toggle='collapse' data-target='#collapse".
$row["id"] ."' aria-expanded='true' aria-controls='collapse".
$row["id"] . "'><h5>".$row["fname"] ."<span id='lnameText'>".
$row["lname"] ."</span></h5></button></h5></div><div
id='collapse". $row["id"] . "' class='collapse'
aria-labelledby='headingOne' data-parent='#accordion'><div
class='card-body'><table id='myUL' class='table'><tr></tr><tr>
<td><h5>Address</h5></td><td>". $row["address"] ."</td></tr>
<tr><td><h5>Birthday</h5></td><td>".$row["birthday"]."</td>
</tr><tr><td><h5>Cell</h5></td><td>". $row["cell"]."</td></tr>
<tr><td><h5>Email</h5></td><td>". $row["email"] ."</td></tr>
</table></div>";
echo "<div class='col-md-6'><h3>Children</h3><table class='table'><tr><th><h5>Name</h5></th><th><h5>Birthday</h5></th>";
while($row1 = mysqli_fetch_array($results1)) {
if (!empty($row1["kname"])) {
echo "<tr><td>". $row1["kname"] ."</td><td>". $row1["kbirthday"]."</td></tr>";
}
}
echo "</table></div></div>";
?>
Since the data needed for the second while loop comes exclusively from the kids table, just build your SELECT statement for that, forget the join and the WHERE statement looks for only the parents email.
The below code goes inside the primary while loop and replaces the
$results1 = mysqli_query($con,"SELECT directory.email, dirKids.kname, dirKids.kbirthday FROM directory JOIN dirKids ON '$row[email]' = dirKids.parent");
with
//Build the select statement
$sql = "SELECT kname, kbirthday FROM dirKids WHERE parent = '" .$row[email] . "'";
//now run the query
$results1 = mysqli_query($con,$sql);
//uncomment the below to see the results
//var_dump(mysqli_fetch_array($results1));
Your query should look like this;
$select = mysqli_query($db, "SELECT * FROM parents_database WHERE parent_name = '$parent_name'");
while ($row = mysqli_fetch_array($select, MYSQLI_ASSOC)) {
// echo kids here..
}
Not sure what do you need. Since you posted 2 different queries.
But 1st one has wrong approach, hope you need to fix that one.
I think you've meant something like:
SELECT directory.email
, dirKids.kname
, dirKids.kbirthday
FROM directory
JOIN dirKids
ON directory.email = dirKids.parent
WHERE directory.email = '$row[email]'
I am trying to individually print out each line from my SQL database in PHP. I am trying to do this so each line that is retrieved, it can act like a link which will direct the user to another page. For example, the current SQL query will output the Category names from the database Category, i would like it to output all the values from that table but have it so each one has a different redirect link to another page which clicked on.
$query = "SELECT CATEGORY_NAME
FROM CATEGORIES ORDER BY CATEGORY_ID ASC";
$results = #mysqli_query ($conn, $query);
$numrows = mysqli_num_rows($results);
if ($results) {
if ($numrows >0) {
echo '
<table>
<tr>
<td><strong>Categories</stong></td>
</tr>';
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '<tr>
<td>' . $row['CATEGORY_NAME'] . '</td>
</tr>
';
}
mysqli_free_result ($results);
Such as,
Category
__________
PS4
XBOX
i can click on PS4 and it would take me to another page, i know how to do this with a href and then print out the row in sql however, i'm not sure how do print out each row individually without printing them out using $row['CATEGORY_NAME'].
Thank you for any help
You could just store the link to which it needs to be redirected in a new column CATEGORY_URL along with the CATEGORY_NAME and then print it out like so:
$query = "SELECT CATEGORY_NAME, CATEGORY_URL FROM CATEGORIES ORDER BY CATEGORY_ID ASC";
$results = mysqli_query($conn, $query);
$numrows = mysqli_num_rows($results);
if ($numrows > 0) {
echo '
<table>
<tr>
<td><strong>Categories</stong></td>
</tr>';
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '
<tr>
<td>
'.$row['CATEGORY_NAME'].'
</td>
</tr>';
}
mysqli_free_result ($results);
} else {
// no results found
}
OR if you just want to pass it as a url parameter, you just need to modify if like so:
'.$row['CATEGORY_NAME'].'
OR
'.$row['CATEGORY_NAME'].'
Something like? (I'm not 100% sure if I understand your question)
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '<tr>
<td><a href="somelink.php?cat=' . $row['CATEGORY_NAME'] . '">' .
$row['CATEGORY_NAME'] . '</a></td>
</tr> ';
}
UPDATE
If a category is named playstation, playstation.php would be linked to...
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '<tr>
<td><a href="' . $row['CATEGORY_NAME'] . '.php">' .
$row['CATEGORY_NAME'] . '</a></td>
</tr> ';
}
Though above would work a wild guess is that you're looking for something completely different. The thing you are looking for is essentially a way to present information for playstation, a way to present information about PS4 etc depending on what user clicks on!? In that case you should do something like this instead:
//Include ID of category in your sql-statement
$query = "SELECT ID, CATEGORY_NAME
FROM CATEGORIES ORDER BY CATEGORY_ID ASC";
....
....
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '<tr>
<td><a href="presentinfo.php?id=' . $row['ID'] . '">' .
$row['CATEGORY_NAME'] . '</a></td>
</tr> ';
}
and create presentinfo.php file where you fetch information about the category, based on the id given in the url. (e.g.select from categories id={id given in url})
I have had a long road to get to this last question. Everything is my code is working now, but I can't get this last little issue. Right now I have:
$sql = "SELECT phonenumber,email, dataplan AS currentplan, SUM(datamb) AS
value_sum FROM maindata GROUP BY phonenumber, dataplan";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$val = $row["value_sum"];
$plan = $row["currentplan"];
$remain = $plan - $val;
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
It only subtracts the first value as opposed to the values for all. displayed like this:
while ($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$row['phonenumber'] . "</td> ";
echo "<td>".$row['currentplan'] . "</td> ";
echo "<td>".ROUND ($row["value_sum"],2) . "MB</td> ";
echo "<td>".$remain . " MB</td> ";
echo "<td>".$row['email'] . "</td></tr>";
}
So my goal is to subtract all value_sums from all dataplans, but what I have now, gives me the first value for all columns. Thank you!
mysql_fetch_assoc() will always get one row. You can use it in loop, or better use PDO, eg. like this:
$sql = "SELECT phonenumber,email, dataplan AS currentplan, SUM(datamb) AS
value_sum FROM maindata GROUP BY phonenumber, dataplan";
$results = $pdo->query($sql);
You can read about creating PDO connections here http://www.php.net/manual/en/book.pdo.php
Hi I have trying to learn php by writing little web app for showing me sales data. I have got a query which i now works as i have tested it but i want it to echo the datematched and the number of rows/results found with that date. This is what I have so far
<?php
$con=mysqli_connect("host","user","password","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM matched WHERE datematched IN (
SELECT datematched FROM matched GROUP BY datematched HAVING count(*) > 1");
while($row = mysqli_fetch_array($result))
{
echo "['";
echo "" . $date['datematched'] . "', ";
echo "" . $num_rows . "],";
}
mysqli_close($con);
?>
I know i am doing something wrong here. ryan
EDIT:
<?php
$con=mysqli_connect("host","user","password","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM matched WHERE datematched IN (
SELECT datematched FROM matched GROUP BY datematched HAVING count(*) > 1");
echo "['";
echo " 16/08/2013 ', ";
echo "12345}],";
mysqli_close($con);
?>
Okay i have just checked my echo and they work i put in some data so all i need is to find a way of getting the information of the datematched that has been found and then the number of rows that has been found with that. Thanks Ryan
first of all you need to make an adjustment to your query, so that it has the number of rows your expecting.
$result = mysqli_query($con,"SELECT datematched, COUNT(*) as num_rows "
. "FROM matched GROUP BY datematched HAVING num_rows > 0");
then you can display the data as follows
while($row = mysqli_fetch_array($result))
{
echo $row['datematched'] . ",";
echo $row['num_rows'];
}
if your sql query is perfect then you should write like this wayt
while($row = mysqli_fetch_array($result))
{
echo "['";
echo "" . $row['datematched'] . "', ";
echo "" . $row['num_rows'] . "', ";
}
please set your column as you got in your mysql query.
<?php
$query=mysqli_query($con,"SELECT datematched FROM matched GROUP BY datematched");
$num=mysqli_num_rows($query);
if($num>1)
{
$result = mysqli_query($con,"SELECT * FROM matched");
$num_rows=mysqli_num_rows($result);
while($row = mysqli_fetch_array($result))
{
echo '['; echo $row['datematched']; echo $num_rows; echo ']';
}
}
I have this query for counting the number of items in a category:
SELECT category, COUNT(*) AS category_count FROM users GROUP BY category
Which creates results looking like:
category category_count
========== ================
X 3
Y 2
Now, In PHP I want to display the counts of the categories. For example, I might want to echo the count from category X, how would I do it?
Thanks in advance
Assuming $result holds the result of your query:
while ($row = mysql_fetch_array($result))
{
echo 'Category: ' . $row['category'];
if ($row['category'] == 'X')
{
echo ' Count: ' . $row['category_count'];
}
echo '<br/>';
}
$res = mysql_query("SELECT category, COUNT(*) AS category_count FROM users GROUP BY category");
while($row = mysql_fetch_assoc($res)){
echo $row['category'].": ".$row['category_count']."<br/>";
}
$result = mysql_query("SELECT category, COUNT(*) AS category_count FROM users GROUP BY category");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
{
if ( $row['category'] == 'x' )
{
echo $row['category_count'];
}
}
while ($row = mysql_fetch_array($result))
{
echo 'Category: ' . $row['category'] . ' Count:' . $row['category_count'];
echo "<br>";
}
It would be better if you use the where clause in your query.
SELECT COUNT(*) AS category_count FROM users WHERE category = 'x'
$conn = mysql_connect("address","login","pas s");
mysql_select_db("database", $conn);
$Var = mysql_query("query");
While ($row = mysql_fetch_assoc($var) { echo $var["column"] }.