Jump to content

[SOLVED] How to retreive and echo just one field from db?


sebastiaandraaisma

Recommended Posts

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 name

What 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
Share on other sites

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 13

On 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
Share on other sites

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
Share on other sites

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 installed
I 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
Share on other sites

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
Share on other sites

[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
Share on other sites

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 selected

I 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_global

I realy apriciate the help I'm getting here
I 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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.