trying to get returns on html docs

mmi's picture

They have: 457 posts

Joined: Jan 2001

I get this error:

Quote: Parse error: parse error, expecting `T_WHILE' in /home/ememi/ememi-www/tmp/catch.php3 on line 11

from a page containing this code:

<html><body>

<?php $db = mysql_connect("localhost", "ememi", "password");

mysql_select_db("mydb",$db);

$result = mysql_query("SELECT recipes FROM RECIPES where ing1='cheese'",$db);

do while (
$myrow = mysql_fetch_row($result))
                               {
printf("recipes: %s<br>\n",$myrow[0]);}?>

</body></html>
'

when I run this inside the interface provided by my host, I get a return - does anyone recognize this? - thanks

[Edited by mmi on Jan. 28, 2001 at 11:57 PM]


Web Xpertz Community Forums for Webmasters & Developers

Where You Can Learn, Advise, and Have Fun in the Process

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

hehehe, I've done this myself before. You program VB or ASP, don't you. do while is a VB or ASP function. The PHP equivalent is while (not 'do').

How's that for turn around time?
Good Luck,

(Peter, close mmi's [ code ])

Mark Hensler
If there is no answer on Google, then there is no question.

mmi's picture

They have: 457 posts

Joined: Jan 2001

what took ya so long Wink - this is weird - I get no display in N4.72 for this page although the code is there on view - in IE, I get a strange-lookin' black-and-white box - I guess I didn't close the "code" tag - anyway, thanks a lot - I may have MANY similar questions Wink


Web Xpertz Community Forums for Webmasters & Developers

Where You Can Learn, Advise, and Have Fun in the Process

mmi's picture

They have: 457 posts

Joined: Jan 2001

I originally used "while" but that gets me

Warning: Supplied argument is not a valid MySQL result resource in /home/ememi/ememi-www/tmp/catch.php3 on line 9

let me quickly disabuse you of the notion that I program in ANY language!! Wink - thanks for your help


Web Xpertz Community Forums for Webmasters & Developers

Where You Can Learn, Advise, and Have Fun in the Process

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Because I develope many pages that use the same database, I write one connection string, put it in a function, and in its own file. Then, anywhere I need to connect to that DB, I include "db_name.php" and run db_connect(). If I ever move the database to another server, change username/passwords, or anything else, I can fix everything in that one page.

try this instead:

function db_connect () {

## In the line below, insert: ("host", "ememi", "password"); ##
$link = mysql_pconnect ("host", "user", "password");

## In the line below, insert ("your_database_name") ##
if ($link && mysql_select_db ("mydb")) {
return ($link);
}
else {
return (FALSE);
}
}

/* blah (more code) blah */

db_connect()
   or die("Error connecting to database");

$result = mysql_query("SELECT recipes FROM RECIPES where ing1='cheese'");

do while ($myrow = mysql_fetch_row($result))
{printf("recipes: %s<br>\n",$myrow[0]);}
'
you only need the $db if you plan on having more than one DB connection at the same time. In which case, the code would look like this:
$db = db_connect()
   or die("Error connecting to database");

$result = mysql_query("SELECT recipes FROM RECIPES where ing1='cheese'",$db);
'
The only problem in the code you provided (other than the 'do') was that you forgot to set $db equal to a database link (mysql_select_db("mydb",$db)Wink.

(same day delivery, lol)
Good Luck,

Mark Hensler
If there is no answer on Google, then there is no question.

mmi's picture

They have: 457 posts

Joined: Jan 2001

SUCCESS - got a return!!! Smiling

recipes: Crescent Brie with Almonds
recipes: Gougères (Cheese Puffs)
recipes: Pita and Cheese Toasts
recipes: Cheese Straws

honestly can't thank you enough - you've given me a big boost when I really needed one - (I was pullin' for the Gnats Laughing out loud ) - I'll likely be back for help formatting this return :eek: - THANKS!! THANKS!! THANKS!!


Web Xpertz Community Forums for Webmasters & Developers

Where You Can Learn, Advise, and Have Fun in the Process

mmi's picture

They have: 457 posts

Joined: Jan 2001

can anyone help me get started on troubleshooting this?

<html><head><title>PHP/SQL Test 2</title>

<STYLE TYPE="text/css"><!--

BODY {font: 10pt Tahoma; color: #000080}
SPAN {font: 500 14pt Tahoma; color: #f4c508}

--></STYLE>

<body background="images/bback1.jpg" bgcolor="#ffffff" text="#000000" leftmargin="0"
topmargin="0" marginwidth="0" marginheight="0" id=all>

<?php $db = mysql_connect("localhost", "ememi", "password");

mysql_select_db("ememi_com",$db);?>


<SPAN>ALL RECIPES</SPAN><BR>

<?

$result = mysql_query("SELECT * FROM recipes",$db);

while ($row = mysql_fetch_array($result))

{

//INFO IS IN AN ARRAY CALLED $row. $row[0] IS THE FIRST COLUMN, $row[1] IS THE SECOND COLUMN, AND SO ON.

?>

<table>
  <tr>
    <td><a href="<? echo $row[4]; ?>"><? echo $row[0]; ?></a></td>
  </tr>
  <tr>
    <td>Ingredients for this recipe are: <? echo "$row[1],$row[2],$row[3]"; ?><td>
  </tr>
</table><br>

<?

}

?>

</body></html>
'I'm getting
Quote: Warning: Supplied argument is not a valid MySQL result resource in /home/ememi/ememi-www/tmp/catch3.php3 on line 22

this is line 22while ($row = mysql_fetch_array($result))'Thanks


Web Xpertz Community Forums for Webmasters & Developers

Where You Can Learn, Advise, and Have Fun in the Process

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Your syntax looks fine. Make sure that the table 'recipes' exists in the 'ememi_com' database. Check your username/password.

Try this to check all your connections stuff....

<?
$db = mysql_connect("localhost", "ememi", "password");

if ($db && mysql_select_db("ememi_com",$db)) {
  //all is well..
}
else {
  die("Error connecting to database");
}
?>
'

Good Luck,

Mark Hensler
If there is no answer on Google, then there is no question.

mmi's picture

They have: 457 posts

Joined: Jan 2001

oh yeah, RECIPES is in there - and I just use "password" to substitute for my maximum-security ultra-secret REAL password :eek: - I really appreciate you helping me with this - I have no real clue as I said before
-----------
hold on - I may have it
-----------
you're a gentleman and a programmer - you hit it right on, buddy - the db is "RECIPES" not "recipes" - believe it or not I kinda recognize the error message now - I oughta recognize 'em - I see 'em often enough Roll eyes - THANKS!!! - I may be back for formatting help so better let the machine pick up, if ya know what I mean Laughing out loud btw, here's the page - guess you're kind of a godfather Wink

[Edited by mmi on Feb. 01, 2001 at 05:04 PM]


Web Xpertz Community Forums for Webmasters & Developers

Where You Can Learn, Advise, and Have Fun in the Process

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

anytime... glad I could help!
[formating is fun... ;)]

mmi's picture

They have: 457 posts

Joined: Jan 2001

ok, counselor, time to move in for the...creation - I wanna use this db in a search engine on this page - [grab some popcorn, hit search and enjoy the film!! - off-center because it's outside its pop-up window] - as usual, I'm clueless here and basically just looking to get started in the right direction - like a framework / gameplan - I want to generate a list of links to recipes containing an ingredient typed into the textbox by a viewer - here's the code I have so far

<html><head><title>PHP/SQL Test 2</title>

<STYLE TYPE="text/css"><!--

BODY {font: 10pt Tahoma; color: #000080}
SPAN {font: 500 14pt Tahoma; color: #f4c508}

--></STYLE>

<body background="images/bback1.jpg" bgcolor="#ffffff" text="#000000" leftmargin="0"
topmargin="0" marginwidth="0" marginheight="0" id=all>

<?php $db = mysql_connect("localhost", "ememi", "password");

mysql_select_db("ememi_com",$db);?>


<SPAN> ALL RECIPES</SPAN><BR>

<?

$result = mysql_query("SELECT * FROM RECIPES",$db);

while ($row = mysql_fetch_array($result))

{

//INFO IS IN AN ARRAY CALLED $row. $row[0] IS THE FIRST COLUMN, $row[1] IS THE SECOND COLUMN, AND SO ON

?>

<table>
  <tr>
    <td><a href="<? echo $row[5]; ?>" target="Introduction"><? echo $row[1]; ?></a></td>
  </tr>
  <tr>
    <td>
      <table>
        <tr><td valign="top">keyword(s):</td>
            <td><? echo $row[2]; ?><br>
                <? echo $row[3]; ?><br>
                <? echo $row[4]; ?></td></tr>
      </table></td>
  </tr>
</table><br>

<?}?>

</body></html>
'maybe the easiest way to show you what I'm working with is to give you a screenshot of the db - any ideas?


Web Xpertz Community Forums for Webmasters & Developers

Where You Can Learn, Advise, and Have Fun in the Process

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

thx for the screen shot, it helps to visualize where I'm pulling from...

here is some ruff code..

<!-- results page: results.php-->
<html>
<body bgcolor="#ffffff" text="#000000" id=all>
<?
$db = mysql_connect("localhost", "ememi", "password");
mysql_select_db("ememi_com",$db);

$query = "SELECT * FROM RECIPES WHERE (ing1 LIKE '%$ingrediant%') OR (ing2 LIKE '%$ingrediant%') OR (ing3 LIKE '%$ingrediant%')";

$result = mysql_query($query,$db);

while ($recipe = mysql_fetch_array($result))
{
?>
<table>
  <tr>
    <td><a href="<? echo $recipe[href]; ?>" target="Introduction"><? echo $recipe[recipe]; ?></a></td>
  </tr>
  <tr>
    <td>
      <table>
        <tr><td valign="top">keyword(s):</td>
            <td><? echo $recipe[ing1]; ?><br>
                <? echo $recipe[ing2]; ?><br>
                <? echo $recipe[ing3];?></td></tr>
      </table></td>
  </tr>
</table>
<br>
<?
} #END while
?>
</body></html>
'
<!--input search page: search.php-->
<html>
<body bgcolor="#ffffff" text="#000000" id=all>
<form action="results.php" method=POST>
<input type=text name=ingrediant><BR>
<input type=submit value="Go get it!">
</form>
</body>
'

What's happening...
A % in the query is a wild card. So I put a % before and after the ingrediant that the user is looking for. This way they can enter 'berry' and get results on 'blue berry' or 'raspberry'... If you want an exact phrase, take out all %.

Also note the way I handle the mysql_fetch_array... I have it assigned to $recipe. What I want to point out is that I am not using numbers to address fields. I am using the field name. To read more about this, click here.

Mark Hensler
If there is no answer on Google, then there is no question.

mmi's picture

They have: 457 posts

Joined: Jan 2001

definite progress, my friend - when ya type an ingredient (say, "cheese" Wink ) into this page, there's the recipes with cheese as an ing!!! - I think maybe the best thing for me to do (to preserve forum capital if nothing else) is to play around with this new toy you've handed me to see if I can format it up the way I picture it in my mind (can't let you have ALL the fun Wink ) - I may get nowhere and almost certainly won't get to the final destination without more indispensible help, so I'm sure I'll be back - btw, my brother is an overpaid underworked Oracle dba who says he can't help me much ("sorry, I don't program anymore" - makes sense - he seems to just sit around waiting for systems that never fail to...ah...fail Roll eyes ) and who MAY decide I'm no longer just a "worthless commie troublemaker" when I show him this - then I'll tell him it's just another triumph for "real" socialism - the kind practiced in a wonderful community like this forum by friendly, helpful, knowledgable people like you - HA!! take THAT ya country-clubber!!! Sticking out tongue Wink THANKS!!! THANKS!!! THANKS!!!


Web Xpertz Community Forums for Webmasters & Developers

Where You Can Learn, Advise, and Have Fun in the Process

mmi's picture

They have: 457 posts

Joined: Jan 2001

can ya give me one more on this (so much for that "lemme do it myself talk Roll eyes ) - I can't figure out howta get the titles to come up in the return - anything???
-------
hold on - may have an idea
-------
nope - nuthin' Sad
-------
seem to have it now!!! Laughing out loud

[Edited by mmi on Feb. 03, 2001 at 02:10 AM]


Web Xpertz Community Forums for Webmasters & Developers

Where You Can Learn, Advise, and Have Fun in the Process

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

LOL!! so, does it work now? Wink

I would love to get paid to just sit around waiting... Laughing out loud

mmi's picture

They have: 457 posts

Joined: Jan 2001

go to Cooking My Way

click on "index" in the navbar

type in an accepted keyword, e.g., cheese

voilà!!

just need to set up a timeout to let the apple spin for a couple 'a seconds - I'm kinda concerned though cuz when I try to put the code back the way it was the movie displays but it's static Confused


Web Xpertz Community Forums for Webmasters & Developers

Where You Can Learn, Advise, and Have Fun in the Process

mmi's picture

They have: 457 posts

Joined: Jan 2001

well, here's one - how would I direct viewers to a page that says "sorry, nuthin' on that"? -I'm wonderin' if this is gonna be complicated by the use of LIKE - I know a little about db's and SQL and I'm guessing that working out the final details on this may get complicated - thanks
---------
do I wanna test to see if the return on ingredient is null and then print a string? - is this something to be handled in SQL, PHP, js? - easily Confused apparently Wink

[Edited by mmi on Feb. 06, 2001 at 12:58 AM]


Web Xpertz Community Forums for Webmasters & Developers

Where You Can Learn, Advise, and Have Fun in the Process

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Make this change to the query string....

// old
$query = "SELECT * FROM RECIPES WHERE (ing1 LIKE '%$ingrediant%') OR (ing2 LIKE '%$ingrediant%') OR (ing3 LIKE '%$ingrediant%')";

// new
$query = "SELECT * FROM RECIPES WHERE (ing1 LIKE '%$ingrediant%') OR (ing2 LIKE '%$ingrediant%') OR (ing3 LIKE '%$ingrediant%')";
$query .= " AND (ing1 IS NOT NULL) AND (ing2 IS NOT NULL) AND (ing31 IS NOT NULL)";
'
then, check to see if you have any results....
if (mysql_num_rows($result) == 0) {
  // "Sorry nuthin' on that"
}

while ($recipe = mysql_fetch_array($result) {
  // if there are no rows returned, this part will never be used
  //  so don't bother putting this in an IF statement

  // blah (do your loop thing here) blah
}
'

Mark Hensler
If there is no answer on Google, then there is no question.

mmi's picture

They have: 457 posts

Joined: Jan 2001

thanks a lot Laughing out loud - I'll see what I can make 'a that mess after my nightly nap Roll eyes Wink - Produce Theater Productions is apparently gettin' struck by the Teamsters at the moment - just wonderin', you got any ideas on how to get my flippin' apple integrated into this? - I've been advised to move it from the "search" page to the "results" page? - does that seem more workable to you?


Web Xpertz Community Forums for Webmasters & Developers

Where You Can Learn, Advise, and Have Fun in the Process

mmi's picture

They have: 457 posts

Joined: Jan 2001

this is great!! HOWEVER Wink , how should I try to handle the lollowing? - if ya type in, say, "parsnip", I get the response I want - but is there any way to better manage an entry like "pa"? - right now I get Lasagna cuz it's got pasta and a chicken dish that features paprika - is a return like this the price I MUST pay for using the wildcards or is there a way to limit things and get a more "reasoned" response? btw, I suppose I'll be at least morally obligated to credit your contribution to CMW - or would that be an honor of dubious distinction? Laughing out loud
----------
guess I'll hafta dig out my Oracle / SQL books :eek:


Web Xpertz Community Forums for Webmasters & Developers

Where You Can Learn, Advise, and Have Fun in the Process

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I'm not sure what you want to do. What do you want "pa" to return? nothing?

you can do a strict search by taking out the wildcards. So "pa" will not return anything, and "salt" will only match "salt" and not "saltines".

If you want you could get even wilder, and allow the user to choose 'partial word' matches, 'exact phrases', or 'any word'.

Example... search criteria: "apple pie"
Partial word would break up the spaces and search for anything with "apple" or "pie" in it. This could return "apple", "pie", "apple pie", and "pineapple".
Exact phrase would search for just that... the exact phrase. and return only "apple pie".
Any word would split the spaces and search for "apple" or "pie". And return "apple", and "pie". But not "pineapple"

If you want a search that powerful, it will require a bit of ASP coding. So make a new post in the 'Other Scripting Languages Help Forums'.

I'm drawing a blank... what's CMW?

Mark Hensler
If there is no answer on Google, then there is no question.

mmi's picture

They have: 457 posts

Joined: Jan 2001

thanks as always for the useful info - my understanding is that ASPs can only run on Windows NT servers and since my provider has those "no longer patchy" ones, I figger I can't do 'em - I kinda expected that there wasn't gonna be a way around the limitations of these wildcards but at least now I KNOW that's the case and I can express an intelligent opinion about an alternative - you really helped me out on this to say the least and I very much appreciate it - CMW is Cooking My Way - ya might wanna check out the navbar's index !! Cool


Web Xpertz Community Forums for Webmasters & Developers

Where You Can Learn, Advise, and Have Fun in the Process

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

looking good Laughing out loud

mmi's picture

They have: 457 posts

Joined: Jan 2001

Hey

Like a voice (hopefully not too nightmarish) from the somewhat distant past, I've returned to yer friendly community in an effort to get the little search engine on mmy recipe page working again. It stopped working a few years ago when I moved the code to a different server. Yesterday, I again tried changing the username — and it worked!
Smiling

Well, I got a result at least. The problem is: the code designed to cause a subset of the recipes to be returned (those with an ingredient matching the string entered in the textbox) isn't functioning. You instead get all the recipes held in the db (currently only about fifteen or so that I've entered for testing).

Here's the search page. Here's the code for results.php

<html><head><title>CMW Search Results for <? echo $ingredient ?></title>

<SCRIPT LANGUAGE=javascript TYPE="text/javascript">

function openCmwWindow(){
    cmwWindow=window.open('','cmwWin','menubar=yes,status=yes,scrollbars=no,resizable=yes,width=740,height=420');}

</script>

<STYLE TYPE="text/css"><!--

BODY {font: 10pt Tahoma; color: #fffff0}
SPAN {font: 500 13pt Tahoma; color: #f4c508}
A {font: 600 14pt Tahoma; color:orange; text-decoration:none}

--></style>

</head>

<body bgcolor="#000080" text="#000000" id=all>

<div width="95%" align="right"><a href="http://ememi.com/cmw/realindex.html">another<br>search </a></div>

<?

$db = mysql_connect("localhost", "username","password");

mysql_select_db("mmi_ememicom",$db);

$query = "SELECT * FROM RECIPES WHERE (ing1 LIKE '%$ingredient%') OR (ing2 LIKE '%$ingredient%') OR (ing3 LIKE '%$ingredient%')";

$query .= " AND (ing1 IS NOT NULL) AND (ing2 IS NOT NULL) AND (ing3 IS NOT NULL)";

if (mysql_errno()) { echo mysql_error(); }

$result = mysql_query($query,$db);

if (mysql_num_rows($result) == 0) {

?> <SPAN> <? echo "Sorry, no recipes for that - try another ingredient"; ?> </span>

<?

}

while ($recipe = mysql_fetch_array($result)) {

?>

<a href="<? echo $recipe[href]; ?>" target="cmwWin"><? echo $recipe[recipe]; ?></a>

<a href="<? echo $recipe[href]; ?>" target="cmwWin"><span><? echo $recipe[recipes]; ?></span></a><br><br>

<?

}  #END while

?>

</body></html>

Anything jump out as a reason for all recipes being returned?

Thanks!


Web Xpertz Community Forums for Webmasters & Developers

Where You Can Learn, Advise, and Have Fun in the Process

mmi's picture

They have: 457 posts

Joined: Jan 2001

I was handed a solution by a colleague:

add $ingredient = $_REQUEST['ingredient'];

WORKS!
Smiling

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.