English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i have a index.php file where i show 10 songs and there have a button for each song (for listen).for example

song title code play
hi friend.... 3456 play

now i want a file where i can update all songs and then after automaticaly it update in my index.php file

for that i need code for php and mysql connection

2007-10-29 21:32:10 · 1 answers · asked by sandeep u 2 in Computers & Internet Programming & Design

1 answers

example code for reading database:

$dbh = mysql_connect("localhost", "root", "") # host, user, password
mysql_select_db("mydatabase",$dbh) # database name
$query = "SELECT title,group,code FROM songs LIMIT 10";
$sth = mysql_query($query, $dbh); # query, database handle
while (list($title, $group, $code) = mysql_fetch_array($dbh, MYSQL_NUM)) {
# do something
};
mysql_close($dbh);

example code for writing database:

$dbh = mysql_connect("localhost", "root", "") # host, user, password
mysql_select_db("mydatabase",$dbh) # database name
$group = $_POST["group"]; # read from POSTed form
$title = $_POST["title"];
$code = $_POST["code"] + 0; # force it to be numeric
if ($title && $group && $code) {
$query = "INSERT INTO songs(title,group,code) VALUES(\"$title\",\"$group\",$code)";
$success = mysql_query($query, $dbh);
if (!$success) {
print "Error: ", mysql_error($dbh), "\n";
};
}
else {
print "Something missing!\n";
};
mysql_close($dbh);


mysql_connect($host, $user, $password) - returns a database handle (like a filehandle) for a connection on $host with username $user and password $password. You can normally use root and no password.

mysql_select_db($dbname, $dbh) - selects a database (like USE in the mysql command line).

mysql_query($query, $dbh) - connects to the database with handle $dbh and issues the query in $query. If it's a SELECT query, returns a statement handle, which is a reference to the query result; again a bit like a filehandle. You can then read the records you SELECTed using mysql_fetch_array(). If it's a an UPDATE or INSERT, returns true if successful or false if failed (so you can test it with an if statement). Use double speech marks so you can insert variables directly; and remember that non-numeric fields will need speech marks around their values. If you use double speech marks, you will need to prefix them with a backslash \.

mysql_fetch_array($sth, MYSQL_NUM) - returns a numeric array from the query whose statement handle is $sth. The elements will be in whatever order you asked for them in the query (which needn't be the same as the order of the fields in the table). This can be assigned straight to a list.

mysql_fetch_array($sth, MYSQL_ASSOC) - returns an associative array from the query whose statement handle is $sth. The elements will have keys which match the column names in the database, and if you use a foreach() on the array, you will find them ordered as you asked for them in the query.

Either form of mysql_fetch_array will return false if there are no (more) records available from the query. If you want to read several records, you can just do the mysql_fetch_array in a while() loop and the loop will finish after the last record is read.

mysql_close($dbh) - closes the connection with database handle $dbh.

2007-10-29 22:13:18 · answer #1 · answered by sparky_dy 7 · 1 0

fedest.com, questions and answers