0

I am trying to fetch data from the database, but not retrieve data particular id. this is my one page: example1.php

  <a style="color: #3498DB;" class="btn btn-default" href="http://www.example.com/getafreequote?id=<?php echo $row['product_id'];  ?>">Get Quote</a>

example2.php

 <?php 
   $id = isset($_GET['id'])?$_GET['id']:'';

        $query = "SELECT * FROM oc_product_description WHERE product_id=$id";
        $run1 = mysql_query($query);
         while ($fetch1 = mysql_fetch_object($run1)){


   ?>

    <div class="col-xs-12 col-sm-6">
     <label for="GetListed_product"></label>
                <input class="le-input" name="product" id="GetListed_product" type="text"  value="<?php 
                    $b = $fetch1->product_id;

                            $q2 ="SELECT product_id,name FROM oc_product_description WHERE product_id = $b";
                            $q3 = mysql_fetch_assoc(mysql_query($q2));
                         echo $q3['name'];

                ?>" >
    <span id="productmsg" class="msg"></span>
    </div>
<?php
                  }
                  ?>

</div>

but didnot get data form particular product id. I have got error show like this

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in example.com/example2.php on line 71
1
  • check by die(mysql_error()); Commented Oct 31, 2015 at 12:02

2 Answers 2

1

Please don't use mysql functions they are deprecated. Use mysqli or PDO for database operations. Also the way you write the query string makes it easy for an SQL injection, use prepared statements instead. Here is an example:

$db = new PDO("...");
$statement = $db->prepare("select id from some_table where name = :name");
$statement->execute(array(':name' => "Jimbo"));
$row = $statement->fetch(); 

You can also use prepared statements for inserting or updating data. More examples here

Sign up to request clarification or add additional context in comments.

1 Comment

The sql injection is the most important part here.
0

As said by FilipNikolovski, don't use mysql functions they are deprecated. Use mysqli or PDO for database operations.

For your problem, the function mysql_query is returning false. The query is not returning any result and thus mysql_query is returning false.

Make a check like this:

$query = "SELECT * FROM oc_product_description WHERE product_id=$id";
$run1 = mysql_query($query);
if($run1)
{
     if(mysql_num_rows($run1) > 0)
     {
          while ($fetch1 = mysql_fetch_object($run1))
          {
                // your stuff here
          }
     }
     else
     {
          echo "No records found.";
     }
}
else
{
      echo "Error in query : ".mysql_error();
}

This will help you to detect the problem and to solve as well.

6 Comments

I am try to put this code but result show blank page
I am also use mysqli $conn = new mysqli($servername, $username, $password, $dbname);
$id = isset($_GET['id'])?$_GET['id']:''; $query = "SELECT * FROM oc_product_description WHERE product_id=$id"; $run1 = mysql_query($query); if($run1) { if(mysql_num_rows($run1) > 0) { while ($fetch1 = mysql_fetch_object($run1)) { <div class="col-xs-12 col-sm-6"> <label for="GetListed_product"></label> <input class="le-input" name="product" id="GetListed_product"type="text" value="<?php $b = $fetch1->product_id; $q2 ="SELECT product_id,name FROM oc_product_description WHERE product_id = $b"; $q3 = mysql_fetch_assoc(mysql_query($q2)); echo $q3['name']; ?>" >
@balvantparmar you have lots of syntax errors in the code pasted above
from where do you want to fetch id?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.