drawing out data from a MySQL DB
Hey guys, I'm having a problem with some data being drawn from a Db and printed...it's supposed to search for rows, and then print the data in groups...once through each list of text (e.g. the 1st set is mine, the 2nd set is from someone in Austrailia)...here's the code (i'm getting errors):
<?php
...connection stuff
$sql = mysql_query(\"SELECT * FROM data\");
$qry = mysql_fetch_array($sql);
echo \"Husband's Name: $qry['hname'] \n\";
echo \"Wife's Name: $qry['wname'] \n\";
echo \"Address: $qry['address'] \n\";
echo \"City: $qry['city'] \n\";
echo \"State: $qry['state']\n\";
echo \"Zip Code: $qry['zip'] \n\";
echo \"Phone Number: $qry['phone1'] $qry['phone2'] $qry['phone3']\n\";
echo \"Email Address: $qry['email'] \n\";
echo \"Membership Type: $qry['family'] \n\";
echo \"Child 1: $qry['child1'] \n\";
echo \"Child 1 Birthday: $qry['month1'] $qry['day1'] $qry['year1'] \n\";
echo \"Child 2: $qry['child1'] \n\";
echo \"Child 2 Birthday: $qry['month2'] $qry['day2'] $qry['year2'] \n\";
echo \"Child 3: $qry['child3']\n\";
echo \"Child 3 Birthday: $qry['month3'] $qry['day3'] $qry['year3']\n\";
echo \"Child 4: $qry['child4'] \n\";
echo \"Child 4 Birthday: $qry['month4'] $qry['day4'] $qry['year4'] \n\";
...closing stuff
?>
any problems that you see? thanks.
Busy posted this at 06:20 — 10th November 2003.
He has: 6,151 posts
Joined: May 2001
tried no quotes in the $qry[var] ?
Suzanne posted this at 13:04 — 10th November 2003.
She has: 5,507 posts
Joined: Feb 2000
You need a loop.
Unless there is only one row returned at all times.
kb posted this at 23:21 — 10th November 2003.
He has: 1,380 posts
Joined: Feb 2002
alright, well i tried this:
<?php
$sql = mysql_query(\"SELECT * FROM data\");
$qry = mysql_fetch_array($sql);
$numrows = mysql_num_rows($sql);
if ($numrows > 0) {
foreach ($numrows) {
echo \"Husband's Name: \"$qry['hname'];
echo \"Wife's Name: \"$qry['wname'];
echo \"Address: \"$qry['address'];
echo \"City: \"$qry['city'];
echo \"State: \"$qry['state'];
echo \"Zip Code: \"$qry['zip'];
echo \"Phone Number: \"$qry['phone1'] $qry['phone2'] $qry['phone3'];
echo \"Email Address: \"$qry['email'];
echo \"Membership Type: \"$qry['family'];
echo \"Child 1: \"$qry['child1'];
echo \"Child 1 Birthday: \"$qry['month1'] $qry['day1'] $qry['year1'];
echo \"Child 2: \"$qry['child1'];
echo \"Child 2 Birthday: \"$qry['month2'] $qry['day2'] $qry['year2'];
echo \"Child 3: \"$qry['child3']\n\";
echo \"Child 3 Birthday: \"$qry['month3'] $qry['day3'] $qry['year3'];
echo \"Child 4: \"$qry['child4'];
echo \"Child 4 Birthday: \"$qry['month4'] $qry['day4'] $qry['year4'];
}
}
else {
echo \"No data at this time.\";
};
?>
but i'm getting an error on the line with the if statement...
Suzanne posted this at 23:28 — 10th November 2003.
She has: 5,507 posts
Joined: Feb 2000
Concatenate those echos! Escape those quotes!
echo 'Husband\'s Name:**' . $qry['hname'];
OR do this:
echo "Husband\'s Name:*$qry['hname']";
kb posted this at 00:25 — 11th November 2003.
He has: 1,380 posts
Joined: Feb 2002
oops...
does anyone know of a php validtor or editor that is intelligent enough to pick up some errors?
ie. i am still getting an error that refers to my numrows line:
<?php
$numrows = mysql_num_rows($sql);
?>
Suzanne posted this at 00:36 — 11th November 2003.
She has: 5,507 posts
Joined: Feb 2000
Well, no, but I do find that I use exit; a lot for testing, lol...
Perhaps Mark or Mark knows.
What's your actual error?
kb posted this at 00:49 — 11th November 2003.
He has: 1,380 posts
Joined: Feb 2002
it says, and i quote:Parse error: parse error in .../index.php on line 7
which, unless i counted wrong, is the num rows line
Mark Hensler posted this at 07:18 — 11th November 2003.
He has: 4,048 posts
Joined: Aug 2000
I see nothing wrong with the num_rows line.
Parse errors can be caused by unpaired quotes '' "", parenthesis (), brackets [], braces {}, and backticks (or accent graves) `` on lines preceeding the error line. That said, the more code you paste here, the better we can help you.
Here's the loop Suzanne mentioned, and the corrected echo strings.
(disclaimer: I believe it's correct, but haven't tested it.)
<?php
$sql = mysql_query(\"SELECT * FROM data\");
while ($qry = mysql_fetch_array($sql)) {
echo \"Husband's Name: $qry[hname] \n\";
echo \"Wife's Name: $qry[wname] \n\";
echo \"Address: $qry[address] \n\";
echo \"City: $qry[city] \n\";
echo \"State: $qry[state]\n\";
echo \"Zip Code: $qry[zip] \n\";
echo \"Phone Number: $qry[phone1] $qry[phone2] $qry[phone3]\n\";
echo \"Email Address: $qry[email] \n\";
echo \"Membership Type: $qry[family] \n\";
echo \"Child 1: $qry[child1] \n\";
echo \"Child 1 Birthday: $qry[month1] $qry[day1] $qry[year1] \n\";
echo \"Child 2: $qry[child1] \n\";
echo \"Child 2 Birthday: $qry[month2] $qry[day2] $qry[year2] \n\";
echo \"Child 3: $qry[child3]\n\";
echo \"Child 3 Birthday: $qry[month3] $qry[day3] $qry[year3]\n\";
echo \"Child 4: $qry[child4] \n\";
echo \"Child 4 Birthday: $qry[month4] $qry[day4] $qry[year4] \n\";
}
?>
Mark Hensler
If there is no answer on Google, then there is no question.
kb posted this at 00:01 — 12th November 2003.
He has: 1,380 posts
Joined: Feb 2002
awesome. thanks.
nike_guy_man posted this at 00:47 — 12th November 2003.
They have: 840 posts
Joined: Sep 2000
Doesn't mysql_fetch_array use up twice the CPU required for mysql_fetch_assoc?
I only use mysql_fetch_assoc now...
I'd also suggest putting an extra \n at the end so you can see between the different entries. Either that, or put it in a table
Suzanne posted this at 00:53 — 12th November 2003.
She has: 5,507 posts
Joined: Feb 2000
Where would you get stats on that? I'm curious to read more about this.
nike_guy_man posted this at 00:55 — 12th November 2003.
They have: 840 posts
Joined: Sep 2000
It was a while ago, but mysql_fetch_array takes a full array and assigns all rows as $var['row'] and $var['#'] with the number being the row number
mysql_fetch_assoc takes an associative array, just $var['row']
Right?
Suzanne posted this at 00:57 — 12th November 2003.
She has: 5,507 posts
Joined: Feb 2000
Specifically, it seems to not do anything faster in particular, and in fact may be SLOWER... if you believe php.net, that is... http://ca2.php.net/manual/en/function.mysql-fetch-assoc.php
Interesting, though. Reading it again to understand the subtleties...
Further edit: seems that it's not significantly slower than fetching one row at a time... Hmm. That is it's a bit (maybe?) slower than http://ca2.php.net/manual/en/function.mysql-fetch-row.php ? *sigh* I'm officially confused.
Suzanne posted this at 00:58 — 12th November 2003.
She has: 5,507 posts
Joined: Feb 2000
Wouldn't Kyle's script here require mysql_fetch_array, though?
Edit: hmmm... nope. Cool!
Interesting the release of the data, too.
kb posted this at 01:57 — 12th November 2003.
He has: 1,380 posts
Joined: Feb 2002
umm yea....lol
nike_guy_man posted this at 02:00 — 12th November 2003.
They have: 840 posts
Joined: Sep 2000
Suzanne I think you got kyle and i both confused here
Are you saying we should use it?
I've used it since I found out about it about a year ago.
Of course, I can't tell the difference between 1.04s and 1.2s in execution time (arbitrary numbers, no meaning)
Suzanne posted this at 02:10 — 12th November 2003.
She has: 5,507 posts
Joined: Feb 2000
lol, yes, I think I am saying that it's an equally fine choice.
mysql_fetch_row();
mysql_fetch_assoc();
mysql_fetch_array();
They all seem to do relatively the same thing, depending on what you want to have happen. You can limit mysql_fetch_array to mimic either mysql_fetch_row mysql_fetch_array($sql,MYSQL_NUM) or mysql_fetch_assoc mysql_fetch_array($sql,MYSQL_ASSOC) -- it seems that the specialized fetches came about to replace the second argument for mysql_fetch_array itself.
I don't see anything that indicates that anything is significantly slower or faster, the key word being significantly. I would assume that you can use whichever you please that works best for you and your script.
Mark Hensler posted this at 02:34 — 12th November 2003.
He has: 4,048 posts
Joined: Aug 2000
I think the only time differnce is a few extra low level instructions, which cannot really be timed.
m3rajk posted this at 04:25 — 12th November 2003.
They have: 461 posts
Joined: Jul 2003
ok. ignoring the discussion about the time difference....
eskater: i know of two that will tell you the line number and column number if you want. however they are programming editors so they do much more than php,... they also do html, js, xhtml, c, c++, java, perl, scheme, lisp, python, and a large number of others i can't remember at this time.
xemacs.com is the one i prefer. you might prefer emacs, found at gnu.org (note, this is a posix oriented site, however, even though both of these were originally posix, they both have windows ports.)
if you haven't switched to mark's suggestion of a while loop, i'd like to state that if you're checking that the number of rows is greater than 0, that's fine, but if you want to use a foreach, you need to have an array. a for loop can use the number of rows, but a foreach is on an array, not a number. that may have been your error.
mark's suggestion removes the need for the checking on the number of rows, as well as looping regaurdless
POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.