...

View Full Version : create database



cgibie
01-11-2006, 10:33 PM
Hello I'm pretty new to php... I downloaded the phpdev5 version. I used php to code up to create a database


<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'mypass';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$query = "Create DATABASE phpcake";
$result = mysql_query($query);

mysql_select_db($dbname);

mysql_close($conn);
?>


I saved this as data.php in the www folder, then I ran it but I don't know where the database goes to? I cannot find a database/folder called phpcake. Any idea about this? Also I created a folder called php in that www folder, but then I cannot access it, why? Please help!

Element
01-11-2006, 10:39 PM
Thats because its MySQL and stored in MySQL. You need to use phpMyAdmin installed on your server (if it is) and edit from there, also its better to create columns in your table while creating it.

cgibie
01-11-2006, 10:49 PM
I have phpmyadmin ... so you are saying I should save that data.php in that phpmyadmin folder? but then when I typed localhost on the web browser, I cannot see the folder phpmyadmin, then how am I going to run that data.php?

Element
01-11-2006, 10:52 PM
No... you use phpMyAdmin to access MySQL database, PHP only uses MySQL.

Consider PHP your handle to open MySQL data and output it.

Rich Pedley
01-11-2006, 10:56 PM
PHP only uses MySQL.

*cough*

Element
01-11-2006, 10:59 PM
Yeah, not the best way to put it.

More like, when you make a database its not like fopen() where you'll find mytable.db or something. All the information is stored privately by MySQL where it can't be accessed on the public side.

cgibie
01-11-2006, 11:05 PM
Oh I kinda get it... but how can I get the data from the database to print to the php page? I have this code:




<?php
include 'config.php';
include 'opendb.php';

$query = "SELECT name, subject, message FROM contact";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))
{
echo "Name :{$row['name']} <br>" .
"Subject : {$row['subject']} <br>" .
"Message : {$row['message']} <br><br>";
}

include 'closedb.php';
?>



Where shall I save it to? I mean which folder?

marek_mar
01-11-2006, 11:09 PM
Folder? You want to cache it now?
That code should work.

cgibie
01-11-2006, 11:12 PM
Yeah I have that codes, say I call it read.php then where shall I save it to? is it to the www directory folder? Or in mysql folder? OR in phpmyadmin folder? by the way where can I find the password of the user of database?

marek_mar
01-11-2006, 11:31 PM
Call the server admin. If you are the admin... use phpmyadmin to add a user/change your password.
A MySQL installation comes with a user called root with no password.

You can put the file where you want... as long as it's in the servers web root. You should move the included files aswell

cgibie
01-11-2006, 11:36 PM
I went into the phpmyadmin and created a table called university

University (table)
Name (row)
David (row)

Then I coded




<?php
$dbhost = 'localhost';
$dbuser = 'phpbie';
$dbpass = 'mypass';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$query = "SELECT * FROM University";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))
{
echo "Name: {$row['name']} <br>";
}

mysql_close($conn);
?>


I put this data.php file in the root directory, when I ran it , it gives me nothing not even error message.

Please help!

marek_mar
01-11-2006, 11:41 PM
Blank page?
Add error_reporting(E_ALL); at the top and add "print 'hello';" to check if the PHP is really being executed. Also change
$result = mysql_query($query);
to
$result = mysql_query($query) or die(mysql_error());

cgibie
01-11-2006, 11:44 PM
It says

hello No database selected

Did I write the code wrong? There's a database there.

cgibie
01-11-2006, 11:54 PM
Oh forgot to put the database name there... well it prints

hello name:

I want to print that name David out




<?php
error_reporting(E_ALL);

$dbhost = 'localhost';
$dbuser = 'phpbie';
$dbpass = 'mypass';
$dbname = 'phpbie';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

mysql_select_db($dbname);
$query = "SELECT name FROM university";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result))
{
echo "Name: {$row[0]} <br>";
}

mysql_close($conn);
?>

cgibie
01-12-2006, 12:09 AM
Problem solved :)

Cheers!

danielwarner
01-12-2006, 12:17 AM
<?php

// ---- Connection

define("DB_SERVER", "localhost");
define("DB_USER", "phpbie");
define("DB_PASS", "mypass");
define("DB_NAME", "phpbie");

mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME) or die(mysql_error());

// ---- Query

$result = mysql_query("SELECT * FROM university");
while($row = mysql_fetch_array($result))
{

$name = $row["name"];

echo "Name:" . $name . "<br />";

}

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum