PHP and MySQL example <?php // Connecting, selecting database $link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error()); echo 'Connected successfully'; mysql_select_db('my_database') or die('Could not select database');

sacktoysSoftware and s/w Development

Dec 13, 2013 (3 years and 10 months ago)

83 views

PHP and MySQL example


<?php

// Connecting, selecting database

$link
=
mysql_connect
(
'mysql_host'
,
'mysql_user'
,
'mysql_password'
)



or die(
'Could not connect: '
.
mysql_error
());

echo
'Connected successfully'
;

mysql_select_db
(
'my_database'
) or die(
'Could

not select database'
);


// Performing SQL query

$query
=
'SELECT * FROM my_table'
;

$result
=
mysql_query
(
$query
) or die(
'Query failed: '
.
mysql_error
());


// Printing results in HTML

echo
"<table>
\
n"
;

while (
$line
=
mysql_fetch_array
(
$result
,
MYSQL_ASSOC
)) {



echo
"
\
t<tr>
\
n"
;



foreach (
$line
as
$col_value
) {







echo
"
\
t
\
t<td>$col_value</td>
\
n"
;



}



echo
"
\
t</tr>
\
n"
;

}

echo
"</table>
\
n"
;


// Free resultset

mysql_free_result
(
$result
);


// Closing connection

mysql_close
(
$link
);

?>




PHP and SQLi
te example


<?php


// create new database (OO interface)

$db
= new
SQLiteDatabase
(
"db.sqlite"
);


// create table foo and insert sample data

$db
-
>
query
(
"BEGIN;


CREATE TABLE foo(id INTEGER PRIMARY KEY, name CHAR(255));


INSERT INTO foo (
name) VALUES('Ilia');


INSERT INTO foo (name) VALUES('Ilia2');


INSERT INTO foo (name) VALUES('Ilia3');


COMMIT;"
);


// execute a query



$result
=
$db
-
>
query
(
"SELECT * FROM foo"
);

// iterate through the retrieved rows

while (
$result
-
>
valid
()) {


// fetch current row


$row
=
$result
-
>
current
();




print_r
(
$row
);

// proceed to next row


$result
-
>
next
();

}


// not generally needed as PHP will destroy the connection

unset(
$db
);


?>