Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am trying to get this code to do else if there are no more rows to display but not working could anyone point me in the right direction for fixing this.
if($mymethod == "LOADGEMINVENTORY")
{
$result = mysqli_query($con,"SELECT * FROM gemshop_table LIMIT $startrow, 6")or
die(mysql_error());//Pulles six items at a time to display.
if(!mysql_error()){ echo ("DISPLAYING6OFINVENTORY^");
while($row = mysqli_fetch_array($result))
{
echo $row['id'];echo (",");echo $row['image'];echo ("^");
}
}else{echo ("NOINVENTORY");}
}
You're mixing mysqli with mysql. You should really use one of these in your code.
If you're using mysqli, you can do the following to get number of rows:
$row_count = $result->num_rows;
if( $row_count == 0 ) {
echo "NOINVENTORY";
}
These is no while/else condition in PHP (or most programming language that I know of).
while() will automatically loop through the data until the end. So in your case, if you want to show something after all the data has been retrieved, you would just show it after the while() loop.
while($row = mysqli_fetch_array($result))
{
echo $row['id'];echo (",");echo $row['image'];echo ("^");
}
echo "Show anything you want after the data above.";
However, it seems to me that you want to show the records when you have data, and NOINVENTORY when there's none. For that, use if() statement with num_rows
if(!mysql_error())
{
if (0 == mysqli_num_rows ($result))
{
echo "NOINVENTORY";
}
else
{
while($row = mysqli_fetch_array($result))
{
echo $row['id'];echo (",");echo $row['image'];echo ("^");
}
}
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 11 months ago.
Improve this question
I have been trying to insert into my database but it inserts with a random behavior. Although, the code runs correctly on PHP Version 7., I'm having problems with running it on PHP Version 8.1.2.
Sometimes it inserts correctly, most times it inserts twice. That's my only challenge actually. I also tried to fix by reporting errors using || ob_start(); ini_set('display_errors', 1); || and also disabled the javascript alert but no error was displayed. I'd appreciate it if you could look at the code a little closer. I checked the error logs and there's no problem.
if(isset($_POST['submit_sales'])){
$prd = $_POST['prd'];
$count = count($prd);
$rand_sales = rand();
$cust_name = check_input($_POST['custname']);
$cust_num = check_input($_POST['custnumber']);
// echo $count;
// Looping all files
if($count<1){
// $error = 'Cart is empty';
echo '<script>alert("Cart is empty !!!");window.location="all_products.php";</script>';
// echo '<script>window.location="all_products.php";</script>';
}else{
for($i=0;$i<$count;$i++){
$products = $_POST['prd'][$i];
$price = $_POST['price'][$i];
$qty = $_POST['qty'][$i];
$fl= dbconnect()->prepare("INSERT INTO sales SET cust_name=:custname, cust_number=:custnumber, prd_name=:prd, qty=:qty, price=:price, ref_code=:random");
$fl->bindParam(':custname', $cust_name);
$fl->bindParam(':custnumber', $cust_num);
$fl->bindParam(':prd', $products);
$fl->bindParam(':qty', $qty);
$fl->bindParam(':price', $price);
$fl->bindParam(':random', $rand_sales);
$fl->execute();
}
if($fl){
echo "<script>alert('doneit');window.location='all_products.php';</script>";
}else {
echo "<script>alert('Error Inserting Into Database')</script>";
}
}
}
I had included a script to reload the page in my header hence the double inserting, upon discarding the script, my problem was fixed. Thank you all for helping me with my challenge
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to build a forum with PHP and PDO at the moment and I have a link which takes you to the page for a category with the categories ID in the URL (eg. WEBSITE/category.php?id=1). When I get there, I want to display the name of the category you are looking at using this $_GET information, but it won't seem to do it for me.
Here is what I've got:
<?php
include 'dbconfig.php';
include 'header.php';
$sql = "SELECT cat_id, cat_name, cat_description FROM categories WHERE cat_id = " . $_GET['id'];
$query = $DB_con->prepare($sql);
$query->execute();
$numRows = $query->fetchColumn();
if (!$query) {
echo 'Something went wrong whilst getting the category from the database.';
} else {
if ($numRows == 0) {
echo 'Sorry, this category does not exist';
} else {
while($catRow = $query->fetch(PDO::FETCH_ASSOC)){
echo $catRow['cat_name'];
}
}
}
include 'footer.php';
?>
So as you can see, I have tried to make a while loop that creates an array using PDO::FETCH_ASSOC allowing me to print category details, but when I go to the page nothing shows up except the header.php and the footer.php. There also aren't any errors that come up. Can anybody see what I'm doing wrong? Or let me know if there's information that I have left out. Thanks.
The problem is with $numRows PDOStatement::fetchColumn does not count the rows that are in the result set. There is PDOStatement::rowCount for that.
As for the sql injection, It is not so much the class or functions you use that make it save, but the way you use the functions. To read more about it go here (link was in the top link in related for me)
Applying what we just learned to your code will give us something like this:
$sql = "SELECT cat_id, cat_name, cat_description FROM categories WHERE cat_id = :id"; // Parameterize query to prevent sql injection
$query = $DB_con->prepare($sql);
$query->execute([":id" => $_GET['id']]); // Binding parameter(s), could also be done using bindParam
$results = $query->fetchAll(PDO::FETCH_ASSOC); // For later use
$numRows = $query->rowCount();
if ($query->errorCode() == "00000") { // I don't think that PDOStatement ever fails to be created, so $query would never not be set
echo 'Something went wrong whilst getting the category from the database.';
} else {
if ($numRows == 0) {
echo 'Sorry, this category does not exist';
} else {
foreach ($results as $category){
echo $category['cat_name'];
}
}
}
Note that the way I bind my parameters (in the execute) is my preferred way, not nessesarily the best way
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm newbie so I hope u'll help me. I need to update my SQL with this PHP form. I googled a lot, but problem is i have name in variable:
($rowclanky["id"])
And simply just can't handle it... Tried a lot of things like adding slashes, etc. But I still couldn't make it. Can someone help me how to figure this out? Thanks a lot guys and sorry for my English.
<form method='POST'>;
$sql = "SELECT id,text FROM clanky";
$result = $con->query($sql);
if ($result->num_rows > 0){
while($row = $result->fetch_assoc()) {
echo "<input type='text' name=".$row["id"]." value='".$row["text"]."'><br>";
}
}
echo "<input type='submit' name='send' value='Upravit'><br> </form>;
}
You have not declared the $rowclacky variable. Try the following:
while ($row = $result->fetch_assoc()) {
echo "<input type='text' name=".$row["id"]." value='".$row["text"]."'><br>";
}
notice that you are setting the results from "fetch-assoc()" to the variable $row. So when you try to access the columns in you need to get them from the $row variable.
Hopefully this helps.
$rowclanky is not defined anywhere
$rowclanky["id"]
should be
$row["id"]
And
$rowclanky["text"]
should be
$row["text"]
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to print a list of links, this means I need to use a href in the 3rd line, but I don't know how the syntax should be in this situation. Also, the link is a PHP variable.
$sql = mysql_query("select nm_linha from linhas where cidades_origem like '%$cod_cidades%'");
while($exibe = mysql_fetch_assoc($sql)){
echo $exibe['nm_linha'] .'<br>';
}
$sql = mysql_query("select nm_linha from linhas where cidades_origem like '%$cod_cidades%'");
while($exibe = mysql_fetch_assoc($sql)){
echo "<a href='link.html'>".$exibe['nm_linha'] ."</a><br>";
}
$sql = mysql_query("select nm_linha from linhas where cidades_origem like '%$cod_cidades%'");
while($exibe = mysql_fetch_assoc($sql)){
echo '<a href='".$exibe['nm_linha']."'>'.$exibe['nm_linha'].'</a><br>';
}
this is going to list the links as well as , on clicking it is redirected to th respective link.
try this.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Hello guys i don't know what is call if or switch statement.
i have database table field which is call price, so i want to make if the price if the price field empty its show "please Call us for the price " and if the price field is not its i will show what ever i put it..
here is my code but i don't know why its not working..
any body can help please
thanks you
<?php
$result = mysql_query("SELECT * FROM articles where articlefriendlyURL='%s'",mysql_real_escape_string($aid));
while($row = mysql_fetch_assoc($result)) {
if ($row['price']; = = '') {
echo ("Please Call Us for the price");
else {
echo $row_getArticle['price'];
}
}
}
?>
You can't separate the equals signs or its no longer a conditional statement to check the equal to. you also had a syntax error with your if/else from the brackets. Try this:
<?php
$result = mysql_query("SELECT * FROM articles where articlefriendlyURL='%s'",mysql_real_escape_string($aid));
while($row = mysql_fetch_assoc($result)) {
if (empty($row['price'])) {
echo ("Please Call Us for the price");
} else {
echo $row['price'];
}
}
?>
I think the problem is on
if ($row['price']; = = '0')
I think the correct syntax of that statement is
if ($row['price'] == '0')