php - Do I always need to use mysql_fetch

279

I seem to always use these lines when i am doing a select in PHP

 $product_query = "Select ProductID from productinfo where PartName = '".mysql_real_escape_string($data[0])."'";
 $product_result = mysql_query($product_query) ;
 $product = mysql_fetch_assoc($product_result) ;
 $my_product_id = $product['ProductID'];

This works but it clutters the page so i was wondering if there an way to do this in a more concise manner

305

Answer

Solution:

You can make your own class which just returns the data after you give it the query.

$db = new Db;
$data = $db->query('blah');
print_r($data);

You can also use PDO, which has a similar, cleaner and safer API:

http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html

People are also looking for solutions to the problem: Learn C or C++ for php extensions

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.