Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php/mySQL newbie question

Status
Not open for further replies.

brownfox

Programmer
Joined
Jan 5, 2003
Messages
173
Location
GB
Im migrating from asp to php (not by choice exactly, I thought I was buying Domain hosting with asp, but infact the hosting uses php/mySQL - thanks maxipointservers.net!).I don't have a clue where to start. All the tutorials I can find want me to install mySQL etc. However I can't see why I need to do this since I am, effectively, renting server space with this already installed. So I want to CREATE TABLE in mySQL database but I dont have access to any sort of control panel that I can see so I upload a php script:
<?
$username = &quot;*******&quot;;
$password = &quot;*******&quot;;
$hostname = &quot;localhost&quot;;
$database = &quot;*******&quot;;
mysql_connect($hostname, $username, $password);
@mysql_select_db($database) or die(&quot;Could not select database&quot;);
$query=&quot;CREATE TABLE contacts (id int(6) NOT NULL auto_increment,first varchar(15) NOT NULL,last varchar(15) NOT NULL,phone varchar(20) NOT NULL,mobile varchar(20) NOT NULL,fax varchar(20) NOT NULL,email varchar(30) NOT NULL,web varchar(30) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))&quot;;
mysql_query($query);
mysql_close();
?>

I insert some test data from a form and try to select * from 'contacts' (the table above):
<?
$username=&quot;*******&quot;;
$password=&quot;*******&quot;;
$database=&quot;*******&quot;;
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( &quot;Unable to select database&quot;);
$query=&quot;SELECT * FROM contacts&quot;;
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
echo &quot;<b><center>Database Output</center></b><br><br>&quot;;
$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,&quot;first&quot;);
$last=mysql_result($result,$i,&quot;last&quot;);
$phone=mysql_result($result,$i,&quot;phone&quot;);
$mobile=mysql_result($result,$i,&quot;mobile&quot;);
$fax=mysql_result($result,$i,&quot;fax&quot;);
$email=mysql_result($result,$i,&quot;email&quot;);
$web=mysql_result($result,$i,&quot;web&quot;);
echo &quot;<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>&quot;;
++$i;
}
?>
And I get this error:
Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/virtual/site266/fst/var/ on line 12
Database Output

With asp I could use my 'Database Manager' to create my tables and it all seemed a lot clearer. Can anyone explain to me as if I'm a complete idiot, in words no more than two syllables, what my first steps should be in trying to create a table for mySQL? Thanks
 
You're not performing enough error trapping to diagnose the problem.

The &quot;not a valid MySQL result resource&quot; error is caused when your query causes an error -- a syntax error in your query, permission errors, etc.

At a bare minimum, change this line:

$result=mysql_query($query);

to read:

$result=mysql_query($query) or die (mysql_error());

Want the best answers? Ask the best questions: TANSTAAFL!
 
Thanks I've got it now. I don't quite know why but when I changed the code as you suggested, the error msg said the table didn't exist, so I re-created it and it all works now. Cheers!
 
Just as a fyi, most servers that I know (very limited experience however), will provide something like phpMyAdmin to administer your mySQL stuff, it is invaluable IMHO. If not you can download something like sqlYOG so you have a mssql like front end.

I'd recommend it though it is no way necessary.

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top