Hi Everyone, I'm trying to convert my MySql data into variables so I can use them on my website. For example, if I want to convert a city or even first or last name into a variable. How would I go about doing that? Thanks everyone!
Its so simple... Fist need a connection to page for database. $con = mysql_connect('<host_name>','<user_name>','<pass_word>') or die("Could not found the server!"); mysql_select_db('<db_name>', $con) or die("Could not connect to the database!"); PHP: Case 1: Select single row. Write your query(What data you want to retrieve from MySQL database). // Getting Single row result. $query = "SELECT `<column_one>`, `<column_two>`, `<column_three>` FROM <table_name> WHERE <any_column_name>=<saved_row_value>;"; PHP: Gather the result and make variables. $result = mysql_query($query, $con); $row = mysql_fetch_array($result); $variable1 = $row['<column_one>'] $variable2 = $row['<column_two>'] $variable3 = $row['<column_three>'] echo "Full Name: $variable1 $variable2, Email is: $variable3"; PHP: Result(on page): Full Name: Mukunda Panchal, Email is mukunda@tacnix.com Case 2: Select multiple row. Write your query(What data you want to retrieve from MySQL database). // Getting Single row result. $query = "SELECT `<column_one>`, `<column_two>`, `<column_three>` FROM <table_name> ORDER BY <any_column_name> ASC;"; /* ** * Into ORDER BY <any_column_name> must be an "ID" or "Date" or if your table have an alphabetical column "ALPHABETS" for getting the perfect result. * For example: * "SELECT `first_name`, `last_name`, `email` FROM `registry` ORDER BY `id`;" or * "SELECT `first_name`, `last_name`, `email` FROM `registry` ORDER BY `register_date`;" * "SELECT `first_name`, `last_name`, `email` FROM `registry` ORDER BY `alphabets`;" * ** PHP: Gather the result and make variables. $result = mysql_query($query, $con); while($row = mysql_fetch_array($result)) { $variable1 = $row['<column_one>'] $variable2 = $row['<column_two>'] $variable3 = $row['<column_three>'] echo "Full Name: $variable1 $variable2, Email is: $variable3\n"; } PHP: Result(on page): Full Name: Mukunda Panchal, Email is mukunda@tacnix.com Full Name: Mac Panchal, Email is mukunda1@tacnix.com Full Name: Mukunda Pan, Email is mukunda2@tacnix.com Full Name: Mac Pan, Email is mukunda3@tacnix.com
Convert in to a variable? You need to learn how to use mySQL from with in PHP. In fact, you actually need to learn exactly what mySQL is. mySQL is a database that can store data in columns with in tables. You interact with mySQL by executing queries known as SQL. An application or script will use a library and driver to connect to a database to execute your SQL statements to create, retrieve, update or delete entries with in your database. You fetch and retrieve data via the SELECT sql statement. mySQL offers PDO which allowes you to easily fetch columns and BIND the column values (your data) to scalar variables ($blah) so you can display the results. Please refer to Google.com to search for tutorials. Or visit php.net
That does not help at all, scm22ri I would recommend you looking at the post tacnix made, as I could not improve on it I would just be rewriting what he wrote.
I'm replying this post for i forgot to use semicolon at the end of fetched variables. Here: Forgot a semicolon ";". $variable1 = $row['<column_one>'] $variable2 = $row['<column_two>'] $variable3 = $row['<column_three>'] PHP: Applied a semicolon ";", where i forgot. $variable1 = $row['<column_one>']; $variable2 = $row['<column_two>']; $variable3 = $row['<column_three>']; PHP: And "<table_name>, <column_one>" is replaced by your "real table name" & "real column name". Also i strongly agree with NetStar for "You need to learn how to use mySQL from with in PHP. In fact, you actually need to learn exactly what mySQL is.". Sorry for forgetting a semicolon.
Thanks for everyone's help. I appreciate it! Thanks also tacnix! Kinda like this? $result = mysql_query("SELECT * FROM info") or die(mysql_error()); while ($row = mysql_fetch_array($result)) { $firstname = $row['first_name']; $lastname = $row['last_name']; $city = $row['city']; $address = $row['address']; Code (markup): Let me ask everyone this question. I'm going to have a product oriented website but each of those products is going to be centered around a location. The location being (state,city). My URL might look something like this. mysite.com/new-york/albany/for-sale/2002-ford-explorer How would I make such a URL? using the above information? Thanks everyone!
It use htaccess mod rewrite.. read it: http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html http://corz.org/serv/tricks/htaccess2.php http://zenverse.net/seo-friendly-urls-with-htaccess/
Have you use a query strings in your url's. Like: mysite.com/for-sale/2002-ford-explorer.php?state=new%20york&city=albany Code (markup): or mysite.com/for-sale.php?state=new%20york&city=albany&model=2002-ford-explorer Code (markup): or mysite.com/for-sale/car.php?state=new%20york&city=albany&year=2002&company=ford&model=explorer Code (markup): or mysite.com/search/place.php (if user like to search by place)mysite.com/search/cars.php (if user like to search by cars) Code (markup): If search is done by "place.php", then url might like this: mysite.com/cars/for-sale/new_york/albany/details.php?year=2002&brand=ford&model=explorer Code (markup): If search is done by "cars.php", then url might like this: mysite.com/cars/for-sale/ford_explorer/details.php?year=2002&state=new_york&city=albany Code (markup): For knowledge base: If yes then place your links into a page where your product will show as thumbnails.Example: Flow of product display on the page. Name or the Product. Image of the product. Short Info of the product. Link(detail.php) of the product. [TABLE="width: 600"] [TR] [TD]Tabled type flow example:[/TD] [TD][/TD] [TD][/TD] [TD][/TD] [/TR] [TR] [TD]Product Name Short information about the product. Link...[/TD] [TD]Product Name Short information about the product. Link...[/TD] [TD]Product Name Short information about the product. Link...[/TD] [TD]Product Name Short information about the product. Link...[/TD] [/TR] [TR] [TD]Product Name Short information about the product. Link...[/TD] [TD]Product Name Short information about the product. Link...[/TD] [TD]Product Name Short information about the product. Link...[/TD] [TD]Product Name Short information about the product. Link...[/TD] [/TR] [TR] [TD]Product Name Short information about the product. Link...[/TD] [TD]Product Name Short information about the product. Link...[/TD] [TD]Product Name Short information about the product. Link...[/TD] [TD]Product Name Short information about the product. Link...[/TD] [/TR] [/TABLE] Link should look like this in php code: // If files in same folder. echo "< a href='details?product=$product_name'>$product_name< a/ >\n"; // If files in different folder. echo "< a href='cars/details?product=$product_name'>$product_name< a/ >\n"; PHP: The link will look like into address bar "mysite.com/cars/for-sale/ford_explorer/details.php?year=2002&state=new_york&city=albany". So every link of your product generate automatically in "while" loop. Here example: // i've rename some column names for understanding properly. $result = mysql_query("SELECT * FROM info", $con) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { $brandname = $row['brand_name']; $modelname = $row['model_name']; $carname = "$brandname - $modelname"; $city = $row['city']; $address = $row['address']; $year = $row['year']; echo "Product name."; // Load Product name from database. echo "Product Image."; // Load images from database or load from the path. echo "Product Short info"; // whatever the info you have. echo "<a href='details?year=$year&city=$city&address=$address&car=$carname'>$carname< a/>\n"; // Load product values(year, state & city) from database } mysql_close($con); PHP: For live example visit on aauraglobal.com After this you can try "bluebenz" post for rewriting your query strings. if you wish/want.
Wow tacnix, thanks for the detailed message. I appreciate that. I'm going to be reviewing this code. If I have any questions I'll post them in the forum. Thanks again! I appreciate it!
Hi Everyone, Sorry to keep on beating this subject into the group but this is where I am at the stage in the game. These are the steps I've already done. 1. I've connected to my database 2. I've declared the variables 3. I tested the variables in a sentece to see if they were working. It seems they are. For me this is where the confusion starts to come into play .... Here's my question. What do I need to do next? Do I need to write a script or something along those links to create my URLS? what would that script look like? Does it have to be a different page? Thanks for everyones help! Below is the code for my website so far.
Try to read this resource. PHP Resources: 1) http://www.php.net/manual/en/getting-started.php 2) http://www.w3schools.com/php/default.asp 3) http://www.tizag.com/phpT/index.php MySQL Resources: 1) http://dev.mysql.com/resources/ (MySQL resources) 2) http://dev.mysql.com/usingmysql/get_started.html (Get Started with MySQL) 3) http://dev.mysql.com/usingmysql/ (Development with MySQL) 4) http://dev.mysql.com/tech-resources/ (tech-resources) htaccess Resources: 1) http://httpd.apache.org/docs/2.4/ 2) http://www.htaccess-guide.com/ 3) http://www.htaccess-guide.com/useful-resources/ IMP Apache Resource *) http://wiki.apache.org/httpd/ *) http://apache.org/ I've followed "Beginning with php 5.3" eBook and make self study from this eBook. I suggest you to download this eBook and make self study from this eBook... http://numii.com/hanuman/Beginning%20PHP%205.3%20by%20Matt%20Doyle.pdf
How to convert MySql data into variables? Here's what it came to my mind when I first read the title of this thread: Convert the column_name and value, returned by the query result to different variables. Consider the case of storing some settings. Prefixing a "$" before a variable name, declares a variable with the content. For example: $settings['dbusername'] = 'abc'; $settings['dbpassword'] = '12345'; foreach($settings as $key => $value) $$key = $value; echo $dbusername . '<br />' . $dbpassword; PHP: So if you have a variable, say, $a = 'b'; PHP: Then if you prefix it with a "$", like this: $$a = 'c'; PHP: Then the compiler will define a variable called $b with the value "c" in it. If you check the output of this: echo $b; PHP: Then you will see that it echoing "c".