Jump to content
shams

php mysql query with two conditions problem?

Recommended Posts

My web pages are organized in two mysql tables, table category with two columns cat_id and category and table subcategory with three columns cat_id, subcategory and subcat2,  i run this msyql query from terminal it is working:

mysql> SELECT subcat2 from subcategory  WHERE cat_id=1 AND subcategory=3;
+--------------+
| subcat2      |
+--------------+
| 001_003.html |
+--------------+
1 row in set (0.01 sec)

Next, i execute the dd-mysqli.php it successfully get the sql query values from the drop down menu and parse to the chgoto.php and run this as well this is in the dd-mysqli.php:

echo "<form method=post name=f1 action='chgoto.php'>";

to print the cat_id and subcategory, it is working, this is chgoto.php:

<?php
require 'config-mysqli.php';
//////// End of connecting to database ////////
$cat=$_POST['cat'];
$page=$_POST['subcat'];
echo "$cat <br> $page";
?>

the reslut is shown here:

1 (is cat_id)
3  (is subcategory

The problem is with the next php code, i want to add a sql query with two conditions in the chgoto.php to get the subcat2 value which is the url of html pages (as i run this mysql query in termianl in the begining of this post) but this is not working and there is only blank page in the output:

<?php
require 'config-mysqli.php';
//////// End of connecting to database ////////
$cat=$_POST['cat'];
$page=$_POST['subcat'];
$result = mysqli_query($connection,'SELECT subcat2 from subcategory  WHERE cat_id='.$cat.' AND subcategory='.$page.'');
$row = mysqli_fetch_assoc($result);
while($row = mysqli_fetch_assoc($result)){
        $url = $row['subcat2'];
        echo $url;
exit ;
}

$result->close();
$connection->close();
?>

I already enabled the php error log but no errors are reported for this query.

Share this post


Link to post
Share on other sites

Take a look at these two lines of code

$row = mysqli_fetch_assoc($result);
while($row = mysqli_fetch_assoc($result)){

and tell your rubber duck what they are doing.

  • Like 2

Share this post


Link to post
Share on other sites
  • Thanks for the hint, i remove the line
    $row = mysqli_fetch_assoc($result);
  • now it work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.