...

View Full Version : Basic Script with MySQL



QwertyKeyboard
02-12-2006, 03:40 AM
Can someone give me some basic script that uses MySQL and PHP functions and variables please? I just want to use it to learn how to code, I'm a visual learner so I think it'll help. Thanks.

Jesters8
02-12-2006, 05:11 AM
Here's a basic script to find a record with a particular number:


$ladderQuery = "SELECT * FROM ladders WHERE num = ".$l;
$ladderInfo = mysql_Query($ladderQuery);
$ladder = mysql_Fetch_Array($ladderInfo);

If you ask for more specific things I can probably be more helpful. :)

QwertyKeyboard
02-12-2006, 05:57 AM
Hmm... How about something that you click on a link and Number A (stored in the MySQL Database) goes up a random number between 8 and 10.

ralph l mayo
02-12-2006, 06:31 AM
Hmm... How about something that you click on a link and Number A (stored in the MySQL Database) goes up a random number between 8 and 10.



<?php
if (isset($_POST['update']))
{
mysql_connect('localhost', 'root', '');
mysql_select_db('somedb');
mysql_query('CREATE TABLE IF NOT EXISTS `sometable` (
`id` INT NOT NULL AUTO_INCREMENT ,
`somefield` INT NOT NULL ,
PRIMARY KEY ( `id` )
) TYPE = innodb;');
mysql_query('INSERT INTO `sometable`(`somefield`, `id`)
VALUES (' . mt_rand(8, 10) . ', 1)
ON DUPLICATE KEY UPDATE `somefield` = `somefield` + ' . mt_rand(8,10));
$q = mysql_query('SELECT `somefield`
FROM `sometable`
WHERE `id` = 1
LIMIT 1');
$r = mysql_fetch_assoc($q);
print_r($r);
}

?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input name="update" type="submit" value="do the thing" />
</form>
</body>
</html>

QwertyKeyboard
02-12-2006, 07:38 AM
Thanks, though what do I fill in to the "sometable" thing?

ralph l mayo
02-12-2006, 07:43 AM
Nothing, the only thing that needs to change is the connection username and password if you have something other than the default root/blank and the database name. MySQL should install with a database named 'mysql' if you don't know how to create your own.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum