|
Ginge Reymont
Registered User
Join date: 10 Oct 2005
Posts: 190
|
11-14-2006 07:18
Hiya everyone, I decided to start learning PHP but on my first script build I am having problems with the database displaying my data: <?php
$host="meh"; $user="meh"; $password="meh"; $getting = $_POST['name']; echo ($getting); mysql_connect($host,$user,$password) or die ("Couldnt connect to the db"); mysql_select_db("phptester") or die ("Couldnt select db"); $query = ("SELECT Age FROM users WHERE Name = $getting"); $result = mysql_query($query) or die (mysql_error()); echo $result; ?>
Any ideas?
|
|
Jillian Callahan
Rotary-winged Neko Girl
Join date: 24 Jun 2004
Posts: 3,766
|
11-14-2006 07:39
mysql_query returns a collection/resource. try this in place of "echo $result;": echo("<table>"); while ($newArray = mysql_fetch_array($result, MYSQL_ASSOC)) // fetch one row from the $result as an array, column names associated { echo("<tr>"); foreach($newArray as $value) // for each member of the array { echo("<td>".$value."</td>"); } echo("</tr>\n"); } echo("</table>");
|
|
Raeyan Aldrich
Registered User
Join date: 14 Oct 2006
Posts: 44
|
11-20-2006 22:17
also depending on the version of MySQL your server is using, you may need to reformat the query: $query = ("SELECT `Age` FROM `users` WHERE `Name` = '". $getting. "'");
|
|
Nynthan Folsom
Registered User
Join date: 29 Aug 2006
Posts: 70
|
12-02-2006 00:08
You certainly will need the single quotes unless $getting is a numerical value.
But in my experience with current iterations of mysql,
$query = "select age from users where name='$getting'";
will work just fine. Typing a whole bunch of unnecessary quotes gets old very quick.
|
|
Eloise Pasteur
Curious Individual
Join date: 14 Jul 2004
Posts: 1,952
|
12-02-2006 02:39
You might also find something like:
$sql = "SELECT * FROM mytable WHERE Name = \"$name\" "; works just fine. Does for me in recent versions of php and mysql anyway.
|