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!

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.