Forum Moderators: coopster

Message Too Old, No Replies

Help! Simple MySql query with PHP

         

mattclayb

4:25 pm on Jul 5, 2005 (gmt 0)

10+ Year Member



Help, I'm having loads of trouble with a very simple task,
I want to query a specific value from a table using variables in PHP to point to the column and row, and return the value to PHP as a usable integer.
I'm using the following code, but the query will only return the value 'Array'
Any Ideas?

$sql = "SELECT '".$shipCode."' FROM shipping WHERE shipType = '".$shipType."'";

$result = mysql_query($sql) or die('Error: ' . mysql_error());

$row = mysql_fetch_assoc($result);

if($row === false) die('Row not found');

echo $row;

sned

4:31 pm on Jul 5, 2005 (gmt 0)

10+ Year Member



Hi mattclayb ...

mysql_fetch_assoc returns an array that looks something like:

'field_name'=>'value'

So to return an actual value from the query, you would have to call it like this:

echo $row['shipType']; (or fill in whatever column you want)

-sned

mcibor

6:54 pm on Jul 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try out this code:

$sql = "SELECT shipCode FROM shipping WHERE shipType = '$shipType'";
$result = mysql_query($sql) or die('Error: ' . mysql_error());
$row = mysql_fetch_assoc($result);
if($row === false) die('Row not found');

print_r($row);

This will print you exactly what's in the variable. Including the type.

Remember that in SELECT you DON'T want the variable there!
Michal Cibor