Select all function php

PHP MySQL Select Data

The SELECT statement is used to select data from one or more tables:

or we can use the * character to select ALL columns from a table:

To learn more about SQL, please visit our SQL tutorial.

Select Data With MySQLi

The following example selects the id, firstname and lastname columns from the MyGuests table and displays it on the page:

Example (MySQLi Object-oriented)

$servername = «localhost»;
$username = «username»;
$password = «password»;
$dbname = «myDB»;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) die(«Connection failed: » . $conn->connect_error);
>

$sql = «SELECT id, firstname, lastname FROM MyGuests»;
$result = $conn->query($sql);

if ($result->num_rows > 0) // output data of each row
while($row = $result->fetch_assoc()) echo «id: » . $row[«id»]. » — Name: » . $row[«firstname»]. » » . $row[«lastname»]. «
«;
>
> else echo «0 results»;
>
$conn->close();
?>

Code lines to explain from the example above:

First, we set up an SQL query that selects the id, firstname and lastname columns from the MyGuests table. The next line of code runs the query and puts the resulting data into a variable called $result.

Then, the function num_rows() checks if there are more than zero rows returned.

If there are more than zero rows returned, the function fetch_assoc() puts all the results into an associative array that we can loop through. The while() loop loops through the result set and outputs the data from the id, firstname and lastname columns.

The following example shows the same as the example above, in the MySQLi procedural way:

Example (MySQLi Procedural)

$servername = «localhost»;
$username = «username»;
$password = «password»;
$dbname = «myDB»;

Читайте также:  Resultset and statement in java

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) die(«Connection failed: » . mysqli_connect_error());
>

$sql = «SELECT id, firstname, lastname FROM MyGuests»;
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) // output data of each row
while($row = mysqli_fetch_assoc($result)) echo «id: » . $row[«id»]. » — Name: » . $row[«firstname»]. » » . $row[«lastname»]. «
«;
>
> else echo «0 results»;
>

You can also put the result in an HTML table:

Example (MySQLi Object-oriented)

$servername = «localhost»;
$username = «username»;
$password = «password»;
$dbname = «myDB»;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) die(«Connection failed: » . $conn->connect_error);
>

$sql = «SELECT id, firstname, lastname FROM MyGuests»;
$result = $conn->query($sql);

if ($result->num_rows > 0) echo «

«;
// output data of each row
while($row = $result->fetch_assoc()) echo «

«;
>
echo «

ID Name
«.$row[«id»].» «.$row[«firstname»].» «.$row[«lastname»].»

«;
> else echo «0 results»;
>
$conn->close();
?>

Select Data With PDO (+ Prepared Statements)

The following example uses prepared statements.

It selects the id, firstname and lastname columns from the MyGuests table and displays it in an HTML table:

Example (PDO)

class TableRows extends RecursiveIteratorIterator <
function __construct($it) <
parent::__construct($it, self::LEAVES_ONLY);
>

function current() return «

» . parent::current(). «

«;
>

$servername = «localhost»;
$username = «username»;
$password = «password»;
$dbname = «myDBPDO»;

try $conn = new PDO(«mysql:host=$servername;dbname=$dbname», $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare(«SELECT id, firstname, lastname FROM MyGuests»);
$stmt->execute();

Источник

How to create a select list using PHP OOP

I’ve started recoding a PHP project into OOP. One thing I can’t work out among many is how to make a dynamic select list. I have many lookup select lists to make. What’s the best way to go about it? I made a DatabaseObject class which has all my generic database queries in it. Do I add them here or make a special class for them, and how do I go about coding it?

require_once("database.php"); class DatabaseObject < protected static $table_name; // find all from a specific table public static function find_all()< global $database; return static::find_by_sql("SELECT * FROM ".static::$table_name); >// select all from a specific table public static function find_all_from($table) < global $database; return static::find_by_sql("SELECT * FROM " .$table); >// find all from a specific table public static function find_by_id($id) < global $database; $result_array = static::find_by_sql(" SELECT * FROM ".static::$table_name. " WHERE LIMIT 1"); // return the data only for the one user return !empty($result_array) ? array_shift($result_array) : false; >// find using sql public static function find_by_sql($sql="")< global $database; // return all data from sql $result_set = $database->query($sql); $object_array = array(); while($row = $database->fetch_array($result_set)) < $object_array[] = static::instantiate($row); >return $object_array; > protected static function instantiate($record)< $class_name = get_called_class(); $object = new $class_name; foreach($record as $attribute=>$value)< if($object->has_attribute($attribute))< $object->$attribute = $value; > > return $object; > protected function has_attribute($attribute)< $object_vars = $this->attributes(); // here we only want to know if the key exist // so we will return true or false return array_key_exists($attribute, $object_vars); > protected function attributes() < $attributes = array(); foreach(static::$db_fields as $field) < if(property_exists($this,$field)) < $attributes[$field]= $this->$field; > > return $attributes; > protected function sanitised_attributes() < global $database; $clean_attributes = array(); foreach($this->attributes() as $key => $value)< $clean_attributes[$key] = $database->escape_value($value); > return $clean_attributes; > public function save() < // A new object won't have an id yet return isset($this->id) ? $this->update() : $this->create(); > // create new protected function create() < global $database; $attributes =$this->sanitised_attributes(); $sql = "INSERT INTO ".static::$table_name." ("; $sql .= join(", " ,array_keys($attributes)); $sql .= ") VALUES ( '"; $sql .= join("', '" ,array_values($attributes)); $sql .= "')"; if($database->query($sql)) < $this->id = $database->insert_id(); return true; > else < return false; >> // update details protected function update() < global $database; $attributes =$this->sanitised_attributes(); $attribute_pairs = array(); foreach($attributes as $key => $value) < $attribute_pairs[] = "=''"; > $sql = "UPDATE " .static::$table_name. " SET "; $sql .= join(", ",$attribute_pairs); $sql .= " WHERE DELETE FROM ".static::$table_name; $sql .= " WHERE LIMIT 1"; $database->query($sql); return ($database->affected_rows() ==1) ? true : false ; > > 

Источник

How do you select the entire PHP function definition?

Press V after the selection command you post, to convert the selection to line selection, and it will select the function declaration:

@wallyk: Will work on normal mode, after B is pressed (make sure is capital B) the block selection will appear, and then when V is pressed, the selection will include the function declaration, since it’s in the same line that the opening curly brace.

@Ewan: Try it out, the iB part of the command will make that the initial line selection (started with V ), become a normal character selection.

It’s been a long time since this question was asked and answered, but I will add my own answer because it’s the one I was looking for and none of the others work exactly like this one:

nnoremap vaf ?func.*\n*\s*ma/%mb`av`b vmap af okvaf 

The user can then press V to turn to linewise select mode if she wants to.

The only problem that I found is that when I am in the body of a big function, but below a line that uses a lambda (let’s say «small») function, this will stop searching at the beginning of the small function and select it’s body instead of reaching the start of the big function and select all of its body.

function show_video_server(v_server) < // this whole function should get selected var something = function()< /* this function gets selected */ >; // | the cursor is here when I type "vaf" > 

As a workaround I use the second mapping: vmap af okvaf . It feels like a repetition or expansion of the selection. What it really does is abandon the selection and go to the line before it, and then try it agan. If the «big» function uses several lambda functions the user has to repeat the af several times to reach the big one.

Usually, vaf es enough. Sometimes vaf af or vaf af af is needed. Anyway, it’s the closest I could get to what I wanted, so this is the version I’m using.

Источник

Оцените статью