Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Website Design/Showcase (http://www.chiefdelphi.com/forums/forumdisplay.php?f=64)
-   -   MySQL Problem (http://www.chiefdelphi.com/forums/showthread.php?t=29095)

Joshua May 16-06-2004 11:25

MySQL Problem
 
I am trying to learn working with MySQL databases through PHP but I'm having some problems. I've written this simple scipt which takes a table named "contacts" and first enters some information into the table. Then, it attempts to read and display the data in the table.

Here's the full code:
PHP Code:

<?php
$username 
"username";
$password "password";
$database "hhsrobotics_circuitrunners_com_-_ContactList";

$link mysql_connect(localhost,$username,$password);

@
mysql_select_db($database) or die( "Unable to select database");

$query "INSERT INTO contacts VALUES ('','John','Smith','555-555-5555','444-444-4444','333-333-3333','johnsmith@email.com','http://www.johnsmith.com')";

mysql_query($query);

$result mysql_query("SELECT * FROM contacts",$link);

$num mysql_numrows($result);

$i=0;
while (
$i $num) {

$first mysql_result($result,$i,"first");
$last mysql_result($result,$i,"last");
$phone mysql_result($result,$i,"phone");
$mobile mysql_result($result,$i,"mobile");
$fax mysql_result($result,$i,"fax");
$email mysql_result($result,$i,"email");
$web mysql_result($result,$i,"web");

echo 
"<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br>";

$i++;
}

mysql_close();
?>

The problem is that I keep getting argument errors with the lines
PHP Code:

$result mysql_query("SELECT * FROM contacts",$link); 

PHP Code:

$num mysql_numrows($result); 

and all of the
PHP Code:

$first mysql_result($result,$i,"first"); 

lines.

Oh, and there is a primary key in the first part of the table named "id" which is not displayed to the user, and its set to Auto_incriment so the data doesn't have to be entered for it.

jeremy562 16-06-2004 14:17

Re: MySQL Problem
 
I believe the function is "mysql_num_rows", not "mysql_numrows" (note the underscore between num and rows).

evulish 16-06-2004 20:57

Re: MySQL Problem
 
Try using:

PHP Code:

$result mysql_query("SELECT * FROM contacts",$link) or die ("Mysql error: ".mysql_error()); 


Also, all that mysql_result stuff is probably overkill thanks to mysql_fetch_array.

PHP Code:

while ($array mysql_fetch_array($result,MYSQL_ASSOC)) {
print 
"$array['row'] $array['otherrow'] $array['etc']";


That makes it a whole lot cleaner and I'd assume quicker (but I'm not positive). (Also, take a look at www.php.net/mysql_fetch_array to see what that MYSQL_ASSOC is about, if you're wondering. You don't really need it, I guess, but I always use it)

Nate Smith 16-06-2004 21:51

Re: MySQL Problem
 
PHP Code:

while ($array mysql_fetch_assoc($result)) {
print 
"$array['row'] $array['otherrow'] $array['etc']";


You can actually use this for the same effect as the mysql_fetch_array with MYSQL_ASSOC as the parameter. That's what I'm using in a script I'm writing for my church's web site...

Joshua May 17-06-2004 00:35

Re: MySQL Problem
 
Allright guys, thanks, I'll try these.

Joshua May 17-06-2004 10:35

Re: MySQL Problem
 
OK, I found the problem, but I didn't get a good error report until evulish suggested that line with the "or die()". The real problem was that my table was not named "contacts", it was contact. lol

Thanks alot guys.


All times are GMT -5. The time now is 15:31.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi