Jump to content

Trying to use mysql_fetch_array but the output keeps on being "Array"!?


champbronc2

Recommended Posts

<?php
$id = $_GET['d'];
$servername="localhost";
    $username="c_links";
    $password="pass";
    $conn=  mysql_connect($servername,$username,$password)or die(mysql_error());
    mysql_select_db("c_links",$conn);
$query1 = mysql_query("SELECT url FROM links WHERE id='$id'");
$url = mysql_fetch_array($query1) or die(mysql_error());
$query2 = mysql_query("SELECT name FROM links WHERE id='$id'");
$name = mysql_fetch_array($query2) or die(mysql_error());
$query3 = mysql_query("SELECT size FROM links WHERE id='$id'");
$size = mysql_fetch_array($query3) or die(mysql_error());
?>
<html>
blah blah blah
<?php eco $id; ?> //THIS ONE ACTUALLY WORKS
<?php echo $url; ?>
<?php echo $name; ?>
<?php echo $size; ?>
</html>

 

When I do this and I go to http://example.com/?d=xxxx, where ever I have $url $name and $size in my html, the output is "Array" when it should be actually outputting a URL or a name or size.

 

The $id one is the only one that works in the HTML body.

 

The page completely loads and there are no error messages displayed.

 

What is wrong with the code? Shouldn't it query my database, then when I run fetch_array store whatever the result is into the variable???

 

Thanks for any help!

You fetch 1 row which is an array of the data.  Also, you will only get url because that is all you put in the query.

 

$query1 = mysql_query("SELECT url, name, size FROM links WHERE id='$id'")  or die(mysql_error());
$row = mysql_fetch_array($query1);
$url = $row['url'];
$name = $row['name'];
$size = $row['size'];

nooo you are echoing the contents of the array that mysql_fetch_array() outputs. by using the constructor echo() you are converting the array into a string which will always output "Array" do what abra said in order to correctly output your array values

You fetch 1 row which is an array of the data.  Also, you will only get url because that is all you put in the query.

 

$query1 = mysql_query("SELECT url, name, size FROM links WHERE id='$id'")  or die(mysql_error());
$row = mysql_fetch_array($query1);
$url = $row['url'];
$name = $row['name'];
$size = $row['size'];

 

Also, if you are only returning 1 row then you can put LIMIT 1 on the end of the query.

Archived

This topic is now archived and is closed to further replies.

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