View Full Version : PHP Questions
MartynJ
Tue 4th Jun '02, 4:17pm
OK, not exactly vBulletin related, but all the actual PHP forums on the net are crap. Basically, I am building a site for someone from scratch, taking the content of their HTML site and entering it into a MySQL database. I have started learning PHP and can connect to the database, read strings from it and dump them onto a page. What I would like to do is say "Query this, and call the result $nameofresult or whatever so that I can go back and make vB style pages with HTML code that references these.
The biggest problem is that for the database, I have the URL of an image, but want to call that from the database by PHP and call it $image1url or whatever and then get the PHP to drop it into
< IMG SRC = "$image1url " > etc
Help, please?
scoutt
Tue 4th Jun '02, 4:52pm
then just put the path to the image in the database. and call it like you just suggested.
MartynJ
Tue 4th Jun '02, 5:12pm
Yeah, but thing is, I know heck all about PHP. Can someone please post up how to call a path from a database, then tie it to a variable name, then insert that variable into standard HTML?
scoutt
Tue 4th Jun '02, 7:38pm
easy. the path that is in the atabase could be something like /images/img.jpg
$sql = "SELECT * FROM image_table'"; // queries the database for the image paths
$result = mysql_query ($sql);
if (!$result) {echo mysql_error();} // detects to see if thre is an error and then prints it out.
while ($row = mysql_fetch_array($result)) // gets the table row
{
$path = $row["path"];
}
echo "<img src=\"$path\" border=0>";
MartynJ
Tue 4th Jun '02, 8:12pm
Thanks mate, I appreciate it. Though, watch this space for any further problems I have. :rolleyes: :confused:
Dan615
Tue 4th Jun '02, 10:02pm
Originally posted by scoutt
easy. the path that is in the atabase could be something like /images/img.jpg
$sql = "SELECT * FROM image_table'"; // queries the database for the image paths
$result = mysql_query ($sql);
if (!$result) {echo mysql_error();} // detects to see if thre is an error and then prints it out.
while ($row = mysql_fetch_array($result)) // gets the table row
{
$path = $row["path"];
}
echo "<img src=\"$path\" border=0>";
Since I'm bored, I'm gonna reformat the code the way I like it...I think MY way is more efficient ;) Just kidding :p
$result = mysql_query("select image_path from the_table") or die(mysql_error()); // get the image_path field from the_table (the name of the table)
while ($image = mysql_fetch_array($result)) {
echo "<img src=\"$image[image_path]\">"; // print the image
}
Actually, your code wouldn'r run correctly, the $path variable gets re-assigned every time it loops through the rows...so only one image would get printed, instead of all of them :)
I'm not poking fun, just pointing it out :D
scoutt
Wed 5th Jun '02, 1:41am
good catch dan, but he didn't say he wanted more than 1 image ;)
and I just threw that up there as I know there is several ways of doing it. that was a piece I had handy :)
MartynJ
Wed 5th Jun '02, 8:25am
Well, thanks guys for all your help. Basically, I am making a site in PHP/MySQL about a tv series, I have an SQL table called episodes, and in each unique ID I have fields for "epTitle", "epReview", "epRating" and "epImage" etc, what I want to be able to do is create a single PHP page that can accept URL parameters from a search form and build up a list of matching results by filling in the search result data into predesigned HTML tags, looping if necessary until all matching results are shown. Help? :)
Dan615
Wed 5th Jun '02, 11:14pm
Bah, that's easy ;) Unfortunately, I'm in the midst of doing something else, but I'll get back to you...
In the mean time, what version of PHP and what version of MySQL are you running?
scoutt
Thu 6th Jun '02, 1:14am
if it was so easy why couldn't you spend the 2 minutes it took to write it and do it ;)
it is all ready done for you. from Dan's post. :)
if($_REQUEST["submit"]){
// make sure search word doesn't contain special characters
$searchword = htmlspecialchars($_REQUEST['searchword']);
$result = mysql_query("select epTitle, epReview, epRating, epImage from the_table where epTitle = '".%$searchword%."' ") or die(mysql_error()); // get the everything field from the_table (the name of the table) that = the search word
while ($row = mysql_fetch_array($result)) {
echo "Title = ". $row["epTitle"];
echo "Review: ". $row["epReview"];
echo "Rated at: ". $row["epRating"];
echo "Title = ". $row["eptitle"];
echo "<img src=\"".$row["epImage"]."\">"; // print the image
}
}
//then you have your html form here, which I think you can do.
//just make the filed you search from be named searchword
// very easy. if you don't know how to do any of this then I would study
// up on php for a bit.
MartynJ
Thu 6th Jun '02, 7:06pm
OK, so I can use that PHP above me to gather the info and pass it to another page in the form of a URL referral? Well, what I want is a page that will contain an HTML form, that passes the info to something like "searchresults.php" - which I have mocked up in HTML at this location. (http://www.somethingreal.net/test/) - so you can see what I am trying to achieve. Any ideas on how best (easiest) to go about this?
scoutt
Thu 6th Jun '02, 8:09pm
so what is hard about that. you have your html page with the form in it
<form action="searchresults.php" method=post>
<input type="text" name="searchword">
<input type="submit" name="submit" value="send">
</form>
then when it gets subitted it goes to the searchresults page with the last code I showed in my last post.
<?
if($_REQUEST["submit"]){
// make sure search word doesn't contain special characters
$searchword = htmlspecialchars($_REQUEST['searchword']);
?>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="65%" id="AutoNumber1">
<?
$result = mysql_query("select epTitle, epReview, epRating, epImage from the_table where epTitle = '".%$searchword%."' ") or die(mysql_error()); // get the everything field from the_table (the name of the table) that = the search word
while ($row = mysql_fetch_array($result)) {
echo "Title = ".
echo "Review: ".
echo "Rated at: ".
echo "Title = ". $row["eptitle"];
echo // print the image
echo<<<myhtml
<tr>
<td width="26%"><u><font face="Verdana" size="2"><b>$row["epTitle"]</b></font></u></td>
<td width="174%"> </td>
</tr>
<tr>
<td width="26%"><font face="Verdana" size="1">$row["epRating"]</font></td>
<td width="174%">
<font size="1"><img src="$row["epImage"]"> </font></td>
</tr>
<tr>
<td width="26%"> </td>
<td width="174%"><font size="1">$row["epReview"]</font></td>
</tr>
<tr>
<td width="26%"> </td>
<td width="174%"> </td>
</tr>
<tr>
<td width="26%"><font size="1">Result1</font></td>
<td width="174%"><a href="review.php&id=$row["id"]"><font size="1">eplongreview.php&$id=</font></a></td>
</tr>
myhtml;
}
echo"</table>";
}
then you have the review page that gets the id from that link.
MartynJ
Fri 7th Jun '02, 4:33pm
Tried it with that code, get parse error when attempting to run it, on line 10. I added in the database details as per other simpler pages I have made, can anyone see the particular problem on line 10?
<?
if($_REQUEST["submit"]){
// make sure search word doesn't contain special characters
$searchword = htmlspecialchars($_REQUEST['searchword']);
?>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="65%" id="AutoNumber1">
<?
$db = mysql_connect("localhost","root");
mysql_db_select("sg1",$db);
$result = mysql_query("SELECT epTitle, epReview, epRating, epImage1 FROM episodes WHERE epTitle = '".%$searchword%."'"); // get the everything field from the_table (the name of the table) that = the search word
while ($row = mysql_fetch_array($result)) {
echo "Title = ".
echo "Review: ".
echo "Rated at: ".
echo "Title = ". $row["eptitle"];
echo // print the image
echo<<<myhtml
<tr>
<td width="26%"><u><font face="Verdana" size="2"><b>$row["epTitle"]</b></font></u></td>
<td width="174%"> </td>
</tr>
<tr>
<td width="26%"><font face="Verdana" size="1">$row["epRating"]</font></td>
<td width="174%">
<font size="1"><img src="$row["epImage"]"> </font></td>
</tr>
<tr>
<td width="26%"> </td>
<td width="174%"><font size="1">$row["epReview"]</font></td>
</tr>
<tr>
<td width="26%"> </td>
<td width="174%"> </td>
</tr>
<tr>
<td width="26%"><font size="1">Result1</font></td>
<td width="174%"><a href="review.php&id=$row["id"]"><font size="1">eplongreview.php&$id=</font></a></td>
</tr>
myhtml;
}
echo"</table>";
}
scoutt
Fri 7th Jun '02, 5:59pm
ahh sorry I left this in. you will have to delete it.
echo "Title = ".
echo "Review: ".
echo "Rated at: ".
echo "Title = ". $row["eptitle"];
echo // print the image
delete all those lines. sorry
MartynJ
Fri 7th Jun '02, 7:51pm
Nah, that makes no diff, it's line 10 that parses wrong - eg,
$result = mysql_query("SELECT epTitle, epReview, epRating, epImage1 FROM episodes WHERE epTitle = '".%$searchword%."'"); // get the everything field from the_table (the name of the table) that = the search word
scoutt
Fri 7th Jun '02, 9:49pm
but there is nothing wrong with that line....
Dan615
Fri 7th Jun '02, 10:34pm
Oh yes there is! The percent signs aren't a part of the PHP language, they're part of the out dated MySQL LIKE clause. it is now more appropriate to use something like
SELECT epTitle, epReview, epRating, epImage1 FROM episodes WHERE strcmp(epTitle, \"$searchword\") > -1
scoutt
Fri 7th Jun '02, 11:15pm
I beg to differ. I just forgot the LIKE in the select statement, I use it all the time in my code and it works beautifully.
but that would not have caused the parse error. doing what you have there will cause the search to be more precise and have the potential to not return anything. and doing it that way is case sensitive and most searches need to be case insensitive. besides we use the % in VB all the time when we query the access database, no different than mysql.
ahh I did goof. it should be like this.
$result = mysql_query("SELECT epTitle, epReview, epRating, epImage1 FROM episodes WHERE epTitle LIKE '%$searchword%' "); // get the everything field from the_table (the name of the table) that = the search word
that will work garrunteed
Dan615
Sat 8th Jun '02, 12:07am
Originally posted by scoutt
I beg to differ. I just forgot the LIKE in the select statement, I use it all the time in my code and it works beautifully.
but that would not have caused the parse error. doing what you have there will cause the search to be more precise and have the potential to not return anything. and doing it that way is case sensitive and most searches need to be case insensitive. besides we use the % in VB all the time when we query the access database, no different than mysql.
ahh I did goof. it should be like this.
$result = mysql_query("SELECT epTitle, epReview, epRating, epImage1 FROM episodes WHERE epTitle LIKE '%$searchword%' "); // get the everything field from the_table (the name of the table) that = the search word
that will work garrunteed
I never said it didn't exist in MySQL, I know that the LIKE clause exists...but I don't think that works with newer versions, i thought i saw it somewhere in the manual.
In your original code, you had the % signs outside the mysql expression, inside the PHP code, and that caused the parse error...not just because you forgot the LIKE clause ;)
This isn't a competition though :p
scoutt
Sat 8th Jun '02, 1:10am
that is correct, it shouldn't have had the ". ." as that was the error. but I jsut forgot the like so it wouldn't work anyway. :p
this is in the mysql manual
SQL pattern matching allows you to use `_' to match any single character and `%' to match an arbitrary number of characters (including zero characters). In MySQL, SQL patterns are case insensitive by default. Some examples are shown below. Note that you do not use = or != when you use SQL patterns; use the LIKE or NOT LIKE comparison operators instead.
To find names beginning with `b':
mysql> SELECT * FROM pet WHERE name LIKE "b%";
MartynJ
Sat 8th Jun '02, 9:36am
The error has gone, though now we have another in line 16, which is
<td width="26%"><u><font face="Verdana" size="2"><b>$row["epTitle"]</b></font></u></td>
The actual error reads:
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in c:\apache\htdocs\searchresults.php on line 14
I'm maybe off track (don't know much about PHP), but is is that $row hasn't been properly defined?
From the previous code, certain variables had to be clearly defined, and as I read the code below, it seems like $row is referenced to, but not set up before this? :confused:
while ($row = mysql_fetch_array($result)) {
As I said, that was the firest ref, which seems to say "So long as there is info from the query to feed into $row, pass the info to the table below and fill it in" or something like that.
scoutt
Sat 8th Jun '02, 1:54pm
my appologies, I wasn't sure in the heredoc syntax would except the " in the varialbe. try this
<td width="26%"><u><font face="Verdana" size="2"><b>$row['epTitle']</b></font></u></td>
you will have to change all of them that are like that in between the myhtml syntax.
MartynJ
Sat 8th Jun '02, 2:21pm
Still the same problem. Just thinking. The database connection part, where you define database name etc to $db - the user is specified as root, but I did not include pass as you do in something like vB config.php file. I have attached the php file here as it exists at the moment, and if I can, I will attach the sql too.
MartynJ
Sat 8th Jun '02, 2:24pm
Nah, needed a double post to also attach the sql, it's in this zip file. For ease of construction, I made it in phpMyAdmin. (in case that means anything to anyone) - oh, and thanks for your help so far guys, I really do appreciate it. A lot. Once I get going with PHP/MySQL myself I will be certain to help those with less knowledge myself...
scoutt
Sat 8th Jun '02, 2:32pm
well all though it works I don't like it, but try this.
<?
if($_REQUEST["submit"]){
// make sure search word doesn't contain special characters
$searchword = htmlspecialchars($_REQUEST['searchword']);
?>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="65%" id="AutoNumber1">
<?
$db = mysql_connect("localhost","root");
mysql_db_select("sg1",$db);
$result = mysql_query("SELECT epTitle, epReview, epRating, epImage1 FROM episodes WHERE epTitle LIKE '%$searchword%' "); // get the everything field from the_table (the name of the table) that = the search word
while ($row = mysql_fetch_array($result)) {
echo <<<myhtml
<tr>
<td width="26%"><u><font face="Verdana" size="2"><b>$row[epTitle]</b></font></u></td>
<td width="174%"> </td>
</tr>
<tr>
<td width="26%"><font face="Verdana" size="1">$row[epRating]</font></td>
<td width="174%">
<font size="1"><img src="$row[epImage1]"> </font></td>
</tr>
<tr>
<td width="26%"> </td>
<td width="174%"><font size="1">$row[epReview]</font></td>
</tr>
<tr>
<td width="26%"> </td>
<td width="174%"> </td>
</tr>
<tr>
<td width="26%"><font size="1">Result1</font></td>
<td width="174%"><a href="review.php&id=$row[id]"><font size="1">eplongreview.php&$id=</font></a></td>
</tr>
myhtml;
}
echo"</table>";
}
?>
MartynJ
Sat 8th Jun '02, 4:20pm
Yay! No more errors, but, now nothing at all. It just goes to a blank page. Even if I put in the *exact* phrase I know to exist in the table.
scoutt
Sat 8th Jun '02, 5:01pm
do you have a form on the same page? what is the whole page? can you upload it here
scoutt
Sat 8th Jun '02, 5:24pm
here this works. you had some problems with table names.
<?
echo "<html><head></head><body>";
if($_REQUEST["submit"]){
// make sure search word doesn't contain special characters
$searchword = htmlspecialchars($_REQUEST['searchword']);
echo"<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\" width=\"65%\" id=\"AutoNumber1\">";
$db = mysql_connect("localhost","root","*****");
mysql_select_db("DB_name");
$search = ("SELECT epName, epShortReview, epRating, epImage1 FROM episodes WHERE epName LIKE '%$searchword%' "); // get the everything field from the_table (the name of the table) that = the search word
$result = mysql_query($search);
if (!$result){echo mysql_error();}
$num = mysql_num_rows($result);
if ($num){
while ($row = mysql_fetch_array($result)) {
echo <<<myhtml
<tr>
<td width="26%"><u><font face="Verdana" size="2"><b>$row[epName]</b></font></u></td>
<td width="174%"> </td>
</tr>
<tr>
<td width="26%"><font face="Verdana" size="1">$row[epRating]</font></td>
<td width="174%">
<img src="$row[epImage1]"></td>
</tr>
<tr>
<td width="26%"> </td>
<td width="174%"><font size="1">$row[epShortReview]</font></td>
</tr>
<tr>
<td width="26%"> </td>
<td width="174%"> </td>
</tr>
<tr>
<td width="26%"><font size="1">Result1</font></td>
<td width="174%"><a href="review.php&id=$row[id]"><font size="1">eplongreview.php&$id=</font></a></td>
</tr>
myhtml;
}
}else{
echo "<tr><td>Sorry no results found";
}
echo"</table>";
}
?>
<form action="searchresults.php" method="POST">
<input type=text name="searchword">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
MartynJ
Sat 8th Jun '02, 7:14pm
The approach I was looking for was something like:
http://hostname/seach.htm
which posts data to
http://hostname/searchresults.php
This kinda weird, cause it's always "neaarly there". The searchresults.php should be exaclty as the demo at
http://www.somethingreal.net/test - reading results etc
scoutt
Sat 8th Jun '02, 8:27pm
that is fine, just make the form on the other page. I just had it there for testing purposes. it doesn't have to be there.
MartynJ
Sat 8th Jun '02, 9:05pm
Well, will try tomorrow, too tired, too drunk right now... Good old Sat night. Well, thanks anyway guys, really appreciate it.
Dan615
Sat 8th Jun '02, 9:44pm
quote:
--------------------------------------------------------------------------------
SQL pattern matching allows you to use `_' to match any single character and `%' to match an arbitrary number of characters (including zero characters). In MySQL, SQL patterns are case insensitive by default. Some examples are shown below. Note that you do not use = or != when you use SQL patterns; use the LIKE or NOT LIKE comparison operators instead.
To find names beginning with `b':
mysql> SELECT * FROM pet WHERE name LIKE "b%";
--------------------------------------------------------------------------------
so THAT was my problem! I just put WHERE LIKE, without the row name, and I was wondering what the hell I was doing wrong the whole time...AGH! thanx :)
MartynJ
Mon 10th Jun '02, 6:37pm
Help with this, anyone? vB team guys? Somebody? It's that close to working, yet doesn't... :(
scoutt
Mon 10th Jun '02, 9:57pm
so what is the problem now. you want help when it works and you don't say what the problem was.
MartynJ
Tue 11th Jun '02, 8:31am
My apologies. Well, as I think I might have posted, the errors are all gone, but the form returns no results for anything, regardless of what I search for, all I get is a blank page. I can set up a dyndns client on my comp to let you see what I mean?
scoutt
Tue 11th Jun '02, 11:17am
well like I said the last script I posted works. I used your sql from phpmyAdmin and made a database of all the shows. then ran that script. I found there was some table name problems so I changed it in the script. look at my last one and you can see it.
I have the script at home and I will put it up on my site so you can see it work.
MartynJ
Wed 12th Jun '02, 7:39pm
IS it on your site, I had a quick look about the one listed in your WWW link and don't see it?
scoutt
Wed 12th Jun '02, 10:25pm
sorry I got busy.
http://www.snippetlibrary.com/test/searchresults.php
search for something you think is in there.
MartynJ
Thu 13th Jun '02, 5:24pm
Thanks, I get the same local, though get a problem where it seems to be working sometimes, not others, like i get no results on a search one time, and if I run it again a second later get results. Thanks a lot. Had a few ideas about improving the form so people can search with more refined parameters etc, so will post that up here tomorrow some time I think. Thanks a lot for this.
vBulletin® v3.8.0 Beta 4, Copyright ©2000-2008, Jelsoft Enterprises Ltd.