Jump to content

Echo SQL result?


brokenglass

Recommended Posts

Hi guys,

 

Here's my code:

 

<?php
$servername="127.0.0.1";
$username="root";
$password="";
$dbname="testdb";
$connection = new mysqli($servername,$username,$password,$dbname);
$sql = "SELECT Wages FROM w WHERE \"Country\"=\"".$_POST["blah"]."\"";
$r = $connection->query($sql);
$result = $r->fetch_all(MYSQLI_ASSOC);
echo $result;
?>

 

When I go to the page I get the following message:

Notice: Undefined index: w in C:\xampp\htdocs\test\processing.php on line 13
 

Does anyone know what problem I'm having here? /stuck

 

Cheers,

B

Link to comment
https://forums.phpfreaks.com/topic/292318-echo-sql-result/
Share on other sites

Also my actual code is:

 

 

<?php
$servername="127.0.0.1";
$username="root";
$password="";
$dbname="testdb";
$connection = new mysqli($servername,$username,$password,$dbname);
$sql = "SELECT Wages FROM w WHERE Country=\"".$_POST["blah"]."\"";
echo $sql;
$r = $connection->query($sql);
$result = $r->fetch_all(MYSQLI_ASSOC);
echo $result;
?>
Link to comment
https://forums.phpfreaks.com/topic/292318-echo-sql-result/#findComment-1495934
Share on other sites

fetch_all() returns and array of the returned rows which you would then loop through, as in your last posted topic.

 

If you only expect a single row for the selected country then it is

$r = $connection->query($sql);
$result = $r->fetch_assoc();
echo $result['Wages'];
Link to comment
https://forums.phpfreaks.com/topic/292318-echo-sql-result/#findComment-1495935
Share on other sites

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.