[thelist] MySQL query in PHP

Jay Blanchard jay.blanchard at niicommunications.com
Fri Sep 26 15:44:46 CDT 2003


[snip]
I need an example of a query in PHP on a MySQL table displayed on form.
I 
need this, ideally, in a view, edit and delete mode.

I understand that this may not be too for those who talk PHP in their
sleep, 
but to a new comer to PHP, this is a daunting  task.
[/snip]

Ken - who needs not be daunted, but rather just needs to read

May I suggest subscribing to the PHP mailing list? However, this kind of
request will surely get you flamed as the information is in almost every
PHP book, several tutorials on the web, and on the PHP web site.

<?php
/*connect to database with error checking*/
if(!($dbconnect = mysql_pconnect("localhost", "user", "pw"))){
	print("Failed to connect to database!\n");
	exit();
}
if(!mysql_select_db("database", $dbconnect)){
	print("Failed to select database!\n");
	exit();
}
/*view stuff*/
$sqlSelect = "SELECT foo, bar FROM database ";
if(!($dbSelect = mysql_query($sqlSelect, $dbconnect))){
	print(mysql_error . "\n");
	exit();
}
print("<table>\n");
while($result = mysql_fetch_object($dbSelect)){
	print("<tr>\n");
	print("<td>" . $result->foo . "</td>\n");
	print("<td>" . $result->bar . "</td>\n");
	print("</tr>\n");
}
print("</table>\n");

/*update stuff*/
$sqlUpdate = "UPDATE database SET foo = 'garp' ";
if(!($dbUpdate = mysql_query($sqlUpdate, $dbconnect))){
	print(mysql_error . "\n");
	exit();
}

/*delete stuff*/
$sqlDelete = "DELETE FROM database WHERE foo = 'garp' ";
if(!($dbDelete = mysql_query($sqlDelete, $dbconnect))){
	print(mysql_error . "\n");
	exit();
}
?>

All of this assumes that the user has permission. 



More information about the thelist mailing list