im trying to get some data from my DB using this code:
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
$mysqli2 = new mysqli(********************);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$sql2 = "SELECT message FROM wall_workouts_names WHERE id = ? ";
$stmt2 = $mysqli2->prepare($sql2) or trigger_error($mysqli2->error."[$sql2]");
$id_for_wall = '43';
$stmt2->bind_param('s', $id_for_wall);
$stmt2->execute();
$stmt2->bind_result($message);
$stmt2->store_result();
$stmt2->fetch();
echo $message;
?>
My problem is that i get empty string in my echo.
But if i run the same query in my phpmyadmin i get good results.
Thanks for helping
Most likely there are no row to match condition in your WHERE clause, namely with id = 43
Check your incoming results like this.
while ($stmt->fetch()) {
echo $message;
}
Related
Want to output search result from database into an html table, but all I'm getting is a printed array. Fairly certain that's due to "print_r", but how do I output to html table instead and getting shown more than just the first result?
My html table using data from my database works fine - but there I'm just using a "SELECT *" with no user input.
I've tried playing around with my php-code that works with just the "SELECT *", but no luck.
<?php
$servername = "test";
$username = "test";
$password = "test";
$dbname = "test";
$journal = (isset($_SESSION["journal"])) ? $_SESSION["journal"]
: null;
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("connection failed: " . mysqli_connect_error());
mysqli_select_db($conn, $dbname) or die("something went wrong");
if(isset($_POST["form_submit"]))
{
$journal =$_POST["journal"];
$stmt = $conn->prepare("SELECT * FROM straffesager WHERE journalnummer=?");
$stmt->bind_param("s",$journal);
$stmt->execute();
$val = $stmt->get_result();
$row_count= $val->num_rows;
if($row_count >0)
{
$result =$val->fetch_assoc();
print_r($result);
}
else{
echo "<br><br>Den indtastede information matcher ikke sager i databasen.";
}
$stmt->close();
$conn->close();
}
?>
This is my result: "Array ( [idnumber] => 4 [journalnummer] => 17-1717171 [status] => lukket".
The result above is just text. I'd like it to be in the html table I'm using somewhere else as well.
How do I output the result from my code to an HTML table and have more than one search result being shown?
Thanks for helping out a rookie.
instead of
$result =$val->fetch_assoc();
print_r($result);
you should print your table with help of your $result variable
like this
echo "<table><tr><th>HEAD1</th><th>HEAD2</th><th>HEAD3</th></tr>";
while ($row = $val->fetch_assoc()) {
printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>",
$row['idnumber'],$row['journalnummer'],$row['status']);
//it will put $row instead of %s
}
echo "</table>";
<?php
$mysqli = new mysqli("localhost", "root", "", "titan3d");
if (mysqli_connect_error()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$sdate = "";
$stime = "";
if(isset($_POST['sdate']))
{
$sdate = $_POST["sdate"];
}
if(isset($_POST['stime']))
{
$stime = $_POST["stime"];
}
$statement = $mysqli->prepare("SELECT bookedseat FROM bookings WHERE sdate = ? AND stime = ?");{
$statement->bind_param("si", $sdate, $stime);
if (!$statement->execute()) {
trigger_error('Error executing MySQL query: ' . $statement->error);
}
$statement->bind_result($book);
$statement->fetch();
printf($book);
//header('Location: http://localhost/My%20Project/seats.html');
$statement->close();
}
$mysqli->close();
?>
This is my php file made to get the data from a form and make a query and then display the results.
When executed,the php works perfectly.
But it only displays the first value in the query.
Why is it?
How can I display all the values in my query?
You need to use $stmt->fetch() in a while loop, you can then iterate over each the returned row.
while ($stmt->fetch()) {
// $book will have the value of bookedseat for the current row
}
I want to connect my HTML page to my database but I”m not getting it not sure why. What’s wrong with the code?
<?php
$con = mysqli_connect("localhost","root","","sibd02");
$query = sprintf("SELECT*FROM fornecedor ",
$result = mysql_query($query);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_close($con);
?>
There are lots of issues with your code. Here is my cleaned up version of your code which should work:
// Set the connection or die returning an error.
$con = mysqli_connect("localhost", "root", "sibd02") or die(mysqli_connect_errno());
// Set the query.
$query = "SELECT * FROM fornecedor";
// Run the query.
$result = mysqli_query($con, $query) or die(mysqli_connect_errno());
// Print the result for initial testing.
echo '<pre>';
print_r($result);
echo '</pre>';
// Free the result set.
mysqli_free_result($result);
// Close the connection.
mysqli_close($con);
So let’s look at your original code with notes below on each issue:
$con = mysqli_connect("localhost","root","","sibd02");
$query = sprintf("SELECT*FROM fornecedor ",
$result = mysql_query($query);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_close($con);
First this line $query = sprintf("SELECT*FROM fornecedor ", is incorrect syntax in PHP. And the sprintf is not needed.
Then your MySQL query would fail because it has it’s own syntax error: SELECT*FROM fornecedor That SELECT*FROM should have spaces like this SELECT * FROM.
Then you are using mysqli_connect for the connection but then using mysql_query for the query. Those are two 100% different functions in different classes. Mixing them like this will never work.
Mixing MySQL and MySQLi won't work. You can simply do this:
<?php
/* ESTABLISH CONNECTION */
$con=mysqli_connect("localhost","root","","sibd02");
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
$query = "SELECT * FROM fornecedor";
$result = mysqli_query($con,$query); /* EXECUTE QUERY */
/* IF YOU WANT TO PRINT THE RESULTS, HERE IS AN EXAMPLE: */
while($row=mysqli_fetch_array($result)){
echo $row['column']." ".$row['column2']." ".$row['column3']; /* JUST REPLACE THE NECESSARY COLUMN NAME */
} /* END OF WHILE LOOP */
mysqli_close($con);
?>
i have this script to connect my DB and i get error 500 when using it.
I tried to track where the error is and i think its at this line: while ($row = $stmt->fetch_assoc())
But i looked over examples and its the same is mine.
Here is my code thanks for helping:
<?php
$mysqli = new mysqli(*MY DB DETAILS*);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$sql = "SELECT * FROM comments WHERE workout_name =? AND user =?";
$stmt = $mysqli->prepare($sql) or trigger_error($mysqli->error."[$sql]");
$stmt->bind_param('ss', $workout_name, $user);
$workout_name = $_GET['workout_name'];
$user = $_GET['user'];
$stmt->execute();
if ($stmt)
{
while ($row = $stmt->fetch_assoc())
{
$response["name"] = $row["workout_name"];
echo json_encode($response);
}
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
}
else
{
$response["success"] = 2;
// echoing JSON response
echo json_encode($response);
}
?>
UPDATE:
I had to change $row = $stmt->fetch_assoc()
as described here:
How to remove the fatal error when fetching an assoc array
Before you do this line
$stmt->bind_param('ss', $workout_name, $user);
You have to set $workout_name and $user to contain a value.
eg
$workout_name = 'pumping iron';
$user = 'Arnold Schwarzenegger';
$stmt->bind_param('ss', $workout_name, $user);
In your case as you have not even created these variables yet. I assume you are getting the 500 error because the mysqli code goes BANG trying to find NON-EXISTANT variables to load data from.
I have one function selecting a load of information from one table. This then launches another function to get some info from another table.
Here's the shortened code:
$con = new mysqli("localhost", "user", "password");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$con->select_db("stories_test");
getStories($con);
function getStories($con) {
... //get's a load of data from one table
$result = getStoryName($con, $stringstoryid);
... //More stuff here
}
function getStoryName($con) {
$newquery = "SELECT storyname, genre FROM stories WHERE storyid = 'Anewstory9856'";
if ($stmt = $con->prepare($newquery)) {
echo $newquery;
$stmt->execute();
$stmt->bind_result($storname, $genre);
while ($stmt->fetch()) {
$resultarray = array (
'storname' => $storname,
'genre' => $genre
);
}
}
else {
echo 'statement failed';
}
return $resultarray;
}
All I ever get is 'statement failed' from the second query.
I have tried the exact same query in a separate script on its own and it works fine, but here it seems to fail at 'prepare'.
Does anyone have any clues?