Dear Friend,
I am a beginner in php/mysql and trying to connect my database from a php script to add some info.
But the script doesn’t deliver the info to mysql, your further assistance will be appreciated.
Ercan
Dear Friend,
I am a beginner in php/mysql and trying to connect my database from a php script to add some info.
But the script doesn’t deliver the info to mysql, your further assistance will be appreciated.
Ercan
Here is a quick example with some comments for you:
[php]<?php
define(“DBName”,“your_db_name”);
define(“HostName”,“localhost”);
define(“UserName”,“your_db_user_name”);
define(“Password”,“your_db_user_password”);
// Connecting to MySQL database
$mysqlink=mysql_connect(HostName,UserName,Password) or die(mysql_error());
// Selecting database
mysql_select_db(DBName,$mysqlink);
// Querying data
$r=mysql_query(“SELECT * FROM yourtable where id=1”);
if(mysql_num_rows($r)){
$f=mysql_fetch_array($r);
// Display full record
echo '<pre>';
print_r($f);
echo '</pre>';
}
// Adding record
mysql_query(“INSERT INTO yourtable (id,yourfield1,yourfield2) VALUES (2,‘Value 1’,‘Value 2’)”);
// Updating record
mysql_query(“UPDATE yourtable SET yourfield1=‘New Value 1’, yourfield2=‘New Value 2’ where id=2”);
?>[/php]
Hope this helps
Thank you Sir , but it still doesn’t work, here is my script.thank you for your time(it is a learning project)
<? php define=("DBName" , "..............." ); define= ("HostName",:"............"; define=("UserName", "............"); define=("Password", "............"); $db=mysql_connect ( HostName, UserName,Password) or die(mysql_error()); mysql_select_db(DBName, $db); global $db ; // need the connection $query= 'INSERT INTO ecomm_products (product_code, name, description, price) VALUES ("00001", "Trwy logo t-shirt", 17.95), ("00002", "some else", 5,95), mysql_query ($query, $db) or die (mysql_error ($db)); echo ' no way'; ?>What exactly is not working, are you getting error message? Is database created? You can try debugging, and also turn on notices/warnings/errors using error_reporting() function.
When I save the script , a blank page comes up with no error ,
Nothing just blank
Rgrds
Looks like your syntax is not correct:[php]define=(“DBName” , “…” );
define= (“HostName”,:"…";
define=(“UserName”, “…”);
define=(“Password”, “…”);[/php]
here is correct code:[php]define(“DBName” , “…” );
define(“HostName”,:"…";
define(“UserName”, “…”);
define(“Password”, “…”);[/php]
NO = sign.