Saud_1 0 Newbie Poster

I'm preparing a property website. Initially I had written the code for mysql and now moving on to mysqli in order to streamline, make more secure and use prepared statement

I don't know much about this and just finding new information as an when needed. I used this tutorial to learn : http://www.binpress.com/tutorial/using-php-with-mysql-the-right-way/17

I'm having a few prepared statements which I use in different ways.

 db_query prepared statement


    function db_query($query) {
        // Connect to the database
         $con = db_connect();


        // Query the database
        $result = mysqli_query( $con,$query);

        return $result;
    }





    function db_select($query) {
        $rows = array();
        $result = db_query($query);

        // If query failed, return `false`
        if($result === false) {
            return false;
        }

        // If query was successful, retrieve all the rows into an array
        while ($row = mysqli_fetch_assoc($result)) {
            $rows[] = $row;
        }
        return $rows;
    }

I can return rows by the following (where $qry is db_select("query"))

foreach ($qry as $row){
echo $row['ptype'];
}

but if want to set an if statement I need to use db_query as db_select will give an error that expected parameter is 1

if(mysqli_num_rows($qry)==0){

so what would be the best way to echo using the prepared statements the following

a single row field's value without a while or foreach loop
a mysqli_num_rows if statement
and possibly if there is a better way to do a foreach loop then $qry as $row

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.