View Full Version : MySql and PHP
bolsterboy
10-04-2001, 09:33 AM
Hi i was just wandering, how could i create a mysql database in my part of the server and be able to connect to it? I can create one (sorry reuben if some magically appeared :o) but it does not appear in my space.
How could i create tables, and information to them and then be able to edit the info through HTTP?
also how would i search to a MySql DB to find and retrieve information?
It's just really something to add users,give them access to edit thier passwords, create profiles and allowing searching through a field like age or name.
big question but thanks a lot :)
reuben
10-04-2001, 11:00 AM
Hello,
Keep in mind that I don't do MySQL programming, so I probably can't answer much.
When you create a database, it automatically puts it into our database folder on the server where they are all shared. Then when you set up scripts to access the database with the database name it automatically checks the database folder.
Tables and such can be created with the pypmyadmin utility in the control panel.
I'm not sure about adding and editing info though. I'd try searching at http://mysql.com.
bolsterboy
10-04-2001, 12:30 PM
thanks, i made a script which installs the DB itself and i can add tables using it (:D) but i was just waondering, it doesn't show up in the MySQL section in my cPanel and i cant edit it using phpAdmin so i was just wondering if i did something which i shouldn't have :o
Thanks anyway reuben :)
bolsterboy
10-04-2001, 12:32 PM
Ok i think i've worked out some of it by looking at my xmb board code :)
Just have to figure it out now lol :D
nsr81
10-04-2001, 02:13 PM
Hey Blosterboy, here is my response to what I understood to be your problem.
You don't need a third party script to install(create) a new database. Use control panel to do that. Here is a quick screen cap. of mysql menu and how you'd go about creating a database.
http://www.ny-tech.net/db.gif
1. Create a database by typing in the name for the database and clicking on "Add Db". This will create a database with a name in this format. yourprimaryusername_databasename
2. Now create a User. Type in the username and pass and click on "Add User". As with database, the username will also be like yourprimaryusername_username.
3. ***IMPORTANT*** Add the user you created to the database you created. Select the user name and the database names from the dropdown list and click on "Add User to Db".
You can then use phpMyAdmin to create tables and other stuff. I recommend that you use phpMyAdmin to create tables, since you won't make sytax mistakes that way. Plus phpMyAdmin provides direct links to relative information in mysql manual.
Now how to connect to database using PHP. Here goes:
anything in < > means supply your own. I'm going to assume that I have a table named "table" containing two fields id and emails with lots of entries, that I want to print out.
<?
//connecting to databse.
$connection = @mysql_connect("localhost","<username>","<pass>");
//selecting desired database.
$db = @mysq_select_db("<database name>",$connection);
//constructing a query string.
$query = "SELECT * FROM table";
//Executing query and getting results.
$results = @mysql_query($query, $connection);
//printing out stuff
while( $row = mysql_fetch_array($result))
{
// $row contains one entry of the database with all fields.
// Getting stuff into variables.
$id = $row['id'];
$email = $row['emails'];
// printing out.
echo "$id $email \n";
}
?>
I hope it helps you, if you have any further questions, ask here or email me directly.
bolsterboy
10-04-2001, 02:23 PM
hi thanks but i know how to add the database in the control panel, just playing around at what i could do lol :)
Now i'm getting an error could you take a look?
http://littleprankster.com/php/1.php (is where the program starts) the error is on the second page.
i've set it up so that there are "signposts" to continue :)
thanks a lot :D
nsr81
10-04-2001, 02:35 PM
could you post insert.php code. and specs of your table where you are trying to insert those values.
bolsterboy
10-04-2001, 02:44 PM
Ok here it is :)
i connected to the DB by using the same code from the xmb board and it works :) it's just adding the stuff.
<?php
if ($submit == "click"){
// The submit button was clicked!
// Get the input for fullname and email then store it in the database.
function connectdb() {
$dbname = "bolsterb_members";
$dbuser = "bolsterb_bolster";
$dbpw = "PASSWORD";
$dbhost = "localhost";
mysql_connect($dbhost, $dbuser, $dbpw) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
}
connectdb();
$query = "insert into members_user values ('$user', '$age', '$email', '$location')";
$result = mysql_db_query ("bolsterb_members", $query);
if ($result){
echo "
<html><body>
<p align=\"center\"><h1>Inserted the info</h1></p><br><hr>
<p align=\"center\"><a href=\"show.php\">Click here to see the table</a></p>
</body></html>
";
}
else{
echo mysql_errno().": ".mysql_error()."<BR><br><a href=\"show.php\">Click here to continue</a><br>";
}
mysql_close ();
}
else{
echo "
<html><body>
<form method=\"post\" action=\"insert.php\">
Enter your full name
<input type=\"text\" name=\"user\"></input><br>
Enter your email address
<input type=\"text\" name=\"email\"></input><br>
<input type=\"submit\" name=\"submit\" value=\"click\"></input>
</form>
</body></html>
";
}
?>
and:
database name = "bolsterb_members";
database user = "bolsterb_bolster";
database password= "PASSWORD";
database host = "localhost";
thanks again :)
BTW how do you insert comments into the scripts? is it //? or something
bolsterboy
10-04-2001, 02:47 PM
the board messed it up so it is at
http://littleprankster.com/php/insert.txt
thanks :)
nsr81
10-04-2001, 03:01 PM
I'm going to college for my evening class, be back in a couple of hours. THen I'll give you a detailed response.
use "PHP" button when you are typing in a a new post or replying to insert php code.
/* This is C style commnet */
// This is a C++ style comment
bolsterboy
10-04-2001, 03:31 PM
Oh right :) Don't worry, i'm away to bed :) it's 11:30pm here so i think i should lol :D
if you need help programming, just email me - markh@markhadjar.com
nsr81
10-04-2001, 08:35 PM
Hehe, I just looked at your script thoroughly and I think I got your error by it's neck.
Turns out you are trying to insert more things then there is room for and I don't mean room for more entries.
Just take a look. This is what you are trying to insert into table.
$query = "insert into members_user values ('$user', '$age', '$email', '$location')";
You are sending 4 variables, 2 of which would turn up as "NULL" when inserted into database because you are not assigning any values to $age and $location. AND I think your table has only two fields $user and $email. try using this line instead of the above.
$query = "insert into members_user values ('$user', '$email')";
I might be wrong, but It seems that that is where you are encountering problem.
When you get up,:) Let me know if this solves the problem, hopefully I'll be still around then. if not I'll get back to you in the morning.
bolsterboy
10-05-2001, 02:31 PM
:D I fixed it (sortof) there was a field created which wasn't edited so i think that may have been the problem :) Fixed now :)
vBulletin® v3.8.1, Copyright ©2000-2010, Jelsoft Enterprises Ltd.