Jump to content

How to write a manual function for insert query?


syedkhajafairoz

Recommended Posts

well first you connect to mysql with something like this:

mysql_connect(DBHOST,DBUSER,DBPASS)

then select your database

mysql_select_db(DEFAULTDB)

you can put it into a function to keep it organized and easy to re use:

function connection($dbhost,$dbuser,$dbpass,$dbdefault) {$connection = mysql_connect($dbhost,$dbuser,$dbpass) or die('Cannot connect to mysql '.$dbhost.' '.$dbuser.' '.$dbpass.');if(!mysql_select_db($dbdefault)) die('problem selecting database '.$dbdefault);return $connection;}

then make your insert query with all your fields..

$query = "INSERT INTO `table` (`field1`,`field2`,`field3`) VALUES ('$info1','$info2','$info3') ";

and run it

$exe = mysql_query($query);

then check if there was a problem

if(!$exe){  echo('got problems ' . $query);}

I always found the easiest way to figure out any problems is to echo out the bad query and try to make it right in phpMyAdmin... then correct the php

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...