sebastiaandraaisma Posted December 16, 2006 Share Posted December 16, 2006 Hello I have question that might sound stupid, but how do I echo just one field from the database?I tried the following:// make the connection to the database@require "connect.php";// Retreive data from database for translation$result = mysql_query('SELECT * FROM admins WHERE id = 29');// Select id 29 from table admins$res = mysqli_query($dbh, $result);// I don't understand this$newArray = mysqli_fetch_array($res, MYSQLI_ASSOC);// Something with an aray?$name = $newArray['name'];//Extract the name from the aray and give it the variable $name?echo "$name";// Show the nameWhat it does... actually what it doesn't do is echo (print) the name on the screen.The table looks like this:CREATE TABLE `admins` ( `id` int(11) NOT NULL auto_increment, `name` varchar(50) default NULL, `user` varchar(50) default NULL, `passwd` varchar(50) default NULL, `type` int(11) default NULL, `language` int(11) default '0', PRIMARY KEY (`id`)) TYPE=MyISAM AUTO_INCREMENT=34 ;INSERT INTO `admins` VALUES (29, 'John Doe', 'hello', 'test', 1, 0);I just don't know how to get the name as a variable and than echo"$name"I'm very new to php :)Any help is welcome!Kind regards,Sebas. Link to comment https://forums.phpfreaks.com/topic/30862-solved-how-to-retreive-and-echo-just-one-field-from-db/ Share on other sites More sharing options...
JP128 Posted December 16, 2006 Share Posted December 16, 2006 [code]<?php//DB Connect above this...$result = mysql_query("SELECT * FROM admins WHERE id = '29'");while(mysql_fetch_assoc($result)){echo $result['name'];}[/code] Link to comment https://forums.phpfreaks.com/topic/30862-solved-how-to-retreive-and-echo-just-one-field-from-db/#findComment-142303 Share on other sites More sharing options...
sebastiaandraaisma Posted December 16, 2006 Author Share Posted December 16, 2006 Wow, susch a quick reply! :)Thank you so much, I will try it as soon as I come home :)Thank you. Link to comment https://forums.phpfreaks.com/topic/30862-solved-how-to-retreive-and-echo-just-one-field-from-db/#findComment-142308 Share on other sites More sharing options...
JP128 Posted December 16, 2006 Share Posted December 16, 2006 NP, I hope it helps. Link to comment https://forums.phpfreaks.com/topic/30862-solved-how-to-retreive-and-echo-just-one-field-from-db/#findComment-142314 Share on other sites More sharing options...
sebastiaandraaisma Posted December 16, 2006 Author Share Posted December 16, 2006 Hello again :)I got an error saying:Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\www.globalhome.eu\indextest.php on line 13On line 13 is the following text:while(mysql_fetch_assoc($result)){I tried a couple of things but without result.I don't know if it has something to do with the fact that I'm running on XP and off-line?Kind regards,Sebastiaan Link to comment https://forums.phpfreaks.com/topic/30862-solved-how-to-retreive-and-echo-just-one-field-from-db/#findComment-142355 Share on other sites More sharing options...
QuietWhistler Posted December 16, 2006 Share Posted December 16, 2006 Yes, you can only fetch data from a database when you are connected to the internet, or if you have mysql installed on localhost. Furthermore, are you sure that you have put:[code]$result = $result = mysql_query("SELECT * FROM admins WHERE id = '29'");[/code]instead of just:[code]mysql_query("SELECT * FROM admins WHERE id = '29'");[/code] Link to comment https://forums.phpfreaks.com/topic/30862-solved-how-to-retreive-and-echo-just-one-field-from-db/#findComment-142356 Share on other sites More sharing options...
sebastiaandraaisma Posted December 16, 2006 Author Share Posted December 16, 2006 Hello, thank you for your reply!I have on line 12:$result = mysql_query("SELECT * FROM admins WHERE id = '29'");I did try your version, which gave the same error$result = $result = mysql_query("SELECT * FROM admins WHERE id = '29'");To come back on your question wether I have installed MySQL, Yes MySQL is installedI have also Apache 2.x installed and running. I am able to output other php scripts and in the MySQL command line I can see the database, the table and the value I try to extract. I'm just very new to this and do not realy know what I'm doing wrong.In one of my books I read that an error that includes a Warning has something to do with the file path or something? As far as I know I configured things corectly. Is there anything specific I should look for that you might be aware of?Kind regards,Sebastiaan Link to comment https://forums.phpfreaks.com/topic/30862-solved-how-to-retreive-and-echo-just-one-field-from-db/#findComment-142357 Share on other sites More sharing options...
taith Posted December 16, 2006 Share Posted December 16, 2006 you dont need[code]$result = $result = mysql_query("SELECT * FROM admins WHERE id = '29'");[/code]what that error is telling us is that this query is giving us errors, but not showing them... use this on every query[code]$result = mysql_query("SELECT * FROM admins WHERE `id`='29'") or die(mysql_error());[/code]that will give you an error, and then we can work from there Link to comment https://forums.phpfreaks.com/topic/30862-solved-how-to-retreive-and-echo-just-one-field-from-db/#findComment-142359 Share on other sites More sharing options...
QuietWhistler Posted December 16, 2006 Share Posted December 16, 2006 [quote author=QuietWhistler link=topic=118857.msg486063#msg486063 date=1166271566]Yes, you can only fetch data from a database when you are connected to the internet, or if you have mysql installed on localhost. Furthermore, are you sure that you have put:[code]$result = $result = mysql_query("SELECT * FROM admins WHERE id = '29'");[/code]instead of just:[code]mysql_query("SELECT * FROM admins WHERE id = '29'");[/code][/quote]Woops my bad, must have copied it wrong. Link to comment https://forums.phpfreaks.com/topic/30862-solved-how-to-retreive-and-echo-just-one-field-from-db/#findComment-142361 Share on other sites More sharing options...
sebastiaandraaisma Posted December 16, 2006 Author Share Posted December 16, 2006 Wow, I'm realy dealing with experts here!!!Thanks a lot guys for taking the time to help me out!The error message I get now is:No database selectedI always though that this would be done automaticaly?My connection script looks like this:<?php$dbcnx = @mysql_connect('localhost', 'root', 'abc123');if (!$dbcnx) {echo( '<p>Unable to connect to the ' .'database server at this time.</p>' );exit();}?>The name of the database is:alfdis_globalI realy apriciate the help I'm getting hereI wish I had the knowledge to help someone else on the forum (maybe in 6 months from now if I keep studying hard ;) ) Link to comment https://forums.phpfreaks.com/topic/30862-solved-how-to-retreive-and-echo-just-one-field-from-db/#findComment-142443 Share on other sites More sharing options...
taith Posted December 16, 2006 Share Posted December 16, 2006 you've connected the database, but you havent seleceted it...[code]mysql_connect("$host","$username","$password");mysql_select_db("$database");[/code] Link to comment https://forums.phpfreaks.com/topic/30862-solved-how-to-retreive-and-echo-just-one-field-from-db/#findComment-142444 Share on other sites More sharing options...
sebastiaandraaisma Posted December 16, 2006 Author Share Posted December 16, 2006 Now all errors are gone,But it does not echo out the name.It does do the rest of the php scripts on the page correct.I now have:<?php$database = "alfdis_global";$dbcnx = @mysql_connect('localhost', 'root', 'abc123');mysql_select_db("$database");if (!$dbcnx) {echo( '<p>Unable to connect to the ' .'database server at this time.</p>' );exit();}?>:) Link to comment https://forums.phpfreaks.com/topic/30862-solved-how-to-retreive-and-echo-just-one-field-from-db/#findComment-142457 Share on other sites More sharing options...
sebastiaandraaisma Posted December 17, 2006 Author Share Posted December 17, 2006 Everything seems to work on-line so I guess its some configuration in MySQL, PHP or Apache :)Anyways, a different question.What if I don't want to echo it out but convert the result into a new variable like this:// This section retreives the country id number for the listed object$result1 = mysql_query("SELECT * FROM objects WHERE `id`='$objid'") or die(mysql_error());while(mysql_fetch_assoc($result1)){$country_id = $result1['countryid'];// This is the new variable that holds the country id for the object}I got an error that the variable was not defined. What should I write instead?Thanks in advance!!! :)Sebas. Link to comment https://forums.phpfreaks.com/topic/30862-solved-how-to-retreive-and-echo-just-one-field-from-db/#findComment-142919 Share on other sites More sharing options...
kenrbnsn Posted December 17, 2006 Share Posted December 17, 2006 In the while statement, you need to assign the results of the mysql_fetch_assoc() function to an array variable and then index into that array. Since it seems like you're expect to get only one value you don't need the while loop.[code]<?php$result1 = mysql_query("SELECT * FROM objects WHERE `id`='$objid'") or die(mysql_error());$row = mysql_fetch_assoc($result1)$country_id = $row['countryid'];// This is the new variable that holds the country id for the object?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/30862-solved-how-to-retreive-and-echo-just-one-field-from-db/#findComment-142926 Share on other sites More sharing options...
sebastiaandraaisma Posted December 17, 2006 Author Share Posted December 17, 2006 Wow, such a quick reply!!!I will try it imideatley! :)Thank you Link to comment https://forums.phpfreaks.com/topic/30862-solved-how-to-retreive-and-echo-just-one-field-from-db/#findComment-142930 Share on other sites More sharing options...
sebastiaandraaisma Posted December 17, 2006 Author Share Posted December 17, 2006 It works!!!Thank you so much. It might be a small thing to you but it means a lot to me.I wish I had the knowledge to help others but in time this will be I hope :)Thanks again! Link to comment https://forums.phpfreaks.com/topic/30862-solved-how-to-retreive-and-echo-just-one-field-from-db/#findComment-142939 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.