0

I am trying to get a url from an url array created from db but my array seems empty.How should i get any url from db table's url column.

    $dbc = mysqli_connect($servername, $username, $pass, $dbname) or die('connect failed');
    $r= mysqli_query ($dbc, "select url from urllist ")or die('query failed');

     $url = array();

while($set = mysql_fetch_array($r)){
    $url[] = $set['url'];
}
echo $url[5];
trmt
  • 137
  • 1
  • 14

1 Answers1

0

Use var_dump($url); to inspect your variable contents. You might be trying to access a non-existent index!

Try the following code:

OO Style

$con = new mysqli($servername, $username, $pass, $dbname);
$res = $con->query('SELECT url FROM urllist');
$url = [];

while($row = $res->fetch_array())
{
    $url[] = $row['url'];
}

var_dump($url);

Procedural Style

$con = mysqli_connect($servername, $username, $pass, $dbname);
$res = mysqli_query($con, 'SELECT url FROM urllist');
$url = mysqli_fetch_array($res, MYSQLI_ASSOC);

var_dump($url);

Hope it Helps!

Haine
  • 191
  • 1
  • 7
  • 1
    thats what i was looking for thanks.this is object oriented style so we can solve with procedural style ? – trmt Nov 12 '15 at 00:14
  • Yes, of course! I'll update with a procedural style, just a minute. – Haine Nov 12 '15 at 00:16