Jump to content

famous58

Members
  • Posts

    21
  • Joined

  • Last visited

    Never

Everything posted by famous58

  1. [!--quoteo(post=376120:date=May 22 2006, 12:59 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 22 2006, 12:59 PM) [snapback]376120[/snapback][/div][div class=\'quotemain\'][!--quotec--] Why? because when you pass a variable to a function, the function makes a copy of the variable, for local use inside the function. That's the difference between global and local scope. If you wanted that 2nd echo to print the same thing (that is, change the $cartUser on a global level), you would have to pass it by reference, by adding an ampersand before the variable in the function argument like this: [b]ShowCart(&$cartUser) { .... }[/b] [/quote] Thanks for the help, again [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /] One question though. What if I have say four functions that I want to use $cartUser in. Initially I thought that declaring $cartUser global outside a function would delcare it globaly for the script and any function calling $cartUser would be able to use it. Is there a way to do that? Or do I have to declare $cartUser global in each seperate function? The way I ended up doing it was like so: [code] <? $cartUser = $session->username; function1 ($cartUser){    mysql query needing $cartUser; } function2 ($cartUser){   mysql query needing $cartUser; } function3 ($cartUser){   mysql query needing $cartUser; } function4 ($cartUser){   mysql query needing $cartUser; } [/code] It works and I'm not modifying the variable in any of the functions. However, I want to be sure that doing this isn't going to cause problems down the road. Seems this way is beter than declaring the variable then declaring it global in each function. Yes?
  2. Nevermind. Got this to work: [code] <? $cartUser = 'test'; ShowCart($cartUser); function ShowCart($cartUser){      echo $cartUser; } ?> [/code]
  3. I am stumped. Why doesn't this code work? [code] <? $cartUser = 'test'; ShowCart(); function ShowCart(){      echo $cartUser; } ?> [/code] It seems like everytime I put variables outside of a function they do not pass through. Someone told me to use global but that doesn't work either (or perhaps I'm using it wrong).
  4. [!--quoteo(post=375316:date=May 19 2006, 11:26 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ May 19 2006, 11:26 AM) [snapback]375316[/snapback][/div][div class=\'quotemain\'][!--quotec--] If you're still having a problem with that query, try putting the FROM keyword in there $query = "SELECT * [!--coloro:#FF6666--][span style=\"color:#FF6666\"][!--/coloro--]FROM [!--colorc--][/span][!--/colorc--]users WHERE username=admin"; [/quote] That was just a typo on my part, it is in there. I do have something working now.
  5. [!--quoteo(post=375273:date=May 19 2006, 08:44 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ May 19 2006, 08:44 AM) [snapback]375273[/snapback][/div][div class=\'quotemain\'][!--quotec--] Use the mysql_fetch_assoc() function, not the mysql_fetch_row() function. Ken [/quote] doh! I'll crawl back into my hole now....
  6. So I've been using PHP to query MySQL dbs for a while now and have never had this happen to me. All of a sudden I cannot get the data to display using my normal code. This no longer works: [code] <? mysql_connect("localhost", "db_user", "pass") or die("Could not connect"); mysql_select_db("db_name") or die("Could not select database"); $result = mysql_query("SELECT * FROM `users` WHERE `username`='admin' ") or die(mysql_error()); $row = mysql_fetch_row($result); echo $row[username]; ?> [/code] But this does: [code] <? mysql_connect("localhost", "db_user", "pass") or die("Could not connect"); mysql_select_db("db_name") or die("Could not select database"); $result = mysql_query("SELECT * FROM `users` WHERE `username`='admin' ") or die(mysql_error()); $row = mysql_fetch_row($result); echo $row[1]; ?> [/code] I've triple checked that "username" is "username" in the db. I'm at a total loss. What would cause this all of a sudden?
  7. w00t. It was worth getting out of bed today!
  8. [!--quoteo(post=375107:date=May 18 2006, 06:05 PM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ May 18 2006, 06:05 PM) [snapback]375107[/snapback][/div][div class=\'quotemain\'][!--quotec--] That connection shopuld work fine, obviously your query isn't returning a result for whatever reason. You should always check your queries actually work before trying to use them. [/quote] I'll try that out. It seems though, everytime I try a MySQL connection inside a function, it doesn't work. If I add the connection information (along with the variable declarations) then it works. If I move them outside the function, it does not. Why is that. I see all these scripts that are written the way yours is, but it won't work for me. Is that a server setting? PHP.ini setting? Same goes for declaring the variables. If I declare the variables in say a db.php and include that in the top of the page, if I put the connection inside a function it fails, like so: this does not work: [code] <? include ('db.php'); function Connect(){   mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());   mysql_select_db($dbname, $this->connection) or die(mysql_error());      } function GetInfo(){   $result = mysql_query( "SELECT username FROM users WHERE id='1'" );   $row = mysql_fetch_array($result);   }   GetInfo(); ?> [/code] this does: [code] <? function GetInfo(){   $dbhost = "localhost";   $dbuser = "database_user";   $dbpass = "password";   $dbname = "database_printing";   mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());   mysql_select_db($dbname) or die(mysql_error());   $result = mysql_query( "SELECT username FROM users WHERE id='1'" );   $row = mysql_fetch_array($result);   }   GetInfo(); ?> [/code] My whole purpose of all of this is to create a function that will connect and return data without having to recreate the connection in every function in the file or on other pages where the functions are called. T.I.A.
  9. [!--quoteo(post=375035:date=May 18 2006, 11:06 AM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ May 18 2006, 11:06 AM) [snapback]375035[/snapback][/div][div class=\'quotemain\'][!--quotec--] Well, to be honest, I'm not sure it's going to work how you're setting up the connection. I've never successfully pass a connection around like that. That might be where your problem is. [/quote] Thanks for the honesty. I was kinda taking that code from another script someone else wrote anyway (http://evolt.org/node/60384) and have not been able to figure it out either. I guess I'll go back to my orginal way of connecting, but at least I understand classes a little better now. Thanks again.
  10. [!--quoteo(post=375032:date=May 18 2006, 10:39 AM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ May 18 2006, 10:39 AM) [snapback]375032[/snapback][/div][div class=\'quotemain\'][!--quotec--] That means you have an SQL error, and I'm going to guess it's the fact that you don't have single quotes around $user in your query. [/quote] Tried that, changed it to "SELECT * users WHERE `username`='$user'" with no avail.
  11. [!--quoteo(post=375016:date=May 18 2006, 09:59 AM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ May 18 2006, 09:59 AM) [snapback]375016[/snapback][/div][div class=\'quotemain\'][!--quotec--] Your function would have an input in the class: [code]function GetUserInfo($user) { [/code] And you would call it like this: [code]$cn->GetUserInfo($user);[/code] Alternately, you could also pass this as a value to the constructor and make it a global variable in the class if you plan to use it in other areas of the class. [/quote] OK. Here is what I changed it to: [code] <?php class MySQLConnect{     var $connection;          function MySQLConnect(){         $dbhost = "localhost";         $dbuser = "database_user";         $dbpass = "password";         $dbname = "database_printing";                  $this->connection = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());         mysql_select_db($dbname, $this->connection) or die(mysql_error());                 }          function GetUserInfo($user) {     $query = "SELECT * users WHERE username=$user";     $result = mysql_query($query, $this->connection);     $row = mysql_fetch_array($result);          echo "User: " . $row[username] . "<br>";     echo "Email: " . $row[email] . "<br>";     echo "User Level: " . $row[userlevel] . "<br>";         }          } ?> [/code] and [code] <? $user= 'admin'; include('mysqldb.php'); $output = new MySQLConnect(); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <? $output->GetUserInfo($user); ?> </body> </html>[/code] But I am receiving "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/prellisp/public_html/new/mysqldb.php on line 19" Line 19 is the "$row = mysql_fetch_array($result);". I've triple checked my db variables and everything is correct.
  12. [!--quoteo(post=375002:date=May 18 2006, 09:38 AM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ May 18 2006, 09:38 AM) [snapback]375002[/snapback][/div][div class=\'quotemain\'][!--quotec--] I think you're confused or I'm not catching on to what you're trying to do. I assume "getuserinfo()" is always going to have the same output.... so you would put the echos and whatnot in the class as well. Keep in mind that calls to functions in a class can work somewhat like a more powerful include(). When you call the $cn->GetUserInfo() and you have your while loop and echos in the class, it will display all that information for you. You don't have to return it or anything like that. [/quote] Ahhhh. I was trying to build a connection class that I could use accross the board then grab the info. I see now, I'd have to put the echo's in the function. One more question, and if it's too complicated nevermind, but what if I want the info to be based on a variable, i.e.: [code] function GetUserInfo() {     $query = "SELECT * users WHERE username=$user";     $result = mysql_query($query, $this->connection);     $row = mysql_fetch_array($result);         } [/code] It seems that if $user is being defined on mysqldb_echo.php, then it won't work. Would something like $cn->GetUserInfo($user='admin'); tell the function what $user is?
  13. [!--quoteo(post=374988:date=May 18 2006, 09:19 AM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ May 18 2006, 09:19 AM) [snapback]374988[/snapback][/div][div class=\'quotemain\'][!--quotec--] And that's exactly what you should be getting. All you've done is create the object and run the constructor. You need to do something like this: [code]$cn = new MySQLConnect(); $cn->GetUserInfo();[/code] But that's not going to output anything either, because you don't actually spit out your results, unless you're not showing us everything. [/quote] Thanks for the superfast reply. I guess that's what I'm asking. How do I output the results? Up until now I've been doing things like: [code] $link = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error()); $q = "SELECT * FROM products WHERE `id`=$id"; $result = mysql_db_query(DB_NAME, $q, $link) or die(mysql_error()); $row = mysql_fetch_array($result); echo "User: " . $row[username] . "<br>"; echo "Email: " . $row[email] . "<br>"; [/code] or [code] while($row = mysql_fetch_array($result)) { echo "User: " . $row[username] . "<br>"; echo "Email: " . $row[email] . "<br>"; } [/code] But now I'd like to take it to the next level. I'm getting into things like shopping carts and sessions and it seems like my little thrown together sql statements won't cut it any more.
  14. OK. So I am trying to teach myself PHP Classes. I'm trying to create a MySQL connection and query the DB and echo a result. It's not working. Here's what I have: mysqldb.php [code] <?php class MySQLConnect{     var $connection;          function MySQLConnect(){         $dbhost = "localhost";         $dbuser = "dbuser";         $dbpass = "dbpass";         $dbname = "prellisp_printing";                  $this->connection = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());         mysql_select_db($dbname, $this->connection) or die(mysql_error());                 }          function GetUserInfo() {     $query = "SELECT * users WHERE username=admin";     $result = mysql_query($query, $this->connection);     $row = mysql_fetch_array($result);         }          } ?> [/code] mysqldb_echo.php [code] <? include('mysqldb.php'); $output = new MySQLConnect(); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <? echo $output; ?> </body> </html> [/code] What I get is "Object". I've looked at the OPP tutorial on this site and at webmonkey, but neither show what I'm looking for. I want to be able to display any section of data, i.e. username, email address, etc. Any help is appreciated. [b]Mod Edit(shoz): connection details removed[/b]
  15. [!--quoteo(post=352691:date=Mar 7 2006, 04:22 PM:name=php_b34st)--][div class=\'quotetop\']QUOTE(php_b34st @ Mar 7 2006, 04:22 PM) [snapback]352691[/snapback][/div][div class=\'quotemain\'][!--quotec--] Try checking that you are connected, then check if your queryy worked: [code]<?php $Host = "localhost"; $User = "user"; $Password = "password"; $DBName = "database"; $TableName = "users"; $Link = mysql_connect ($Host,$User,$Password);           or die ("Couldn't connect to server."); $Query = "SELECT * from $TableName"; $Result = mysql_db_query ($DBName, $Query, $Link); if ($result) {     echo 'db connection'; } else {     echo 'problem connecting to db'; } while ($Row = mysql_fetch_array ($Result)) {     print ("hello");     }      ?> [/code] [/quote] Well, I have a problem connecting, but I've triple checked all the variables. How can I see where the connection problem lies? Edit: Nevermind, I'm retarded. Had the incorrect username
  16. OK, I'm really close to hurting something. Can someone take a look at this script and tell me if I have done anything wrong? It will not work, it returns a blank page. If nothing is wrong, is there some kind of php setting that could be causing this? For some reason the php set up on this server seems wacky. [code]<?php $Host = "localhost"; $User = "user"; $Password = "password"; $DBName = "database"; $TableName = "users"; $Link = mysql_connect ($Host,$User,$Password); $Query = "SELECT * from $TableName"; $Result = mysql_db_query ($DBName, $Query, $Link); while ($Row = mysql_fetch_array ($Result)) {     print ("hello");     }      ?>[/code] T.I.A.
  17. [!--quoteo(post=351408:date=Mar 3 2006, 11:48 AM:name=neylitalo)--][div class=\'quotetop\']QUOTE(neylitalo @ Mar 3 2006, 11:48 AM) [snapback]351408[/snapback][/div][div class=\'quotemain\'][!--quotec--] ah... you won't need classes to do this. All you need to do this is access the $_GET array. Check this out. If you're accessing this URL [code]http://www.domain.com/index.php?a=1&b=2[/code] then put this code into index.php to get the data from that URL: [code]<?php $a = $_GET['a']; $b = $_GET['b']; echo $a; //will echo 1 echo $b; //will echo 2 ?>[/code] [/quote] Sa-Weet. It was worth getting out of bed today, I did gone and learnt something! Thanks! [!--quoteo(post=351408:date=Mar 3 2006, 11:48 AM:name=neylitalo)--][div class=\'quotetop\']QUOTE(neylitalo @ Mar 3 2006, 11:48 AM) [snapback]351408[/snapback][/div][div class=\'quotemain\'][!--quotec--] ah... you won't need classes to do this. All you need to do this is access the $_GET array. Check this out. If you're accessing this URL [code]http://www.domain.com/index.php?a=1&b=2[/code] then put this code into index.php to get the data from that URL: [code]<?php $a = $_GET['a']; $b = $_GET['b']; echo $a; //will echo 1 echo $b; //will echo 2 ?>[/code] [/quote] Wait, maybe I spoke too soon. What if I don't have access to index.php cause it's on someone else's server (API)? So, I need to create a page that accesses a URL, get's the info from that URL and puts it into my page?
  18. [!--quoteo(post=351228:date=Mar 2 2006, 07:25 PM:name=neylitalo)--][div class=\'quotetop\']QUOTE(neylitalo @ Mar 2 2006, 07:25 PM) [snapback]351228[/snapback][/div][div class=\'quotemain\'][!--quotec--] Hope I didn't confuse you. :) [/quote] Not completely. How would I then use this method to pull info from an api return url? If I know the variables that are returned could I build a function that takes the info from a URL and then pull elements from what is returned. Does that make sense?
  19. So I asked a rather lengthy question [a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=87298\" target=\"_blank\"]here[/a] and am thinking it might be a bit much. So I thought I would start a little simpler (is that a word?). What the heck is this little deal ->. I see it a lot in giant scripts that I didn't and couldn't write, and I'm thinking this might be something I want to learn about. I tried doing a search for that little dealy bob but of course nothing comes up. And seeing how I don't know what it's called it is kinda difficult to get info on what it is/does. T.I.A. for any insight on this.
  20. Hello all. I've run into a problem that I know I will encounter again and I'm not sure how this is to be done. I am a novice coder and am able to validate a form and write simple if else statements using variables, etc. However, I am now building something using an API interface. A GET method is required when sending the data to the 3rd party, and they in turn send back either txt, html or a xml response. My question is how do I read that response on the back end and then continue on in my site. For example, to check a domain names availability, one would enter the desired domain name and hit submit. I can build that form no problem. But where does the next page go? Since it is a get method I obviously need to hide my values so I assume I would use a POST to go to a "check" page, capture those variables the 3rd party requires and then send them from the "check" page, which I do not know how to do. Then a response will come back (to where I'm not sure). I then need to check that reponse (I'm assuming on the same "check" page somehow) and then issue some kind of resopnse to my user saying either "true" or "false". Hopefully this makes sense and there is a simple solution to my problem that am not aware of. T.I.A. for any help.
×
×
  • 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.