Forum Moderators: coopster

Message Too Old, No Replies

Just need one col from one record

How do I do it

         

ogletree

7:04 pm on Jul 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have a MySQL db I can connect to it but I don't know enough about PHP/mySQL to get it.

Here is what I have already


<?
$db_server ="mysql.domain.com";
$db_name = "db_name";
$username = "user";
$password = "pass";
$dbh = @mysql_connect($db_server,$username,$password) or die
("Connection to $db_server with login '$username'/'$password' failed.");
$db = @mysql_select_db($db_name) or die
("Connection made. But database '$db_name' was not found.");
$query="SELECT col_2 FROM `my_table` WHERE id='4'";
$result=mysql_query($query);
echo "$result";
?>

I get "Resource id #2"

I'm pretty sure the select statement is right. It works from the control panel where I can run SQL.

sned

7:20 pm on Jul 7, 2004 (gmt 0)

10+ Year Member



Well you can do a few things here ..

you can put the result in an object: $object = mysql_fetch_object($result)

Access your column with:
$object->col_2

or you can fetch an array: $array = mysql_fetch_array($result)

or, in your case, the easiest way might be:

echo mysql_result($result, 0, "col_2");

<edit>Some Links:
[us4.php.net...]
[us4.php.net...]
[us4.php.net...]
</edit>

[edited by: sned at 7:23 pm (utc) on July 7, 2004]

Netizen

7:21 pm on Jul 7, 2004 (gmt 0)

10+ Year Member



And the missing line is:

$row=mysql_fetch_row($result);
echo $row;

Check out the mysql [php.net] part of the PHP manual.

Timotheos

7:21 pm on Jul 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well you're almost there Ogletree, just add these lines.

$row = mysql_fetch_assoc($result);
echo $row['col_2'];

btw, I don't know if I like the looks of this...
or die
("Connection to $db_server with login '$username'/'$password' failed.")
Hope that isn't a production thing ;-)

Timotheos

7:22 pm on Jul 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks like I got the bronze medal on this one ;-)

timster

7:37 pm on Jul 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was little late -- never mind

[edited by: timster at 7:37 pm (utc) on July 7, 2004]

ogletree

7:37 pm on Jul 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Timotheos I don't know what that is I copied it from my ISP's directions to connect to their db.

By the way that worked thank you.

Timotheos

8:11 pm on Jul 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$dbh = @mysql_connect($db_server,$username,$password) or die
("Connection to $db_server with login '$username'/'$password' failed.");

It means that if you are unable to connect then a message will be displayed showing your username and password. Beyond debugging I can't think of any reason do this. In fact it's a compromise to your security. I'd suggest replacing it with just a simple message like "Failed to connect to server".

ogletree

8:34 pm on Jul 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Oh I guess that would be bad.

ogletree

9:31 pm on Jul 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ok I have that code working. I put that code in the body. How would I use this code to put the same info in my title.

Timotheos

10:00 pm on Jul 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's more then one way to skin a cat but here's one...

Typically it's good practice to put your db info and connection in a separate file which you can include into whatever page your gonna use that database.

So put this in a file called db.php
<?php
$db_server ="mysql.domain.com";
$db_name = "db_name";
$username = "user";
$password = "pass";
$dbh = @mysql_connect($db_server,$username,$password) or die
("Failed to connect to server.");
$db = @mysql_select_db($db_name) or die
("Failed to select database.");
?>

Now back in the page you're working on put this at the very top to relace the code above:

<?php
include("db.php");
?>

Now when it comes to your title you can do it this way:

<html>
<head>
<title>
<?php
$query="SELECT col_2 FROM `my_table` WHERE id='4'";
$result=mysql_query($query);
$row=mysql_fetch_assoc($result);
echo $row['col_2'];
?>
</title>
<link rel="STYLESHEET" type="text/css" href="format.css">
</head>

ogletree

10:48 pm on Jul 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is there any way to do it without double quotes. I am using access in part of my process and access does not like double quotes.

Timotheos

11:18 pm on Jul 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure what you mean. Could you elobarate?

ogletree

3:50 am on Jul 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Never mind I got it all working think you so much for your help.

ogletree

6:13 pm on Jul 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ok now I want to get fancy. Right now I know that the query will always return only one result. What I want to do is add a LIKE so that it may return more than one result. I never know how many it may find. I do know that it will always find at least one result.

Here is the Query that I think should work.

$query="SELECT * FROM 'myTable' WHERE myCol LIKE '$myVar'";

Right now I have
echo $row['price'];

But this only shows the first record because there is only one. I want to search the results and find which one has the lowest price and show that one.

HughMungus

11:22 pm on Jul 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[dev.mysql.com...]

MIN and MAX are probably what you're looking for.

Netizen

7:32 pm on Jul 12, 2004 (gmt 0)

10+ Year Member



Take a look at the MySQL SELECT [dev.mysql.com] statement - an ORDER BY is probably what you want.

$query="SELECT * FROM 'myTable' WHERE myCol LIKE '%$myVar%'" ORDER BY price DESC LIMIT 1;

This will only work if your price field is numeric rather than a string like $13.99.

[Note: in the LIKE clause you need to surround your search string with %'s - as in the example above]