View Full Version : Creating a db with php
Heywood
12-05-2002, 07:35 AM
Ok, I have looked this over, and I cannot find what I am doing wrong. My host will not allow SSH access, or do any modifications to the MySQL database. He set it up, gave me a login and pass, and thats it. Fine. But I am trying to create a table, and here is what I have for code:
<?php
$dbhost = 'localhost';
$dbname = 'mydb';
$dbuser = 'myusername';
$dbpasswd = 'mypassword';
function connect()
{
if(!$db = mysql_connect("$dbhost","$dbuname","$dbpass")):
print "Cant Connect to the database";
else:
mysql_select_db ("php3", $dbname);
mysql_query(" CREATE TABLE news (id INT
11) not null AUTO_INCREMENT, title VARCHAR (50), news TEXT , author VARCHAR (50) , date DATE ,PRIMARY KEY (id)) ");
print "successful";
endif;
}
?>
of course, "myusername" and password are correct.
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/jeremy/html/sparkie/edit.php on line 13
I can post information to the DB with no error, but I can't seem to retrieve it. Any ideas what might be wrong?
bcarl314
12-05-2002, 12:32 PM
try this:
$linkID = mysql_select_db ("php3", $dbname);
$resultID = mysql_query(" CREATE TABLE news (id INT
11) not null AUTO_INCREMENT, title VARCHAR (50), news TEXT , author VARCHAR (50) , date DATE ,PRIMARY KEY (id)) ", $linkID);
Heywood
12-06-2002, 03:39 AM
Hm.. that didn't seem to work either. I think I am going to see if I can steal some snippet or find a way to display the entire db, so I can see if the tables are being created.
This could also be a php code problem.
raptori
12-06-2002, 05:41 AM
try this:
<?php
$dbhost = 'localhost';
$dbname = 'mydb';
$dbuser = 'myusername';
$dbpasswd = 'mypassword';
connect();
function connect()
{
if(!$db = mysql_connect("$dbhost","$dbuname","$dbpass")){
echo "Cant Connect to the database";
echo "<br>Error is:".mysql_error();
exit;
}
else{
$db = mysql_connect("$dbhost","$dbuname","$dbpass")
mysql_select_db ("php3", $db);
mysql_query(" CREATE TABLE news (id INT
11) not null AUTO_INCREMENT, title VARCHAR (50), news
TEXT , author VARCHAR (50) , date DATE ,PRIMARY KEY (id)) ");
echo "successful";
}
}
?>
bcarl314
12-06-2002, 01:26 PM
raptori: Isn't that pretty much what i suggested? Just you use $db and I use $linkID? lol ;) Oh yeah, and copy all the code??? ;)
raptori
12-06-2002, 11:17 PM
the script had some other errors thats why i copied and fixed it.
i didn't mean anything but i think you missunderstood! :thumbsup: ???
Heywood
12-07-2002, 08:00 AM
What do you guys think the possibility of an interpreter problem? No matter how I code it, I get the same error.
Warning: mysql_fetch_array(): supplied argument is not
a valid MySQL result resource in /home/jeremy/html/sparkie/edit.php on line 13
Line 13 is this:
while ($rows = mysql_fetch_row($result))
I have ripped over it, and I cant get away from it. I have some people from another forum helping as well, and nothing works. I have scoured my "php bible" and also php.net, and following that example, I get this error.
Raptori, I tried your code, and got this:
Parse error: parse error, unexpected T_STRING in /home/jeremy/html/sparkie/config.php on line 23
I also tried bcarl's suggestion.
I can accept that I might be wrong, because I am still learning this, but several other programmers being wrong, and my books, and php.net....all being wrong? Not really likely.
Perhaps my webhost installed PHP wrong?
druffus
12-07-2002, 08:40 AM
can we see some more code around line 13?
Heywood
12-07-2002, 09:30 AM
can we see some more code around line 13?
here is the entire edit.php, I have made a lot of changes, but still the same error.
<?php
include "config.php";
$db = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db ($db_name) or die ("Cannot connect to database");
/* We have now connected, unless you got an error message */
/* Lets display the current titles, so we can select one for editing */
echo "<b>Please click on a title to edit news :</b><BR>";
$query = "SELECT id, title FROM news";
$result = mysql_query($query);
while($r=mysql_fetch_array($result))
{
/* This bit sets our data from each row as variables, to make it easier to display */
$id=$r["id"];
$title=$r["title"];
/* Now lets display the titles */
echo "<A HREF=\"edit_process.php?id=$id\">$title</A><BR>";
}
mysql_close($db);
?>
druffus
12-07-2002, 09:18 PM
Nothing obvious stands out, but a suggestion would be to take it to the basics and see if something is still wrong.
Put you connection variables right into their functions, like...
$db = mysql_connect('143.0.0.0','user','pass');
mysql_select_db ('my_db')
then run as simple and direct a query as possible to ensure no errors in sql statement...
$query = "SELECT * FROM news";
$result = mysql_query($query);
while($r=mysql_fetch_array($result))
{
$id=$r["id"];
$title=$r["title"];
echo $id;
echo $title;
}
for some reason your query to the database is not working, you are not getting a result resource. I'd be looking at my connection to the db and my query. Good luck
Heywood
12-17-2002, 03:50 AM
thanks guys.. you've all been helpful. Basically, I found out there was a problem with the database creation itself. No matter what I tried, it wouldn't worked, so I installed phpmyadmin and set up the DB that way, and everything worked. :thumbsup:
Now its off to more coding...
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.