Jump to content

Hangwire

Members
  • Posts

    41
  • Joined

  • Last visited

Everything posted by Hangwire

  1. I did this: $query = "SELECT * FROM people WHERE [FirstName] = \"%$trimmed%\" ORDER BY FirstName" This is the error it gives... Parse error: syntax error, unexpected T_NS_SEPARATOR in D:\Program Files\xampp\xampp\htdocs\search.php on line 32 I really can't figure this thing out! I tried several edits and still nothing..
  2. As far as I know, it selects the results from the table called people and orders them by the first block, in the case FirstName. EDIT: Thanks! I've made an edit to that and now the script works... kind of. $query = ("SELECT * FROM people ORDER BY FirstName"); Now the problem is that it gives me all the contents of the table and not what I searched for:
  3. Hi all. This is the code I've been trying to execute. <?php // Get the search variable from URL $var = @$_GET['q'] ; $trimmed = trim($var); //trim whitespace from the stored variable // rows to return $limit=10; // check for an empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; exit; } // check for a search parameter if (!isset($var)) { echo "<p>No search parameter</p>"; exit; } //connect to your database ** EDIT REQUIRED HERE ** mysql_connect("localhost","*","*"); //(host, username, password) //specify database ** EDIT REQUIRED HERE ** mysql_select_db("my_db") or die("Can't select database//Does not exist."); //select which database we're using // Build SQL Query $query = "select * from people \"%$trimmed%\" order by FirstName"; // EDIT HERE and specify your table and field names for the SQL query $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); // If we have no results, offer a google search as an alternative if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>"; // google echo "<p><a href=\"http://www.google.com/search?q=" . $trimmed . "\" target=\"_blank\" title=\"Look up " . $trimmed . " on Google\">Click here</a> to try the search on Google</p>"; } // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for: "" . $var . ""</p>"; // begin to show results set echo "Results"; $count = 1 + $s ; // now you can display the results returned while ($row= mysql_fetch_array($result)) { $title = $row["FirstName"]; echo "$count.) $title" ; $count++ ; } $currPage = (($s/$limit) + 1); //break before paging echo "<br />"; // next we need to do the links to other results if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< Prev 10</a>&nbsp "; } // calculate number of pages needing links $pages=intval($numrows/$limit); // $pages now contains int of pages needed unless there is a remainder from division if ($numrows%$limit) { // has remainder so add one page $pages++; } // check to see if last page if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { // not last page so give NEXT link $news=$s+$limit; echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>"; } $a = $s + ($limit) ; if ($a > $numrows) { $a = $numrows ; } $b = $s + 1 ; echo "<p>Showing results $b to $a of $numrows</p>"; ?> As you can see, it's pretty simple and it's got very nice explanations of what everything does. But when I try it, I get this. I'm pretty much stuck. Any ideas what that error means? And yes, there is a entry with the name of Ella in the people table of the database. Help? :-\
  4. Hi all, I have a question. How do I put a new line in a script? I've tried everything. <?php $con = mysql_connect("localhost","*","*"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $sql="SELECT * FROM People WHERE FirstName='Borko' AND LastName='Borkov'"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "Found!"; echo($sql); mysql_close($con) ?> I wrapped it in <html> and so on, <br /> doesn't work. Saved it in html, <br /> still doesn't work and the script doesn't execute. The output I get right now is I want it to display And another question - how can I see what the sql variable is searching for from the browser? For now, as you have seen, I just use the echo($sql) but it doesn't quite cut it - is there a way to see just FirstName and LastName without the whole code? I tried echo($sql [FirstName] [LastName], as you've already guessed it didn't work... A hint would also be greatly appreciated! Any help on these two issues? :-\
  5. bump?
  6. Hi, I need a quick tip. I want this script to produce an output of "Table created" if it succesfully creates a table. <?php $con = mysql_connect("localhost","****","****"); if (!$con) { die('Could not connect: ' . mysql_error()); } // Create table mysql_select_db("my_db", $con); $sql = "CREATE TABLE People ( FirstName varchar(15), LastName varchar(15), Age int )"; // Execute query mysql_query($sql,$con); mysql_close($con); ?> I tried to do it on my own, but I guess I'm still completely new to php I tried: <?php $con = mysql_connect("localhost","****","****"); if (!$con) { die('Could not connect: ' . mysql_error()); } // Create table if (mysql_select_db("my_db", $con)) { echo "Table created"; } else { echo "Error creating Table: " . mysql_error(); } $sql = "CREATE TABLE People ( FirstName varchar(15), LastName varchar(15), Age int )" // Execute query mysql_query($sql,$con); mysql_close($con); ?> It gave me an error with the last two lines mysql_query($sql,$con); mysql_close($con); I removed them and it really did give the output "Table created" yet it didn't create the table in the database. I'm having a feeling that I need to add an "if" statement, something of the sort. if (sql(CREATE TABLE Persons)) But I'm also getting the feeling something there is wrong, I still haven't tried it. Help? :-\
  7. Worked like a charm when I converted it to ANSI! Thank you all for the quick replies and patience!
  8. Nope, no spaces. I tried to put it in <head> (I'm not really sure what you mean by setting headers ) but it still gives the same error.
  9. Can someone explain to me whats wrong with this? <?php session_start(); // store session data $_SESSION['views']=1; ?> <html> <body> <?php //retrieve session data echo "Pageviews=". $_SESSION['views']; ?> </body> </html> The output it gives me in the browser is this: Explanation?
  10. Just a quick question, what does fclose do and why does it have to be there? Does it keep the file from going on forever and display the same thing over and over or... I got it wrong?
  11. Here you go. Yours works perfectly, I even understand it
  12. Thank you so much! But if it's written like that in the W3 tutorial, shouldn't it work?
  13. Hi all. What am I doing wrong here? I cannot read from the file with fopen, this is the code. <html> <body> <?php $file=fopen("welcome.txt","r"); ?> </body> </html> I get a blank page, although I have text written in that file. Whenever I set it to "w" it still shows nothing - when I check the file manually with notepad, I see that it's blanked - but I think thats meant to happen, after all "w" clears the file. Have I understood correctly? This still shows a blank page, but it says it can't find the file if it's not in the directory - how it should be. <html> <body> <?php $file=fopen("welcome2123.txt","r") or exit("Unable to open file!"); ?> </body> </html> tl;dr - Why doesn't fopen read anything from the txt file? Encodings match - everything is in UTF-8, although I doubt thats the problem... Help?
  14. Thank you, I'll try in a seperate file. I just saved my data from htdocs and I'm installing xampp - I don't know why I didn't in the first place.
  15. Thank you for all the replies, then I'm afraid something is wrong with how i set up my webserver. I'm using apache and PHP 5, so far I've had no problems with any of the functions that I've had from that tutorial (tried them, no problem). Also, I execute all of them under firefox as php.php (it opens them without any problem) This is what I've done so far. <?php $cars[0]="Saab"; $cars[1]="Volvo"; $cars[2]="BMW"; $cars[3]="Toyota"; echo $cars[0] . " and " . $cars[1] . " are Swedish cars." . $cars[2] . " and " . $cars[3] . " are not." ?> <br /> <?php $ages['Peter'] = "32"; $ages['Quagmire'] = "30"; $ages['Joe'] = "34"; echo "Peter is " . $ages['Peter'] . " years old."; ?> <br /> <?php $families = array ( "Griffins"=>array ( "Peter", "Lois", "Megan" ), "Quagmires"=>array ( "Glenn" ), "Browns"=>array ( "Cleveland", "Loretta", "Junior" ) ); echo "Is " . $families['Griffins'][2] . " a part of the Griffin family?" . "Is " . $families['Quagmires'][0] . " a pervert?"; ?> <br /> <?php $i=1; while($i<=5) { echo "Chisloto e " . $i . "<br />"; $i++; } ?> <br /> <?php $i=1; do { $i++; echo "The number is " . $i . "<br />"; } while ($i<=5); ?> <br /> <?php $i=1; do { $i++; echo "The number is " . $i . "<br />"; } while ($i<=5); ?> <br /> <?php for ($i=1; $i<6; $i++) { echo "The number is " . $i . "<br />"; } ?> <br /> <?php $g=array("edno","dve","tri"); foreach ($g as $value) { echo $value . "<br />"; } ?> <br /> <?php function writeName() { echo "Smurt Ivanova"; } echo "My name is "; writeName(); ?> <br /> On and On and On and before that - all simple tutorial ones. I use that code just below all others and it gives me a blank page. When I remove it, all the above display normal results. Help?
  16. Hello all, I finally started learning php and with the little knowledge I have from CPP it's far simpler than it seemed years ago. I need a little bit of help with this function I followed in a tutorial and it's just not working. <?php function writeName($fname) { echo $fname . " Refsnes.<br />"; } echo "My name is "; writeName("Kai Jim"); echo "My sister's name is "; writeName("Hege"); echo "My brother's name is "; writeName("Stale"); ?> It just gives me a blank screen. Now, I figured #fname isnt set, so I did this: <?php $fname = "Refsnes"; function writeName($fname) { echo $fname . " Refsnes.<br />"; } echo "My name is "; writeName("Kai Jim"); echo "My sister's name is "; writeName("Hege"); echo "My brother's name is "; writeName("Stale"); ?> Still, no go, which leads me to think I'm probably mistaking. Help? I don't want to skip out things in a tutorial.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.