I'm trying to display data from my SQL database on a HTML page through a php query.
I've done a similar query before in the same way and that worked, however this php query doesn't display any results on my html page and I have no idea why.
<?php
$con=mysqli_connect("localhost","root","password","registration");
if (mysqli_connect_errno())
{
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
$query = "SELECT * FROM goal WHERE id = $_SESSION[userid]";
$result = mysqli_query($con, $query);
if($result == false) {
die("Query failed: ".mysqli_error($con).PHP_EOL.$query);
}
while($row = mysqli_fetch_assoc($result))
{
echo "Weight Lost:"."<p>".$row['weightlost']."</p>\n";
echo "Weight Unit:"."<p>".$row['weightunit']."</p>\n";
echo "Date set for goal to be reached:"."<p>".$row['whenby']."</p>\n";
}
mysqli_close($con);
?>
Related
this is my first question here so feedback on how to improve my questions would be appreciated.
I am trying to display records from a database using PHP.
Here is the code
<?php
$dbConn = new mysqli('localhost', 'twa037', 'twa037Dg', 'autoservice037');
if($dbConn->connect_error) {
die("failed to connect to the database: " . $dbConn->connect_error);
}
else
{
echo "success";
}
$sql = "select * from customer ";
if ($dbConn->query($sql) )
{
echo "query successful";
}
while($row = $sql->fetch_assoc()) {
echo $row['familyName'];
}
$dbconn->close();
?>
I am able to connect to the database and also the query appears to be working fine as it is displaying "success" and "query successfull".
However, I am getting this error
Fatal error: Uncaught Error: Call to a member function fetch_assoc()
on string
Before you guys suggest this could be a duplicate of another post, I have noticed that in the other posts they have used the same code as I have without an issue.
mysqli_query returns a result on that you can call fetch_assoc(). not on the querystring itself.
if ($result = $dbConn->query($sql) )
{
echo "query successful";
}
while($row = $result->fetch_assoc()) {
more informations you can find here
while($row = $sql->fetch_assoc()) <=> while($row = $dbConn->query($sql)->fetch_assoc())
Good day,
I have done extensive research on this issue but unfortunately none of the related issues solved my problem.
Here I have a very basic PHP mySQLi db connection. The connection succeeds and so does the query that is run on the table. The issue is that the the result set will not display. All of my references are correct and when I check to see if the result set is populated, it is. I believe the issue is with my while block but no errors are returned when this is run.
Thank you for your time
<?php
$db = mysqli_connect('localhost','root','','securitour') //connection to the database
or die('Error connecting to MySQL server.');
?>
<html>
<head>
</head>
<body>
<?php
$query = "SELECT * FROM location"; //The SQL query
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query); //query the table an store the result set in a variable
$row = mysqli_fetch_array($result); //create an array and store the records of the result set in it
if (mysqli_num_rows($result) != 0) //to check if the result set contains data
{
echo "results found"; //THIS is what is returned.
}
else
{
echo "results not found";
}
while ($row = $result->fetch_assoc()) //itterate through the array and display the name column of each record
{
echo $row['name'];
}
mysqli_close($db);
?>
</body>
</html>
Lots of things not right here.
You are processing the mysqli_query() function twice - there is no need.
You are selecting all fields in your SQL query (SELECT *). You should select fields by name.
You're interchanging between procedure and class-based MySQLi - You should stick to one or the other.
Try this instead:
<?php
$db = mysqli_connect('localhost','root','','securitour') //connection to the database
or die('Error connecting to MySQL server.');
?>
<html>
<head>
</head>
<body>
<?php
$query = "SELECT name FROM location"; //The SQL query
$result = mysqli_query($db, $query) or die('Error querying database'); //query the table an store the result set in a variable
if(mysqli_num_rows($result) > 0){
echo "Results found!";
while($row = mysqli_fetch_array($result)){ //create an array and store the records of the result set in it
echo $row['name'];
}
} else {
echo "results not found";
}
mysqli_close($db);
?>
</body>
</html>
You don't need to run mysqli_query() twice. and you need to use mysqli_fetch_assoc for associative array
<?php
$db = mysqli_connect('localhost','root','','securitour') or die('Error connecting to MySQL server.');
?>
<html>
<head>
</head>
<body>
<?php
$query = "SELECT * FROM location"; //The SQL query
$result = mysqli_query($db, $query) or die('Error querying database.'); //query the table an store the result set in a variable
$row = mysqli_fetch_assoc($result); //create an array and store the records of the result set in it
if (mysqli_num_rows($result) != 0) //to check if the result set contains data
{
echo "results found"; //THIS is what is returned.
} else {
echo "results not found";
}
foreach ( $row as $name=>$val) {
echo $name . ':' . $val . '<br>';
}
mysqli_close($db);
?>
</body>
</html>
I have been working on a website which has a xampp server and a database called users with a table called AccountDetails. About a year ago I got it to work perfectly, but the server I was using then required MySQL not MySQLi. Now I have to use MySQLi and can't even get the simplest of sql's SELECT function to work, any ideas would be much appreciated.
<?php
$link = mysqli_connect("localhost:3306", "root","", "users");
if(mysqli_connect_errno($link)){
echo "MySql Error: " . mysqli_connect_error();
} else {
echo"Connection Successful <br></br>";
}
echo("Check if still working <br></br>");
// -----------------------------//
echo("Its running <br></br>");
$result = $link->query("SELECT ID, UserName FROM AccountDetails");
return $result->result();
var_dump($result);
mysqli_close($link);
?>
The Query itself works when I plug it into the phpmyadmin SQL section and it returns the values that I expect it too.
I've spent days looking online for different answers but none of them work, and the var_dump only gives me "bool(false)" which I don't think I should be getting.
You can try this code
<?php
$link = mysqli_connect("localhost:3306", "root","", "users");
if(mysqli_connect_errno($link)){
echo "MySql Error: " . mysqli_connect_error();
} else {
echo"Connection Successful <br></br>";
}
echo("Check if still working <br></br>");
// -----------------------------//
echo("Its running <br></br>");
$sql_select = "SELECT * FROM AccountDetails";
$result = $link->query($sql_select);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "UserName: " . $row['UserName']. "<br>";
}
} else {
echo "No Records";
}
$link->close();
?>
I want to retrieve data from my SQL database and use it on my HTML page.
I have a php script get_score.php like this:
<?php
$con = mysqli_connect( "localhost", "xxx1", "xxx2", "xxx3");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT `name`, `score` FROM crossstreet ORDER BY `score` DESC LIMIT 5";
if (!mysqli_query($con, $sql))
{
die('Error: ' . mysqli_error($con));
}
$result = mysqli_query($con, $sql);
while ($row = mysqli_fetch_assoc($result)) {
$output = ($row["name"]);
}
mysqli_free_result($result);
echo json_encode($result);
mysqli_close($con);
?>
And then I want to retrieve the data I via JQuery. I have read about the cross-origin issue but the data I retrieve and the html/Jquery are on the same server.
$.get("get_score.php", function( data ) {
console.log(window[data]);
}, "json" );
This returns undefined. If I make it $(data) it returns an object like the one on this screenshot.
Where is my mistake; how can I just get the data from the server to use in html?
You need to make $output an array so you get all the rows.
$output = array();
while ($row = mysqli_fetch_assoc($result)) {
$output[] = $row['name'];
}
Then you should echo this:
echo json_encode($output);
And in your jQuery code, you should do console.log(data);
mysqli_free_result($result);
echo json_encode($result);
i think this is the problem.
Try to reverse the order. You are making the result free before using it.
Do like this, instead of the above .
echo json_encode($result);
mysqli_free_result($result);
The script below connects to the db (I get the connected successfully echo) but none of the data from the query is shown onscreen.
I assume the data must be somewhere as I do not get the error message.
Question: Where is the error in the script?
<?php
//connectdb();
$con = mysqli_connect("localhost","UN","PW");
if ( $con == "" ) { echo " DB Connection error...\r\n"; exit(); }
echo 'Connected successfully';
$result = mysqli_query($con, "SELECT graduation_year FROM wp_gfsept2013");
while($row = mysql_fetch_array($result))
if ($result === "") {echo "An error occurred.";}
{
echo $row['graduation_year'];
echo "<br>";
}
?>
Appreciate any help that can be sent my way, I'm a real newbie at this stuff.
Roger
Try adding an opening brace after while($row = mysql_fetch_array($result)) and a closing brace before the end of the script.
Is this not a syntax issue?? Why is there an IF clause after a WHILE clause but before the opening bracket for the WHILE loop block?
Additionally, you are trying to use mysql_fetch_array() instead of mysqli_fetch_array().
<?php
//connectdb();
$con = mysqli_connect("localhost","UN","PW");
if ( $con == "" ) { echo " DB Connection error...\r\n"; exit(); }
echo 'Connected successfully';
$result = mysqli_query($con, "SELECT graduation_year FROM wp_gfsept2013");
if ($result !== FALSE && mysqli_num_rows($result) > 0) { // Proper way to test for results
while($row = mysqli_fetch_assoc($result))
{
echo $row['graduation_year'];
echo "<br/>";
}
}
else {
die("Query Returned 0 rows...");
}
?>
Documentation: mysqli_result::$num_rows